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(<<-SQL) SELECT trim(trailing e' \n' from pg_get_functiondef(f.oid)) || ';\n' AS definition FROM pg_catalog.pg_proc f INNER JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid) WHERE n.nspname = #{@schema}; SQL File.open(file, mode) do |f| results.each do |result| f.write result['definition'] end end end |