switchman 1.12.2 → 1.12.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d30c5f9189d259f408422987d4b1541fbe4102ff
4
- data.tar.gz: d3e6037589283e074d1574daf373cedf4527c654
3
+ metadata.gz: 1ede324b26c105f71f0d071eee8afa73297430ae
4
+ data.tar.gz: 6dc4b1fd3878baa64ab87827cdab9da20c7693fd
5
5
  SHA512:
6
- metadata.gz: 44d60d03f4fffc373cb5d0d431e5499abbd63d5e84d1045e524a2c6d451196ae6d414bc549e150ee790af5986a76f14be24c7c6ef01bcd3700eab1b18486f420
7
- data.tar.gz: d0e16a2da2cd08f015ea7cb1d65203885597b5e19c149a2f0c7db5c6634873e1500f1062a16ffafdcb8fffaf184f13b692ba79eeedc2b9c2dadf2bf9d549bb2d
6
+ metadata.gz: 8655c6d6342901d739e858f5914aafc2a5b3a875c30a58802128df70630abcb2f09de364a52e43064ad5388368845d8d28c944617a7fd36eaaae7db45cf2e2ae
7
+ data.tar.gz: 5631ec070476582189e1ebb53c1822d50c2c72a5ee0dab301d604a18166f166e143e1b13edfee3fc95df015bf5a2e315e4ebc067d77dd6e8e741ffbd1936290a
@@ -224,7 +224,7 @@ module Switchman
224
224
  end
225
225
 
226
226
  def arel_columns(columns)
227
- columns.map do |field|
227
+ columns.flat_map do |field|
228
228
  if (Symbol === field || String === field) && (klass.has_attribute?(field) || klass.attribute_alias?(field)) && !from_clause.value
229
229
  klass.arel_attribute(field, table)
230
230
  elsif (Symbol === field || String === field) && columns_hash.key?(field.to_s) && !from_value
@@ -233,6 +233,8 @@ module Switchman
233
233
  # the rest of this is pulled from AR - the only change is from quote_table_name to quote_column_name here
234
234
  # otherwise qualified names will add the schema to a column
235
235
  connection.quote_column_name(field.to_s)
236
+ elsif Proc === field
237
+ field.call
236
238
  else
237
239
  field
238
240
  end
@@ -316,7 +318,7 @@ module Switchman
316
318
  predicate.right
317
319
  else
318
320
  local_id = Shard.relative_id_for(predicate.right, current_source_shard, target_shard) || predicate.right
319
- local_id = [] if remove && local_id.is_a?(Fixnum) && local_id > Shard::IDS_PER_SHARD
321
+ local_id = [] if remove && local_id.is_a?(Integer) && local_id > Shard::IDS_PER_SHARD
320
322
  local_id
321
323
  end
322
324
  end
@@ -346,7 +348,7 @@ module Switchman
346
348
  value
347
349
  else
348
350
  local_id = Shard.relative_id_for(current_id, current_shard, target_shard) || current_id
349
- local_id = [] if remove_non_local_ids && local_id.is_a?(Fixnum) && local_id > Shard::IDS_PER_SHARD
351
+ local_id = [] if remove_non_local_ids && local_id.is_a?(Integer) && local_id > Shard::IDS_PER_SHARD
350
352
  if current_id != local_id
351
353
  # make a new bind param
352
354
  ::Arel::Nodes::BindParam.new(query_att.class.new(query_att.name, local_id, query_att.type))
@@ -356,7 +358,7 @@ module Switchman
356
358
  end
357
359
  else
358
360
  local_id = Shard.relative_id_for(value, current_shard, target_shard) || value
359
- local_id = [] if remove_non_local_ids && local_id.is_a?(Fixnum) && local_id > Shard::IDS_PER_SHARD
361
+ local_id = [] if remove_non_local_ids && local_id.is_a?(Integer) && local_id > Shard::IDS_PER_SHARD
360
362
  local_id
361
363
  end
362
364
  end
@@ -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
@@ -232,11 +226,10 @@ module Switchman
232
226
  end
233
227
  end
234
228
  end
235
- shard.save!
236
229
  shard
237
230
  rescue
238
231
  shard.destroy
239
- shard.drop_database if shard.name == name rescue nil
232
+ shard.drop_database rescue nil
240
233
  reset_column_information unless create_schema == false rescue nil
241
234
  raise
242
235
  ensure
@@ -1,3 +1,3 @@
1
1
  module Switchman
2
- VERSION = "1.12.2"
2
+ VERSION = "1.12.3"
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.12.2
4
+ version: 1.12.3
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: 2018-04-25 00:00:00.000000000 Z
13
+ date: 2018-08-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties