Skip to content
Permalink
Browse files

Support unique functions with parameters (#27)

This PR adds support for dropping functions on PostgreSQL 10 without
having to specify the full argument signature.
This works for functions that are uniquely named and will not work for
functions that share the same name but their argument signatures differ.
  • Loading branch information
stoivo authored and teoljungberg committed Jun 7, 2019
1 parent 6443303 commit 53f8b3fb651e7574f98f6ff3685361a13de0981c
Showing with 76 additions and 16 deletions.
  1. +8 −1 .travis.yml
  2. +12 −1 lib/fx/adapters/postgres.rb
  3. +20 −0 spec/acceptance/user_manages_functions_spec.rb
  4. +36 −14 spec/fx/adapters/postgres_spec.rb
@@ -1,10 +1,17 @@
addons:
postgresql: "9.4"
postgresql: "10"
apt:
packages:
- postgresql-10
- postgresql-client-10
before_install:
- "echo '--colour' > ~/.rspec"
- "echo 'gem: --no-document' > ~/.gemrc"
- git config --global user.name "Travis CI"
- git config --global user.email "travis-ci@example.com"
env:
global:
- PGPORT=5433
branches:
only:
- master
@@ -125,7 +125,11 @@ def update_trigger(name, on:, sql_definition:)
#
# @return [void]
def drop_function(name)
execute "DROP FUNCTION #{name}();"
if support_drop_function_without_args
execute "DROP FUNCTION #{name};"
else
execute "DROP FUNCTION #{name}();"
end
end

# Drops the trigger from the database
@@ -150,6 +154,13 @@ def drop_trigger(name, on:)
def connection
Connection.new(connectable.connection)
end

def support_drop_function_without_args
# https://www.postgresql.org/docs/9.6/sql-dropfunction.html
# https://www.postgresql.org/docs/10/sql-dropfunction.html

PG.connect.server_version >= 10_00_00
end
end
end
end
@@ -34,4 +34,24 @@
result = execute("SELECT * FROM test() AS result")
expect(result).to eq("result" => "testest")
end

it "handles functions with arguments" do
successfully "rails generate fx:function adder"
write_function_definition "adder_v01", <<-EOS
CREATE FUNCTION adder(x int, y int)
RETURNS int AS $$
BEGIN
RETURN $1 + $2;
END;
$$ LANGUAGE plpgsql;
EOS
successfully "rake db:migrate"

result = execute("SELECT * FROM adder(1, 2) AS result")
result["result"] = result["result"].to_i
expect(result).to eq("result" => 3)

successfully "rails destroy fx:function adder"
successfully "rake db:migrate"
end
end
@@ -53,22 +53,44 @@ module Fx::Adapters
end

describe "#drop_function" do
it "successfully drops a function" do
adapter = Postgres.new
adapter.create_function(
<<-EOS
CREATE OR REPLACE FUNCTION test()
RETURNS text AS $$
BEGIN
RETURN 'test';
END;
$$ LANGUAGE plpgsql;
EOS
)
context "when the function has arguments" do
it "successfully drops a function with the entire function signature" do
adapter = Postgres.new
adapter.create_function(
<<-EOS
CREATE FUNCTION adder(x int, y int)
RETURNS int AS $$
BEGIN
RETURN $1 + $2;
END;
$$ LANGUAGE plpgsql;
EOS
)

adapter.drop_function(:adder)

expect(adapter.functions.map(&:name)).not_to include("adder")
end
end

context "when the function does not have arguments" do
it "successfully drops a function" do
adapter = Postgres.new
adapter.create_function(
<<-EOS
CREATE OR REPLACE FUNCTION test()
RETURNS text AS $$
BEGIN
RETURN 'test';
END;
$$ LANGUAGE plpgsql;
EOS
)

adapter.drop_function(:test)
adapter.drop_function(:test)

expect(adapter.functions.map(&:name)).not_to include("test")
expect(adapter.functions.map(&:name)).not_to include("test")
end
end
end

0 comments on commit 53f8b3f

Please sign in to comment.
You can’t perform that action at this time.