From b98198012cdb86951583f694db89851aa12aec9b Mon Sep 17 00:00:00 2001 From: Tassos Bareiss Date: Wed, 18 Jan 2023 13:55:26 -0700 Subject: [PATCH] Retain schema in name when parsing out functions (#272) --- lib/pg_query/parse.rb | 6 +++--- spec/lib/parse_spec.rb | 32 ++++++++++++++++---------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/pg_query/parse.rb b/lib/pg_query/parse.rb index 63c7db5..d953bb2 100644 --- a/lib/pg_query/parse.rb +++ b/lib/pg_query/parse.rb @@ -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 @@ -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 }, diff --git a/spec/lib/parse_spec.rb b/spec/lib/parse_spec.rb index a2c5338..454133d 100644 --- a/spec/lib/parse_spec.rb +++ b/spec/lib/parse_spec.rb @@ -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