rails-sharding 1.2.0 → 1.2.1

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
  SHA256:
3
- metadata.gz: '06952a8d4b285bc681adc59a8653eddc1222432d5b62cd4d97462fab6ff63770'
4
- data.tar.gz: a3d9cf13268beaeda50dbb44961afce736ba5df0793ef0aafe20ba37fe2ab155
3
+ metadata.gz: b567029d770be424e7c1141bd5f483fd5fee49a630ee31248d66af9d57976ab9
4
+ data.tar.gz: 0aa3dd59efebe3aba5949784fcd342d3595dbe719057db9a68341480d3dae9bd
5
5
  SHA512:
6
- metadata.gz: 569ea9f4ae0fe748b696fa10aa1af66b2dca6b82231eeb523187f424ecbeec20d21783e8f200076672bf0389f46966bb55f6be3be4ae270071e532bb91f7e2a7
7
- data.tar.gz: bd63818ba25d2d836c116aca83f1b2267a6288f76bce34d0ff93cdcae055e13bf7d20e51eee1d0c87005df6e187cf00da5a6d0bdefa1a761abfbfa9bede1f0e9
6
+ metadata.gz: 88089653b9b9c99acb012872175d426d5b60aad54b7e65a2d98afb6b128c7b7a1e904cdc62d06db1d68bd4c3d2589d1ed5cc7e6537d9ced97bc2f9e0044ba35b
7
+ data.tar.gz: 338d7f533b28c46625a0102cc861932a8819bde1084a02b37cf8a7ae1593b948bd35aefc6b0c5355967399453a70232f28bb465aaefdb86aaf2fe965c1d3b4c1
@@ -33,7 +33,7 @@ module Rails::Sharding
33
33
  environment_config = @@db_configs[environment]
34
34
  return environment_config if environment_config
35
35
 
36
- raise Errors::ConfigNotFoundError, 'Found no shard configurations for enviroment "' + environment + '" in ' + Config.shards_config_file.to_s + ' file was not found'
36
+ raise Errors::ConfigNotFoundError, 'Found no shard configurations for environment "' + environment + '" in ' + Config.shards_config_file.to_s + ' file was not found'
37
37
  rescue Errno::ENOENT
38
38
  raise Errors::ConfigNotFoundError, Config.shards_config_file.to_s + ' file was not found'
39
39
  end
@@ -57,11 +57,11 @@ module Rails::Sharding
57
57
  # yields a block for each shard in each shard group, with its configurations
58
58
  # shard_group_filter: if passed yields only shards of this group
59
59
  # shard_name_filter: if passed yields only shards with this name
60
- def self.for_each_shard(shard_group_filter=nil, shard_name_filter=nil)
60
+ def self.for_each_shard(environment:Rails.env, shard_group_filter:nil, shard_name_filter:nil)
61
61
  shard_group_filter.to_s if shard_group_filter
62
62
  shard_name_filter.to_s if shard_name_filter
63
63
 
64
- configurations.each do |shard_group, shards_configurations|
64
+ configurations(environment).each do |shard_group, shards_configurations|
65
65
  next if shard_group_filter && shard_group_filter != shard_group.to_s
66
66
 
67
67
  shards_configurations.each do |shard, configuration|
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module Sharding
3
- VERSION = "1.2.0"
3
+ VERSION = "1.2.1"
4
4
  end
5
5
  end
@@ -27,25 +27,32 @@ shards_namespace = namespace :shards do
27
27
 
28
28
  desc "Creates database shards (options: RAILS_ENV=x SHARD_GROUP=x SHARD=x)"
29
29
  task create: [:environment] do
30
- Rails::Sharding.for_each_shard(ENV["SHARD_GROUP"], ENV["SHARD"]) do |shard_group, shard, configuration|
31
- puts "== Creating shard #{shard_group}:#{shard}"
32
- ActiveRecord::Tasks::DatabaseTasks.create(configuration)
30
+ # creates DB for both development and test envs, when in development
31
+ each_current_environment do |environment|
32
+ Rails::Sharding.for_each_shard(environment: environment, shard_group_filter: ENV["SHARD_GROUP"], shard_name_filter: ENV["SHARD"]) do |shard_group, shard, configuration|
33
+ puts "== Creating shard #{shard_group}:#{shard}"
34
+
35
+ ActiveRecord::Tasks::DatabaseTasks.create(configuration)
36
+ end
33
37
  end
34
38
  end
35
39
 
36
40
  desc "Drops database shards (options: RAILS_ENV=x SHARD_GROUP=x SHARD=x)"
37
41
  task drop: [:environment, :check_protected_environments] do
38
- Rails::Sharding.for_each_shard(ENV["SHARD_GROUP"], ENV["SHARD"]) do |shard_group, shard, configuration|
39
- puts "== Dropping shard #{shard_group}:#{shard}"
42
+ # drops DB for both development and test envs, when in development
43
+ each_current_environment do |environment|
44
+ Rails::Sharding.for_each_shard(shard_group_filter: ENV["SHARD_GROUP"], shard_name_filter: ENV["SHARD"]) do |shard_group, shard, configuration|
45
+ puts "== Dropping shard #{shard_group}:#{shard}"
40
46
 
41
- # closes connections with shard before dropping (postgres requires this, mysql does not but there is no harm)
42
- Rails::Sharding::ConnectionHandler.remove_connection(shard_group, shard)
47
+ # closes connections with shard before dropping (postgres requires this, mysql does not but there is no harm)
48
+ Rails::Sharding::ConnectionHandler.remove_connection(shard_group, shard)
43
49
 
