Class: MultitenancyTools::FunctionsDumper

Inherits:
Object
  • Object
show all
Defined in:
lib/multitenancy_tools/functions_dumper.rb

Overview

FunctionsDumper can be used to generate a SQL dump of all functions that are present on a PostgreSQL schema.

Please note that C functions are not included in the dump.

Examples:

dumper = MultitenancyTools::FunctionsDumper.new('schema name')
dumper.dump_to('path/to/file.sql')

Constant Summary collapse

FUNCTIONS_SQL =
<<-SQL.freeze
  SELECT
    trim(trailing e' \n' from pg_get_functiondef(f.oid)) || ';'
    AS definition
  FROM pg_catalog.pg_proc f
  JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid)
  JOIN pg_catalog.pg_language l ON (f.prolang = l.oid)
  WHERE n.nspname = %s
  AND l.lanname != 'c';
SQL

Instance Method Summary collapse

Constructor Details

#initialize(schema, connection = ActiveRecord::Base.connection) ⇒ FunctionsDumper

Returns a new instance of FunctionsDumper.

Parameters:

  • schema (String)

    schema name

  • connection (ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) (defaults to: ActiveRecord::Base.connection)

    connection adapter



24
25
26
27
# File 'lib/multitenancy_tools/functions_dumper.rb', line 24

def initialize(schema, connection = ActiveRecord::Base.connection)
  @connection = connection
  @schema = @connection.quote(schema)
end

Instance Method Details

#dump_to(file, mode: 'w') ⇒ Object

Generates a dump and writes it into a file. Please see IO.new for open modes.

Parameters:

  • file (String)

    file path

  • mode (String) (defaults to: 'w')

    IO open mode

See Also:



36
37
38
39
40
41
42
43
44
# File 'lib/multitenancy_tools/functions_dumper.rb', line 36

def dump_to(file, mode: 'w')
  results = @connection.execute(format(FUNCTIONS_SQL, @schema))

  File.open(file, mode) do |f|
    results.each do |result|
      f.puts result.fetch('definition')
    end
  end
end