Module: PGSpecHelper::Functions

Included in:
PGSpecHelper
Defined in:
lib/pg_spec_helper/functions.rb

Instance Method Summary collapse

Instance Method Details

#create_function(schema_name, function_name, function_definition) ⇒ Object

create a function



6
7
8
9
10
11
12
13
14
# File 'lib/pg_spec_helper/functions.rb', line 6

def create_function schema_name, function_name, function_definition
  connection.exec <<~SQL
    CREATE FUNCTION #{schema_name}.#{function_name}() returns trigger language plpgsql AS
    $$#{function_definition.strip}$$;
  SQL
  # so we can delete them later
  @created_functions ||= []
  @created_functions << {schema_name: schema_name, function_name: function_name}
end

#delete_created_functionsObject

delete all functions which were created by this helper



31
32
33
34
35
36
37
38
# File 'lib/pg_spec_helper/functions.rb', line 31

def delete_created_functions
  @created_functions&.each do |function|
    connection.exec(<<~SQL)
      DROP FUNCTION IF EXISTS #{function[:schema_name]}.#{function[:function_name]};
    SQL
  end
  @created_functions = []
end

#get_function_names(schema_name) ⇒ Object

return a list of function names for the provided schema



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pg_spec_helper/functions.rb', line 17

def get_function_names schema_name
  # get the function names
  rows = connection.exec(<<~SQL, [schema_name.to_s])
    SELECT
      routine_name
    FROM
      information_schema.routines
    WHERE
      routine_schema = $1
  SQL
  rows.map { |r| r["routine_name"].to_sym }
end