active_record_shards 3.3.3 → 3.3.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 +4 -4
- data/README.md +4 -4
- data/lib/active_record_shards.rb +1 -0
- data/lib/active_record_shards/tasks.rb +36 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cca0634041dcb99ffe8ace1ac4376f10d2f716e6
|
4
|
+
data.tar.gz: e4620fa793c96ba1c94808ac96bf00fb55304cec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c9d8698dd41d4cba95a361bf828f130165a84e1ae3f513b48a22729f6c9cda49324798ab2ba32a547db8363682e33bc835b791a390621790b6894a4eb057b24
|
7
|
+
data.tar.gz: da9be40c70b4840c099d059f8d10b6fe3b08ae74490b1735a0bcc15e7aa007727f39758a89890e9054993e1e2df756229be9b77f0c3095ecc21605abcdbb3280
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
ActiveRecord Shards is an extension for ActiveRecord that provides support for sharded database and slaves. Basically it is just a nice way to
|
4
4
|
switch between database connections. We've made the implementation very small, and have tried not to reinvent any wheels already present in ActiveRecord.
|
5
5
|
|
6
|
-
ActiveRecord Shards has used and tested on Rails 3.0.x, 3.2.x, and 4.1.x and has in some form or another been used in production on a large rails app for
|
6
|
+
ActiveRecord Shards has used and tested on Rails 3.0.x, 3.2.x, 4.0.x, and 4.1.x and has in some form or another been used in production on a large rails app for
|
7
7
|
more than a year.
|
8
8
|
|
9
9
|
## Installation
|
@@ -43,7 +43,7 @@ basically connections inherit configuration from the parent configuration file.
|
|
43
43
|
## Usage
|
44
44
|
|
45
45
|
Normally you have some models that live on a shared database, and you might need to query this data in order to know what shard to switch to.
|
46
|
-
All the
|
46
|
+
All the models that live on the shared database must be marked as not\_sharded:
|
47
47
|
|
48
48
|
class Account < ActiveRecord::Base
|
49
49
|
not_sharded
|
@@ -55,7 +55,7 @@ All the model that live on the shared database must be marked as not\_sharded:
|
|
55
55
|
belongs_to :account
|
56
56
|
end
|
57
57
|
|
58
|
-
So in this setup the accounts live on the shared database, but the projects are sharded. If accounts
|
58
|
+
So in this setup the accounts live on the shared database, but the projects are sharded. If accounts have a shard\_id column, you could lookup the account
|
59
59
|
in a rack middleware and switch to the right shard:
|
60
60
|
|
61
61
|
class AccountMiddleware
|
@@ -80,7 +80,7 @@ in a rack middleware and switch to the right shard:
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
-
|
83
|
+
You can switch to the slave databases at any point by wrapping your code in an on\_slave block:
|
84
84
|
|
85
85
|
ActiveRecord::Base.on_slave do
|
86
86
|
Account.find_by_big_expensive_query
|
data/lib/active_record_shards.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
require 'active_record_shards'
|
2
2
|
|
3
|
-
%w[db:drop db:create db:abort_if_pending_migrations db:reset].each do |name|
|
3
|
+
%w[db:drop db:create db:abort_if_pending_migrations db:reset db:test:purge].each do |name|
|
4
4
|
Rake::Task[name].clear
|
5
5
|
end
|
6
6
|
|
7
7
|
namespace :db do
|
8
8
|
desc 'Drops the database for the current RAILS_ENV including shards'
|
9
9
|
task :drop => :load_config do
|
10
|
-
env_name = defined?(Rails.env) ? Rails.env : RAILS_ENV || 'development'
|
11
10
|
ActiveRecord::Base.configurations.each do |key, conf|
|
12
|
-
if key.starts_with?(
|
11
|
+
if key.starts_with?(ActiveRecordShards.rails_env) && !key.ends_with?("_slave")
|
13
12
|
begin
|
14
13
|
if ActiveRecord::VERSION::MAJOR >= 4
|
15
|
-
|
14
|
+
ActiveRecordShards::Tasks.root_connection(conf).drop_database(conf['database'])
|
16
15
|
else
|
17
16
|
drop_database(conf)
|
18
17
|
end
|
@@ -30,17 +29,24 @@ namespace :db do
|
|
30
29
|
|
31
30
|
desc "Create the database defined in config/database.yml for the current RAILS_ENV including shards"
|
32
31
|
task :create => :load_config do
|
33
|
-
env_name = defined?(Rails.env) ? Rails.env : RAILS_ENV || 'development'
|
34
32
|
ActiveRecord::Base.configurations.each do |key, conf|
|
35
|
-
if key.starts_with?(
|
33
|
+
if key.starts_with?(ActiveRecordShards.rails_env) && !key.ends_with?("_slave")
|
36
34
|
if ActiveRecord::VERSION::MAJOR >= 4
|
37
|
-
|
35
|
+
begin
|
36
|
+
ActiveRecordShards::Tasks.root_connection(conf).create_database(conf['database'])
|
37
|
+
rescue ActiveRecord::StatementInvalid => ex
|
38
|
+
if ex.message.include?('database exists')
|
39
|
+
puts "#{conf['database']} already exists"
|
40
|
+
else
|
41
|
+
raise ex
|
42
|
+
end
|
43
|
+
end
|
38
44
|
else
|
39
45
|
create_database(conf)
|
40
46
|
end
|
41
47
|
end
|
42
48
|
end
|
43
|
-
ActiveRecord::Base.establish_connection(
|
49
|
+
ActiveRecord::Base.establish_connection(ActiveRecordShards.rails_env)
|
44
50
|
end
|
45
51
|
|
46
52
|
desc "Raises an error if there are pending migrations"
|
@@ -61,4 +67,26 @@ namespace :db do
|
|
61
67
|
end
|
62
68
|
end
|
63
69
|
end
|
70
|
+
|
71
|
+
namespace :test do
|
72
|
+
desc 'Purges the test databases by dropping and creating'
|
73
|
+
task :purge do
|
74
|
+
begin
|
75
|
+
saved_env = Rails.env
|
76
|
+
Rails.env = 'test'
|
77
|
+
Rake::Task['db:drop'].invoke
|
78
|
+
Rake::Task['db:create'].invoke
|
79
|
+
ensure
|
80
|
+
Rails.env = saved_env
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
module ActiveRecordShards
|
87
|
+
module Tasks
|
88
|
+
def self.root_connection(conf)
|
89
|
+
ActiveRecord::Base.send("#{conf['adapter']}_connection", conf.merge('database' => nil))
|
90
|
+
end
|
91
|
+
end
|
64
92
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_shards
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.3.
|
4
|
+
version: 3.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mick Staugaard
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2015-
|
13
|
+
date: 2015-02-05 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|