Class: MultitenancyTools::FunctionsDumper
- Inherits:
-
Object
- Object
- MultitenancyTools::FunctionsDumper
- 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.
Instance Method Summary collapse
-
#dump_to(file, mode: 'w') ⇒ Object
Generates a dump and writes it into a file.
-
#initialize(schema, connection = ActiveRecord::Base.connection) ⇒ FunctionsDumper
constructor
A new instance of FunctionsDumper.
Constructor Details
#initialize(schema, connection = ActiveRecord::Base.connection) ⇒ FunctionsDumper
Returns a new instance of FunctionsDumper.
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.
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") |