Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain schema in name when parsing out functions #272

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/pg_query/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def load_objects! # rubocop:disable Metrics/CyclomaticComplexity
when :OBJECT_FUNCTION
# Only one function can be dropped in a statement
obj = statement.drop_stmt.objects[0].object_with_args
@functions << { function: obj.objname[0].string.str, type: :ddl }
@functions << { function: obj.objname.map { |f| f.string.str }.join('.'), type: :ddl }
end
when :grant_stmt
objects = statement.grant_stmt.objects
Expand All @@ -235,12 +235,12 @@ def load_objects! # rubocop:disable Metrics/CyclomaticComplexity
statements << statement.explain_stmt.query
when :create_function_stmt
@functions << {
function: statement.create_function_stmt.funcname[0].string.str,
function: statement.create_function_stmt.funcname.map { |f| f.string.str }.join('.'),
type: :ddl
}
when :rename_stmt
if statement.rename_stmt.rename_type == :OBJECT_FUNCTION
original_name = statement.rename_stmt.object.object_with_args.objname[0].string.str
original_name = statement.rename_stmt.object.object_with_args.objname.map { |f| f.string.str }.join('.')
new_name = statement.rename_stmt.newname
@functions += [
{ function: original_name, type: :ddl },
Expand Down
32 changes: 16 additions & 16 deletions spec/lib/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1176,49 +1176,49 @@

it 'correctly finds created functions' do
query = described_class.parse(<<-SQL)
CREATE OR REPLACE FUNCTION testfunc(x integer) RETURNS integer AS $$
BEGIN
RETURN x
END;
$$ LANGUAGE plpgsql STABLE;
CREATE OR REPLACE FUNCTION foo.testfunc(x integer) RETURNS integer AS $$
BEGIN
RETURN x
END;
$$ LANGUAGE plpgsql STABLE;
SQL
expect(query.tables).to eq []
expect(query.warnings).to eq []
expect(query.functions).to eq ['testfunc']
expect(query.ddl_functions).to eq ['testfunc']
expect(query.functions).to eq ['foo.testfunc']
expect(query.ddl_functions).to eq ['foo.testfunc']
expect(query.call_functions).to eq []
end

it 'correctly finds called functions' do
query = described_class.parse(<<-SQL)
SELECT testfunc(1);
SELECT foo.testfunc(1);
SQL
expect(query.tables).to eq []
expect(query.warnings).to eq []
expect(query.functions).to eq ['testfunc']
expect(query.functions).to eq ['foo.testfunc']
expect(query.ddl_functions).to eq []
expect(query.call_functions).to eq ['testfunc']
expect(query.call_functions).to eq ['foo.testfunc']
end

it 'correctly finds dropped functions' do
query = described_class.parse(<<-SQL)
DROP FUNCTION IF EXISTS testfunc(x integer);
DROP FUNCTION IF EXISTS foo.testfunc(x integer);
SQL
expect(query.tables).to eq []
expect(query.warnings).to eq []
expect(query.functions).to eq ['testfunc']
expect(query.ddl_functions).to eq ['testfunc']
expect(query.functions).to eq ['foo.testfunc']
expect(query.ddl_functions).to eq ['foo.testfunc']
expect(query.call_functions).to eq []
end

it 'correctly finds renamed functions' do
query = described_class.parse(<<-SQL)
ALTER FUNCTION testfunc(integer) RENAME TO testfunc2;
ALTER FUNCTION foo.testfunc(integer) RENAME TO testfunc2;
SQL
expect(query.tables).to eq []
expect(query.warnings).to eq []
expect(query.functions).to eq ['testfunc', 'testfunc2']
expect(query.ddl_functions).to eq ['testfunc', 'testfunc2']
expect(query.functions).to eq ['foo.testfunc', 'testfunc2']
expect(query.ddl_functions).to eq ['foo.testfunc', 'testfunc2']
expect(query.call_functions).to eq []
end

Expand Down