class Sequel::SchemaSharding::DatabaseManager

Public Instance Methods

create_databases() click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 9
def create_databases
  config.physical_shard_configs.each_pair do |name, config|
    begin
      # Need to create connection manually with specifying a database in order to create the database
      connection = Sequel.postgres(:user => config['username'],
        :password => config['password'],
        :host => config['host'],
        :port => (config['port'] || 5432))

      Sequel::SchemaSharding.logger.info "Creating #{config['database']}.."

      connection.run("CREATE DATABASE #{config['database']}")
    rescue Sequel::DatabaseError => e
      if e.message.include?('already exists')
        $stderr.puts "#{config['database']} database already exists"
      else
        raise e
      end
    ensure
      connection.disconnect
    end
  end
end
create_shards() click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 57
def create_shards
  config.table_names.each do |table_name|
    SchemaIterator.new.iterate_on(table_name) do |conn, schema_name, table_name|
      Sequel::SchemaSharding.logger.warn "Creating schema #{schema_name}.."

      begin
        conn.run("CREATE SCHEMA #{schema_name}")
      rescue Sequel::DatabaseError => e
        if e.message.include?('already exists')
          $stderr.puts "#{schema_name} schema already exists"
        else
          raise e
        end
      end

      migrator_for(conn, schema_name, table_name).run
    end
  end
end
drop_databases() click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 33
def drop_databases
  connection_manager.disconnect
  config.physical_shard_configs.each_pair do |name, config|
    # Need to create connection manually with specifying a database in order to create the database
    begin
      connection = Sequel.postgres(:user => config['username'],
        :password => config['password'],
        :host => config['host'],
        :port => (config['port'] || 5432))

      Sequel::SchemaSharding.logger.info "Dropping #{config['database']}.."
      connection.run("DROP DATABASE #{config['database']}")
    rescue Sequel::DatabaseError => e
      if e.message.include?('does not exist')
        $stderr.puts "#{config['database']} database doesnt exist"
      else
        raise e
      end
    ensure
      connection.disconnect
    end
  end
end
drop_shards() click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 77
def drop_shards
  config.table_names.each do |table_name|
    SchemaIterator.new.iterate_on(table_name) do |conn, schema_name, table_name|
      Sequel::SchemaSharding.logger.warn "Dropping schema #{schema_name}.."
      conn.run("DROP SCHEMA #{schema_name} CASCADE")
    end
  end
end
migrate(table_name, migration_options = {}) click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 86
def migrate(table_name, migration_options = {})
  SchemaIterator.new.iterate_on(table_name) do |conn, schema_name, table_name|
    Sequel::SchemaSharding.logger.warn "Migrating #{table_name} in schema #{schema_name}.."
    migrator_for(conn, schema_name, table_name, migration_options).run
  end
end
migrate_all(options) click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 93
def migrate_all(options)
  config.table_names.each do |table_name|
    migrate(table_name, options)
  end
end
rollback(table_name, migration_options = {}) click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 99
def rollback(table_name, migration_options = {})
  SchemaIterator.new.iterate_on(table_name) do |conn, schema_name, table_name|
    Sequel::SchemaSharding.logger.warn "Rolling back #{table_name} in schema #{schema_name}.."
    migrator = migrator_for(conn, schema_name, table_name, {direction: :down}.merge(migration_options))
    # :((((((((((((((((((((((
    migrator.instance_variable_set(:@target, migrator.current - 1)
    migrator.instance_variable_set(:@direction, :down)
    migrator.run
  end
end
rollback_all(options) click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 110
def rollback_all(options)
  config.table_names.each do |table_name|
    rollback(table_name, options)
  end
end

Private Instance Methods

config() click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 128
def config
  Sequel::SchemaSharding.config
end
connection_manager() click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 132
def connection_manager
  Sequel::SchemaSharding.connection_manager
end
env() click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 124
def env
  config.env
end
migrator_for(connection, schema, table_name, options = {}) click to toggle source
# File lib/sequel/schema-sharding/database_manager.rb, line 118
def migrator_for(connection, schema, table_name, options = {})
  path = Sequel::SchemaSharding.migration_path + "/#{table_name}"
  connection.migration_current_schema = schema
  Sequel::Migrator.migrator_class(path).new(connection, path, { table: connection.migration_schema_for_table(:schema_info)}.merge(options))
end