Class: MultitenancyTools::SchemaMigrator

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

Overview

SchemaMigrator is a wrapper around ActiveRecord::Migrator that executes all migrations inside a PostgreSQL schema.

Unfortunately this can only execute migrations using the global connection (the connection returned by ActiveRecord::Base.connection).

Examples:

migrator = MultitenancyTools::SchemaMigrator.new('my_schema', 'path/to/migrations')
migrator.migrate

Instance Method Summary collapse

Constructor Details

#initialize(schema, migrations_path) ⇒ SchemaMigrator

Returns a new instance of SchemaMigrator.

Parameters:

  • schema (String)

    schema name

  • migrations_path (String)

    path to the migrations files



16
17
18
19
# File 'lib/multitenancy_tools/schema_migrator.rb', line 16

def initialize(schema, migrations_path)
  @schema = schema
  @migrations_path = migrations_path
end

Instance Method Details

#migrateObject

Executes all migrations.



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

def migrate
  run do
    ActiveRecord::Migrator.migrate(@migrations_path, nil)
  end
end

#rollbackObject

Undo the latest migration.



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

def rollback
  run do
    ActiveRecord::Migrator.rollback(@migrations_path)
  end
end