rails-sharding 1.2.0 → 1.2.1
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 +4 -4
- data/lib/rails/sharding/core.rb +3 -3
- data/lib/rails/sharding/version.rb +1 -1
- data/lib/tasks/rails-sharding.rake +44 -37
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b567029d770be424e7c1141bd5f483fd5fee49a630ee31248d66af9d57976ab9
|
4
|
+
data.tar.gz: 0aa3dd59efebe3aba5949784fcd342d3595dbe719057db9a68341480d3dae9bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88089653b9b9c99acb012872175d426d5b60aad54b7e65a2d98afb6b128c7b7a1e904cdc62d06db1d68bd4c3d2589d1ed5cc7e6537d9ced97bc2f9e0044ba35b
|
7
|
+
data.tar.gz: 338d7f533b28c46625a0102cc861932a8819bde1084a02b37cf8a7ae1593b948bd35aefc6b0c5355967399453a70232f28bb465aaefdb86aaf2fe965c1d3b4c1
|
data/lib/rails/sharding/core.rb
CHANGED
@@ -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
|
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
|
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|
|
@@ -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
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
39
|
-
|
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
|
-
|
42
|
-
|
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
|
-
|
50
|
+
ActiveRecord::Tasks::DatabaseTasks.drop(configuration)
|
45
51
|
|
46
|
-
|
47
|
-
|
48
|
-
|
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 :
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
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.
|
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-
|
11
|
+
date: 2020-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|