Class: Sequel::SchemaSharding::DatabaseManager
- Inherits:
-
Object
- Object
- Sequel::SchemaSharding::DatabaseManager
- Defined in:
- lib/sequel/schema-sharding/database_manager.rb
Defined Under Namespace
Classes: SchemaIterator
Instance Method Summary collapse
- #create_databases ⇒ Object
- #create_shards ⇒ Object
- #drop_databases ⇒ Object
- #drop_shards ⇒ Object
- #migrate(table_name, migration_options = {}) ⇒ Object
- #migrate_all(options) ⇒ Object
- #rollback(table_name, migration_options = {}) ⇒ Object
- #rollback_all(options) ⇒ Object
Instance Method Details
#create_databases ⇒ Object
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..include?('already exists') $stderr.puts "#{config['database']} database already exists" else raise e end ensure connection.disconnect end end end |
#create_shards ⇒ Object
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..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 ⇒ Object
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..include?('does not exist') $stderr.puts "#{config['database']} database doesnt exist" else raise e end ensure connection.disconnect end end end |
#drop_shards ⇒ Object
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, = {}) 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, ).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() config.table_names.each do |table_name| migrate(table_name, ) 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, = {}) 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()) # :(((((((((((((((((((((( 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() config.table_names.each do |table_name| rollback(table_name, ) end end |