module MultitenancyTools

Constants

VERSION

Public Class Methods

create(name, sql_file, connection = ActiveRecord::Base.connection) click to toggle source

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

@see SchemaCreator @see SchemaDumper @param name [String] schema name @param sql_file [String] absolute path to the SQL template @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter

# 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) click to toggle source

Drops the schema from the database.

@see SchemaDestroyer @param name [String] schema name @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter

# 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) click to toggle source

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

@see ExtensionsDumper @param file [String] @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter

# 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) click to toggle source

Generates a SQL dump of the schema. Requires pg_dump.

@see SchemaDumper @param database [String] @param schema [String] @param file [String]

# 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) click to toggle source

Generates a SQL dump of the table. Requires pg_dump.

@see TableDumper @param database [String] @param schema [String] @param table [String] @param file [String]

# 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, &block) click to toggle source

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

@see SchemaSwitcher @param schema [String] schema name @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter @yield The block that must be executed using the schema as scope

# File lib/multitenancy_tools.rb, line 43
def self.using(schema, connection = ActiveRecord::Base.connection, &block)
  SchemaSwitcher.new(schema, connection).run(&block)
end