Class: MultitenancyTools::SchemaCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/multitenancy_tools/schema_creator.rb

Overview

SchemaCreator can be used to create a schema on a PostgreSQL database using a SQL file as template. This template should be previously generated by SchemaDumper.

Examples:

creator = MultitenancyTools::SchemaCreator.new('schema name', ActiveRecord::Base.connection)
creator.create_from_file('path/to/file.sql')

Instance Method Summary collapse

Constructor Details

#initialize(schema, connection = ActiveRecord::Base.connection) ⇒ SchemaCreator

Returns a new instance of SchemaCreator.

Parameters:

  • schema (String)

    schema name

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

    connection adapter



12
13
14
15
# File 'lib/multitenancy_tools/schema_creator.rb', line 12

def initialize(schema, connection = ActiveRecord::Base.connection)
  @schema = schema
  @connection = connection
end

Instance Method Details

#create_from_file(file) ⇒ Object

Creates the schema using a SQL file that was generated by MultitenancyTools::SchemaDumper.

Parameters:

  • file (String)

    path to a SQL file



20
21
22
23
24
25
26
27
# File 'lib/multitenancy_tools/schema_creator.rb', line 20

def create_from_file(file)
  quoted_schema_name = @connection.quote_table_name(@schema)

  SchemaSwitcher.new(@schema, @connection).run do
    @connection.create_schema(quoted_schema_name)
    @connection.execute(File.read(file))
  end
end