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/schema_creator.rb,
lib/multitenancy_tools/schema_switcher.rb,
lib/multitenancy_tools/functions_dumper.rb,
lib/multitenancy_tools/schema_destroyer.rb

Defined Under Namespace

Classes: DumpCleaner, FunctionsDumper, PgDumpError, SchemaCreator, SchemaDestroyer, SchemaDumper, SchemaSwitcher, TableDumper

Constant Summary collapse

VERSION =
'0.1.10'

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:



20
21
22
# File 'lib/multitenancy_tools.rb', line 20

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:



29
30
31
# File 'lib/multitenancy_tools.rb', line 29

def self.destroy(name, connection = ActiveRecord::Base.connection)
  SchemaDestroyer.new(name, connection).destroy
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:



49
50
51
# File 'lib/multitenancy_tools.rb', line 49

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:



60
61
62
# File 'lib/multitenancy_tools.rb', line 60

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:



39
40
41
# File 'lib/multitenancy_tools.rb', line 39

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