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.

Examples:

dumper = MultitenancyTools::FunctionsDumper.new('schema name')
dumper.dump_to('path/to/file.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



11
12
13
14
# File 'lib/multitenancy_tools/functions_dumper.rb', line 11

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:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/multitenancy_tools/functions_dumper.rb', line 23

def dump_to(file, mode: 'w')
  results = @connection.execute("    SELECT\n      trim(trailing e' \\n' from pg_get_functiondef(f.oid)) || ';\\n'\n      AS definition\n    FROM pg_catalog.pg_proc f\n    INNER JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid)\n    WHERE n.nspname = \#{@schema};\n  SQL\n\n  File.open(file, mode) do |f|\n    results.each do |result|\n      f.write result['definition']\n    end\n  end\nend\n")