44
- ActiveRecord::Tasks::DatabaseTasks.drop(configuration)
50
+ ActiveRecord::Tasks::DatabaseTasks.drop(configuration)
45
51
 
46
- # reestablishes connection (because we removed before). You can do this even if the database does not exist yet,
47
- # you just cannot retrieve the connection yet.
48
- Rails::Sharding::ConnectionHandler.establish_connection(shard_group, shard)
52
+ # reestablishes connection (because we removed before). You can do this even if the database does not exist yet,
53
+ # you just cannot retrieve the connection yet.
54
+ Rails::Sharding::ConnectionHandler.establish_connection(shard_group, shard)
55
+ end
49
56
  end
50
57
  end
51
58
 
@@ -91,7 +98,7 @@ shards_namespace = namespace :shards do
91
98
  task dump: [:_make_activerecord_base_shardable] do
92
99
  require "active_record/schema_dumper"
93
100
 
94
- Rails::Sharding.for_each_shard(ENV["SHARD_GROUP"], ENV["SHARD"]) do |shard_group, shard, _configuration|
101
+ Rails::Sharding.for_each_shard(shard_group_filter: ENV["SHARD_GROUP"], shard_name_filter: ENV["SHARD"]) do |shard_group, shard, _configuration|
95
102
  puts "== Dumping schema of #{shard_group}:#{shard}"
96
103
 
97
104
  schema_filename = shard_schema_path(shard_group, shard)
@@ -216,7 +223,7 @@ shards_namespace = namespace :shards do
216
223
 
217
224
  desc "Retrieves the current schema version number"
218
225
  task version: [:_make_activerecord_base_shardable] do
219
- Rails::Sharding.for_each_shard(ENV["SHARD_GROUP"], ENV["SHARD"]) do |shard_group, shard, _configuration|
226
+ Rails::Sharding.for_each_shard(shard_group_filter: ENV["SHARD_GROUP"], shard_name_filter: ENV["SHARD"]) do |shard_group, shard, _configuration|
220
227
  Rails::Sharding.using_shard(shard_group, shard) do
221
228
  puts "Shard #{shard_group}:#{shard} version: #{ActiveRecord::Migrator.current_version}"
222
229
  end
@@ -273,32 +280,23 @@ shards_namespace = namespace :shards do
273
280
  end
274
281
 
275
282
  desc "Empty the test shards (drops all tables) (options: SHARD_GROUP=x, SHARD=x)"
276
- task :purge => [:_make_activerecord_base_shardable] do
277
- begin
278
- # saves the current RAILS_ENV (we must change it so the environment is set correcly on the metadata table)
279
- initial_rails_env = Rails.env
280
- Rails.env = 'test'
281
-
282
- Rails::Sharding.for_each_shard(ENV["SHARD_GROUP"], ENV["SHARD"]) do |shard_group, shard, configuration|
283
- puts "== Purging test shard #{shard_group}:#{shard}"
284
- begin
285
- # establishes connection with test shard, saving if it was connected before (rails 4.2 doesn't do this, but should)
286
- should_reconnect = Rails::Sharding::ConnectionHandler.connection_pool(shard_group, shard).active_connection?
287
- Rails::Sharding::ConnectionHandler.establish_connection(shard_group, shard, 'test')
288
-
289
- Rails::Sharding.using_shard(shard_group, shard) do
290
- ActiveRecord::Tasks::DatabaseTasks.purge(configuration)
291
- end
292
- ensure
293
- if should_reconnect
294
- # reestablishes connection for RAILS_ENV environment (whatever that is)
295
- Rails::Sharding::ConnectionHandler.establish_connection(shard_group, shard)
296
- end
283
+ task purge: [:_make_activerecord_base_shardable] do
284
+ Rails::Sharding.for_each_shard(environment: 'test', shard_group_filter: ENV["SHARD_GROUP"], shard_name_filter: ENV["SHARD"]) do |shard_group, shard, configuration|
285
+ puts "== Purging test shard #{shard_group}:#{shard}"
286
+ begin
287
+ # establishes connection with test shard, saving if it was connected before (rails 4.2 doesn't do this, but should)
288
+ should_reconnect = Rails::Sharding::ConnectionHandler.connection_pool(shard_group, shard).active_connection?
289
+ Rails::Sharding::ConnectionHandler.establish_connection(shard_group, shard, 'test')
290
+
291
+ Rails::Sharding.using_shard(shard_group, shard) do
292
+ ActiveRecord::Tasks::DatabaseTasks.purge(configuration)
293
+ end
294
+ ensure
295
+ if should_reconnect
296
+ # reestablishes connection for RAILS_ENV environment (whatever that is)
297
+ Rails::Sharding::ConnectionHandler.establish_connection(shard_group, shard)
297
298
  end
298
299
  end
299
- ensure
300
- # restores rails env
301
- Rails.env = initial_rails_env
302
300
  end
303
301
  end
304
302
  end
@@ -325,4 +323,13 @@ shards_namespace = namespace :shards do
325
323
  raise error_message unless version
326
324
  version
327
325
  end
326
+
327
+ def each_current_environment
328
+ environments = [Rails.env]
329
+ environments << "test" if environments == ["development"]
330
+
331
+ environments.each do |env|
332
+ yield env
333
+ end
334
+ end
328
335
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-sharding
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique Gubert
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-30 00:00:00.000000000 Z
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails