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 "    CREATE FUNCTION \#{schema_name}.\#{function_name}() returns trigger language plpgsql AS\n    $$\#{function_definition.strip}$$;\n  SQL\n  # so we can delete them later\n  @created_functions ||= []\n  @created_functions << {schema_name: schema_name, function_name: function_name}\nend\n"

#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("      DROP FUNCTION IF EXISTS \#{function[:schema_name]}.\#{function[:function_name]};\n    SQL\n  end\n  @created_functions = []\nend\n")

#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("    SELECT\n      routine_name\n    FROM\n      information_schema.routines\n    WHERE\n      routine_schema = $1\n  SQL\n  rows.map { |r| r[\"routine_name\"].to_sym }\nend\n", [schema_name.to_s])