Class: Sequel::SchemaSharding::DatabaseManager

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

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
76
77
78
79
# File 'lib/sequel/schema-sharding/database_manager.rb', line 57

def create_shards
  config.table_names.each do |table_name|
    config.logical_shard_configs(table_name).each_pair do |shard_number, physical_shard|
      schema_name = connection_manager.schema_for(table_name, env, shard_number)
      Sequel::SchemaSharding.logger.info "Creating schema #{schema_name} on #{physical_shard}.."
      connection = connection_manager.master(physical_shard)

      begin
        connection.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

      connection.run("SET search_path TO #{schema_name}")

      Sequel::Migrator.run(connection, Sequel::SchemaSharding.migration_path + "/#{table_name}", :use_transactions => true)
    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



81
82
83
84
85
86
87
88
89
90
# File 'lib/sequel/schema-sharding/database_manager.rb', line 81

def drop_shards
  config.table_names.each do |table_name|
    config.logical_shard_configs(table_name).each_pair do |shard_number, physical_shard|
      schema_name = connection_manager.schema_for(table_name, env, shard_number)
      Sequel::SchemaSharding.logger.info "Dropping schema #{schema_name} on #{physical_shard}.."
      connection = connection_manager[physical_shard]
      connection.run("DROP SCHEMA #{schema_name} CASCADE")
    end
  end
end