Skip to content

Commit

Permalink
Retain schema in name when parsing out functions (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tassosb authored Jan 18, 2023
1 parent 5586abf commit b981980
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
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

0 comments on commit b981980

Please sign in to comment.