Class: Sequel::SchemaSharding::DatabaseManager

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/schema-sharding/database_manager.rb

Defined Under Namespace

Classes: SchemaIterator

Instance Method Summary collapse

Instance Method Details

#create_databasesObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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_shardsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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_databasesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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_shardsObject



77
78
79
80
81
82
83
84
# 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 = {}) ⇒ Object



86
87
88
89
90
91
# 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) ⇒ Object



93
94
95
96
97
# 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 = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object



110
111
112
113
114
# 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