Class: Switchman::DatabaseServer
- Inherits:
-
Object
- Object
- Switchman::DatabaseServer
- Defined in:
- lib/switchman/database_server.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
- .all ⇒ Object
- .create(settings = {}) ⇒ Object
- .find(id_or_all) ⇒ Object
- .server_for_new_shard ⇒ Object
Instance Method Summary collapse
- #cache_store ⇒ Object
- #config(environment = :master) ⇒ Object
- #create_new_shard(options = {}) ⇒ Object
- #destroy ⇒ Object
- #fake? ⇒ Boolean
-
#initialize(id = nil, config = {}) ⇒ DatabaseServer
constructor
A new instance of DatabaseServer.
- #pool_key ⇒ Object
- #primary_shard ⇒ Object
-
#shackle!(environment = :slave) ⇒ Object
locks this db to a specific environment, except for when doing writes (then it falls back to the current value of Shackles.environment).
- #shackles_environment ⇒ Object
- #shard_name(shard) ⇒ Object
- #shards ⇒ Object
- #shareable? ⇒ Boolean
- #unshackle ⇒ Object
- #unshackle! ⇒ Object
Constructor Details
#initialize(id = nil, config = {}) ⇒ DatabaseServer
Returns a new instance of DatabaseServer.
47 48 49 50 51 |
# File 'lib/switchman/database_server.rb', line 47 def initialize(id = nil, config = {}) @id = id @config = config.deep_symbolize_keys @configs = {} end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/switchman/database_server.rb', line 3 def id @id end |
Class Method Details
.all ⇒ Object
6 7 8 |
# File 'lib/switchman/database_server.rb', line 6 def all database_servers.values end |
.create(settings = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/switchman/database_server.rb', line 16 def create(settings = {}) raise "database servers should be set up in database.yml" unless ::Rails.env.test? id = settings[:id] if !id @id ||= 0 @id += 1 id = @id end server = DatabaseServer.new(id.to_s, settings) server.instance_variable_set(:@fake, true) database_servers[server.id] = server end |
.find(id_or_all) ⇒ Object
10 11 12 13 14 |
# File 'lib/switchman/database_server.rb', line 10 def find(id_or_all) return self.all if id_or_all == :all return id_or_all.map { |id| self.database_servers[id || ::Rails.env] }.compact.uniq if id_or_all.is_a?(Array) database_servers[id_or_all || ::Rails.env] end |
.server_for_new_shard ⇒ Object
29 30 31 32 33 |
# File 'lib/switchman/database_server.rb', line 29 def server_for_new_shard servers = all.select { |s| s.config[:open] } return find(nil) if servers.empty? servers[rand(servers.length)] end |
Instance Method Details
#cache_store ⇒ Object
250 251 252 253 254 255 |
# File 'lib/switchman/database_server.rb', line 250 def cache_store unless @cache_store @cache_store = Switchman.config[:cache_map][self.id] || Switchman.config[:cache_map][::Rails.env] end @cache_store end |
#config(environment = :master) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/switchman/database_server.rb', line 62 def config(environment = :master) @configs[environment] ||= begin if @config[environment].is_a?(Array) @config[environment].map do |config| config = @config.merge((config || {}).symbolize_keys) # make sure Shackles doesn't get any brilliant ideas about choosing the first possible server config.delete(environment) config end elsif @config[environment].is_a?(Hash) @config.merge(@config[environment]) else @config end end end |
#create_new_shard(options = {}) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/switchman/database_server.rb', line 130 def create_new_shard( = {}) raise NotImplementedError.new("Cannot create new shards when sharding isn't initialized") unless Shard.default.is_a?(Shard) name = [:name] create_schema = [:schema] # look for another shard associated with this db other_shard = self.shards.where("name<>':memory:' OR name IS NULL").order(:id).first temp_name = other_shard.try(:name) unless id == ::Rails.env temp_name = Shard.default.name if id == ::Rails.env case config[:adapter] when 'postgresql' temp_name ||= 'public' create_statement = lambda { "CREATE SCHEMA #{name}" } password = " PASSWORD #{::ActiveRecord::Base.connection.quote(config[:password])}" if config[:password] when 'sqlite3' if name # Try to create a db on-disk even if the only shards for sqlite are in-memory temp_name = nil if temp_name == ':memory:' # Put it in the db directory if there are no other sqlite shards temp_name ||= 'db/dummy' temp_name = File.join(File.dirname(temp_name), "#{name}.sqlite3") # If they really asked for :memory:, give them :memory: temp_name = name if name == ':memory:' name = temp_name end else temp_name ||= self.config[:database] % self.config create_statement = lambda { "CREATE DATABASE #{name}" } end sharding_config = Switchman.config config_create_statement = sharding_config[config[:adapter]].try(:[], :create_statement) config_create_statement ||= sharding_config[:create_statement] if config_create_statement create_commands = Array(config_create_statement).dup create_statement = lambda { create_commands.map { |statement| statement.gsub('%{name}', name).gsub('%{password}', password || '') } } end create_shard = lambda do shard = Shard.create!(:name => temp_name, :database_server => self) do |shard| shard.id = [:id] if [:id] end begin if name.nil? base_name = self.config[:database] % self.config base_name = $1 if base_name =~ /(?:.*\/)(.+)_shard_\d+(?:\.sqlite3)?$/ base_name = nil if base_name == ':memory:' base_name << '_' if base_name name = "#{base_name}shard_#{shard.id}" if config[:adapter] == 'sqlite3' # Try to create a db on-disk even if the only shards for sqlite are in-memory temp_name = nil if temp_name == ':memory:' # Put it in the db directory if there are no other sqlite shards temp_name ||= 'db/dummy' name = File.join(File.dirname(temp_name), "#{name}.sqlite3") shard.name = name end end shard.activate(*Shard.categories) do ::Shackles.activate(:deploy) do begin if create_statement Array(create_statement.call).each do |stmt| ::ActiveRecord::Base.connection.execute(stmt) end # have to disconnect and reconnect to the correct db shard.name = name if self.shareable? && other_shard other_shard.activate { ::ActiveRecord::Base.connection } else ::ActiveRecord::Base.connection_pool.current_pool.disconnect! end else shard.name = name end old_proc = ::ActiveRecord::Base.connection.raw_connection.set_notice_processor {} if config[:adapter] == 'postgresql' old_verbose = ::ActiveRecord::Migration.verbose ::ActiveRecord::Migration.verbose = false unless create_schema == false reset_column_information migrate = -> { ::ActiveRecord::Migrator.migrate(::ActiveRecord::Migrator.migrations_paths) } if ::ActiveRecord::Base.connection.supports_ddl_transactions? ::ActiveRecord::Base.connection.transaction(requires_new: true, &migrate) else migrate.call end reset_column_information ::ActiveRecord::Base.descendants.reject { |m| m == Shard || !m.table_exists? }.each(&:define_attribute_methods) end ensure ::ActiveRecord::Migration.verbose = old_verbose ::ActiveRecord::Base.connection.raw_connection.set_notice_processor(&old_proc) if old_proc end end end shard.save! shard rescue shard.destroy shard.drop_database if shard.name == name rescue nil reset_column_information unless create_schema == false rescue nil raise end end if Shard.connection.supports_ddl_transactions? && self.shareable? && other_shard Shard.transaction do other_shard.activate do ::ActiveRecord::Base.connection.transaction(&create_shard) end end else create_shard.call end end |
#destroy ⇒ Object
53 54 55 56 |
# File 'lib/switchman/database_server.rb', line 53 def destroy raise "database servers should be set up in database.yml" unless ::Rails.env.test? self.class.send(:database_servers).delete(self.id) if self.id end |
#fake? ⇒ Boolean
58 59 60 |
# File 'lib/switchman/database_server.rb', line 58 def fake? @fake end |
#pool_key ⇒ Object
126 127 128 |
# File 'lib/switchman/database_server.rb', line 126 def pool_key self.id == ::Rails.env ? nil : self.id end |
#primary_shard ⇒ Object
273 274 275 276 277 278 279 280 |
# File 'lib/switchman/database_server.rb', line 273 def primary_shard unless instance_variable_defined?(:@primary_shard) # if sharding isn't fully set up yet, we may not be able to query the shards table @primary_shard = Shard.default if Shard.default.database_server == self @primary_shard ||= shards.where(name: nil).first end @primary_shard end |
#shackle!(environment = :slave) ⇒ Object
locks this db to a specific environment, except for when doing writes (then it falls back to the current value of Shackles.environment)
86 87 88 |
# File 'lib/switchman/database_server.rb', line 86 def shackle!(environment = :slave) @shackles_environment = environment end |
#shackles_environment ⇒ Object
79 80 81 |
# File 'lib/switchman/database_server.rb', line 79 def shackles_environment @shackles_environment || ::Shackles.environment end |
#shard_name(shard) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/switchman/database_server.rb', line 257 def shard_name(shard) if config[:shard_name] config[:shard_name] elsif config[:adapter] == 'postgresql' if shard == :bootstrap # rescue nil because the database may not exist yet; if it doesn't, # it will shortly, and this will be re-invoked ::ActiveRecord::Base.connection.current_schemas.first rescue nil else shard.activate { ::ActiveRecord::Base.connection_pool.default_schema } end else config[:database] end end |
#shards ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/switchman/database_server.rb', line 118 def shards if self.id == ::Rails.env Shard.where("database_server_id IS NULL OR database_server_id=?", self.id) else Shard.where(:database_server_id => self.id) end end |
#shareable? ⇒ Boolean
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/switchman/database_server.rb', line 102 def shareable? @shareable_environment_key ||= [] environment = shackles_environment explicit_user = ::Shackles.global_config[:username] return @shareable if @shareable_environment_key == [environment, explicit_user] @shareable_environment_key = [environment, explicit_user] if explicit_user username = explicit_user else config = self.config(environment) config = config.first if config.is_a?(Array) username = config[:username] end @shareable = self.config[:adapter] != 'sqlite3' && username !~ /%?\{[a-zA-Z0-9_]+\}/ end |
#unshackle ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/switchman/database_server.rb', line 94 def unshackle old_env = @shackles_environment unshackle! yield ensure shackle!(old_env) end |
#unshackle! ⇒ Object
90 91 92 |
# File 'lib/switchman/database_server.rb', line 90 def unshackle! @shackles_environment = nil end |