switchman 1.10.3 → 1.10.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d61d2718f40a07addd2a2bbdbd6835a067800217
4
- data.tar.gz: 0e8df6c7e4c19b9a1b270d0580521423e3942a27
2
+ SHA256:
3
+ metadata.gz: ec38b2f9f4514dd6e8a395c91d56ec88e110df9f65901b09e9990de84d7b5144
4
+ data.tar.gz: 8b1941a4f918b184917a0f4293b67a0f10a3317187eb98fa6736693201801a9b
5
5
  SHA512:
6
- metadata.gz: c5117529fa38094b92df0d730747b1b69f2c35fddf1295630153f96ab98969d70f1e7bf541d2261f6138a3a47751a68f56f35d1bc233ac2db7ea2f463d139de2
7
- data.tar.gz: 8581bae2c4474151156155d7960c000cae51dfe54d7dde9524f944608c065e267fb7b7aa2e46ea97f279210774301d32509e101d84e0f4cbcab42a7d09a7bd96
6
+ metadata.gz: 54771d8a12ce199e67802b8be9c195b00ed8203a602027b6d0ab8776042341ad6ccf40f33c0cf0ac644c1339bcde9fe6668f442e0a864763f28be0ab8d5ba62d
7
+ data.tar.gz: 53e9832319bd99cdbe0032a6c66c0749f57e9c08e09df5bd7239b872d1a4a29b868603a43d13e7d6f23c6157a14fb7a75650b90c13b7f18e424c3e5e98ad54ac
@@ -1,3 +1,5 @@
1
+ require "securerandom"
2
+
1
3
  module Switchman
2
4
  class DatabaseServer
3
5
  attr_accessor :id
@@ -136,27 +138,14 @@ module Switchman
136
138
  create_schema = options[:schema]
137
139
  # look for another shard associated with this db
138
140
  other_shard = self.shards.where("name<>':memory:' OR name IS NULL").order(:id).first
139
- temp_name = other_shard&.name unless id == ::Rails.env
140
- temp_name = Shard.default.name if id == ::Rails.env
141
141
 
142
142
  case config[:adapter]
143
143
  when 'postgresql'
144
- temp_name ||= 'public'
145
144
  create_statement = lambda { "CREATE SCHEMA #{name}" }
146
145
  password = " PASSWORD #{::ActiveRecord::Base.connection.quote(config[:password])}" if config[:password]
147
146
  when 'sqlite3'
148
- if name
149
- # Try to create a db on-disk even if the only shards for sqlite are in-memory
150
- temp_name = nil if temp_name == ':memory:'
151
- # Put it in the db directory if there are no other sqlite shards
152
- temp_name ||= 'db/dummy'
153
- temp_name = File.join(File.dirname(temp_name), "#{name}.sqlite3")
154
- # If they really asked for :memory:, give them :memory:
155
- temp_name = name if name == ':memory:'
156
- name = temp_name
157
- end
147
+ # no create_statement
158
148
  else
159
- temp_name ||= self.config[:database] % self.config
160
149
  create_statement = lambda { "CREATE DATABASE #{name}" }
161
150
  end
162
151
  sharding_config = Switchman.config
@@ -170,27 +159,35 @@ module Switchman
170
159
  end
171
160
 
172
161
  create_shard = lambda do
173
- shard = Shard.create!(:name => temp_name,
174
- :database_server => self) do |shard|
175
- shard.id = options[:id] if options[:id]
162
+ shard_id = options.fetch(:id) do
163
+ case config[:adapter]
164
+ when 'postgresql'
165
+ id_seq = Shard.connection.quote(Shard.connection.quote_table_name('switchman_shards_id_seq'))
166
+ next_id = Shard.connection.select_value("SELECT nextval(#{id_seq})")
167
+ next_id.to_i
168
+ else
169
+ nil
170
+ end
171
+ end
172
+
173
+ if name.nil?
174
+ base_name = self.config[:database].to_s % self.config
175
+ base_name = $1 if base_name =~ /(?:.*\/)(.+)_shard_\d+(?:\.sqlite3)?$/
176
+ base_name = nil if base_name == ':memory:'
177
+ base_name << '_' if base_name
178
+ base_id = shard_id || SecureRandom.uuid
179
+ name = "#{base_name}shard_#{base_id}"
180
+ if config[:adapter] == 'sqlite3'
181
+ name = File.join('db', "#{name}.sqlite3")
182
+ end
176
183
  end
184
+
185
+ shard = Shard.create!(:id => shard_id,
186
+ :name => name,
187
+ :database_server => self)
188
+
177
189
  begin
178
190
  self.class.creating_new_shard = true
179
- if name.nil?
180
- base_name = self.config[:database] % self.config
181
- base_name = $1 if base_name =~ /(?:.*\/)(.+)_shard_\d+(?:\.sqlite3)?$/
182
- base_name = nil if base_name == ':memory:'
183
- base_name << '_' if base_name
184
- name = "#{base_name}shard_#{shard.id}"
185
- if config[:adapter] == 'sqlite3'
186
- # Try to create a db on-disk even if the only shards for sqlite are in-memory
187
- temp_name = nil if temp_name == ':memory:'
188
- # Put it in the db directory if there are no other sqlite shards
189
- temp_name ||= 'db/dummy'
190
- name = File.join(File.dirname(temp_name), "#{name}.sqlite3")
191
- shard.name = name
192
- end
193
- end
194
191
  shard.activate(*Shard.categories) do
195
192
  ::Shackles.activate(:deploy) do
196
193
  begin
@@ -199,14 +196,11 @@ module Switchman
199
196
  ::ActiveRecord::Base.connection.execute(stmt)
200
197
  end
201
198
  # have to disconnect and reconnect to the correct db
202
- shard.name = name
203
199
  if self.shareable? && other_shard
204
200
  other_shard.activate { ::ActiveRecord::Base.connection }
205
201
  else
206
202
  ::ActiveRecord::Base.connection_pool.current_pool.disconnect!
207
203
  end
208
- else
209
- shard.name = name
210
204
  end
211
205
  old_proc = ::ActiveRecord::Base.connection.raw_connection.set_notice_processor {} if config[:adapter] == 'postgresql'
212
206
  old_verbose = ::ActiveRecord::Migration.verbose
@@ -229,11 +223,10 @@ module Switchman
229
223
  end
230
224
  end
231
225
  end
232
- shard.save!
233
226
  shard
234
227
  rescue
235
228
  shard.destroy
236
- shard.drop_database if shard.name == name rescue nil
229
+ shard.drop_database rescue nil
237
230
  reset_column_information unless create_schema == false rescue nil
238
231
  raise
239
232
  ensure
@@ -1,3 +1,3 @@
1
1
  module Switchman
2
- VERSION = "1.10.3"
2
+ VERSION = "1.10.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.3
4
+ version: 1.10.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-08-24 00:00:00.000000000 Z
13
+ date: 2018-05-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties
@@ -243,7 +243,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
243
243
  version: '0'
244
244
  requirements: []
245
245
  rubyforge_project:
246
- rubygems_version: 2.6.11
246
+ rubygems_version: 2.7.6
247
247
  signing_key:
248
248
  specification_version: 4
249
249
  summary: Rails 4 sharding magic