Module: MultitenancyTools

Defined in:
lib/multitenancy_tools.rb,
lib/multitenancy_tools/errors.rb,
lib/multitenancy_tools/version.rb,
lib/multitenancy_tools/dump_cleaner.rb,
lib/multitenancy_tools/table_dumper.rb,
lib/multitenancy_tools/schema_dumper.rb,
lib/multitenancy_tools/dump/data_only.rb,
lib/multitenancy_tools/schema_creator.rb,
lib/multitenancy_tools/schema_migrator.rb,
lib/multitenancy_tools/schema_switcher.rb,
lib/multitenancy_tools/dump/schema_only.rb,
lib/multitenancy_tools/functions_dumper.rb,
lib/multitenancy_tools/schema_destroyer.rb,
lib/multitenancy_tools/extensions_dumper.rb

Defined Under Namespace

Modules: Dump Classes: DumpCleaner, ExtensionsDumper, FunctionsDumper, PgDumpError, SchemaCreator, SchemaDestroyer, SchemaDumper, SchemaMigrator, SchemaSwitcher, TableDumper

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.create(name, sql_file, connection = ActiveRecord::Base.connection) ⇒ Object

Creates a new schema using the SQL file as template. This SQL file can be generated by SchemaDumper.

Parameters:

  • name (String)

    schema name

  • sql_file (String)

    absolute path to the SQL template

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

    connection adapter

See Also:



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

def self.create(name, sql_file, connection = ActiveRecord::Base.connection)
  SchemaCreator.new(name, connection).create_from_file(sql_file)
end

.destroy(name, connection = ActiveRecord::Base.connection) ⇒ Object

Drops the schema from the database.

Parameters:

  • name (String)

    schema name

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

    connection adapter

See Also:



33
34
35
# File 'lib/multitenancy_tools.rb', line 33

def self.destroy(name, connection = ActiveRecord::Base.connection)
  SchemaDestroyer.new(name, connection).destroy
end

.dump_extensions(file, connection = ActiveRecord::Base.connection, **args) ⇒ Object

Generates a SQL dump of all extensions enabled on the database.

Parameters:

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

    connection adapter

See Also:



73
74
75
# File 'lib/multitenancy_tools.rb', line 73

def self.dump_extensions(file, connection = ActiveRecord::Base.connection, **args)
  ExtensionsDumper.new(connection).dump_to(file, **args)
end

.dump_schema(database, schema, file, **args) ⇒ Object

Generates a SQL dump of the schema. Requires pg_dump.

Parameters:

  • database (String)
  • schema (String)
  • file (String)

See Also:



53
54
55
# File 'lib/multitenancy_tools.rb', line 53

def self.dump_schema(database, schema, file, **args)
  SchemaDumper.new(database, schema).dump_to(file, **args)
end

.dump_table(database, schema, table, file, **args) ⇒ Object

Generates a SQL dump of the table. Requires pg_dump.

Parameters:

  • database (String)
  • schema (String)
  • table (String)
  • file (String)

See Also:



64
65
66
# File 'lib/multitenancy_tools.rb', line 64

def self.dump_table(database, schema, table, file, **args)
  TableDumper.new(database, schema, table).dump_to(file, **args)
end

.using(schema, connection = ActiveRecord::Base.connection) { ... } ⇒ Object

Uses the passed schema as the scope for all queries triggered by the block.

Parameters:

  • schema (String)

    schema name

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

    connection adapter

Yields:

  • The block that must be executed using the schema as scope

See Also:



43
44
45
# File 'lib/multitenancy_tools.rb', line 43

def self.using(schema, connection = ActiveRecord::Base.connection, &block)
  SchemaSwitcher.new(schema, connection).run(&block)
end