active_shard 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/active_shard.rb +5 -0
- data/lib/active_shard/active_record/connection_handler.rb +4 -0
- data/lib/active_shard/config.rb +5 -1
- data/lib/active_shard/database_cleaner.rb +3 -0
- data/lib/active_shard/database_cleaner/base.rb +26 -0
- data/lib/active_shard/database_cleaner/transaction.rb +23 -0
- data/lib/active_shard/database_cleaner/truncation.rb +26 -0
- data/lib/active_shard/fixtures.rb +134 -0
- data/lib/active_shard/version.rb +1 -1
- metadata +9 -4
data/lib/active_shard.rb
CHANGED
@@ -9,6 +9,7 @@ module ActiveShard
|
|
9
9
|
autoload :ShardCollection
|
10
10
|
autoload :ShardDefinition
|
11
11
|
autoload :ShardLookupHandler
|
12
|
+
autoload :TestFixtures
|
12
13
|
|
13
14
|
class << self
|
14
15
|
|
@@ -174,6 +175,10 @@ module ActiveShard
|
|
174
175
|
config.shards_by_schema( environment, schema_name )
|
175
176
|
end
|
176
177
|
|
178
|
+
def schemas
|
179
|
+
config.schemas( environment )
|
180
|
+
end
|
181
|
+
|
177
182
|
def logger
|
178
183
|
@logger
|
179
184
|
end
|
@@ -81,6 +81,10 @@ module ActiveShard
|
|
81
81
|
|
82
82
|
active_shard_name = shard_lookup.lookup_active_shard( schema_name )
|
83
83
|
|
84
|
+
connection_pool schema_name, active_shard_name
|
85
|
+
end
|
86
|
+
|
87
|
+
def connection_pool( schema_name, active_shard_name = nil )
|
84
88
|
( active_shard_name.nil? ?
|
85
89
|
get_schema_pool_for( schema_name ) :
|
86
90
|
connection_pools[ connection_pool_id( schema_name, active_shard_name ) ] )
|
data/lib/active_shard/config.rb
CHANGED
@@ -16,6 +16,10 @@ module ActiveShard
|
|
16
16
|
shard_collection( environment ).by_schema( schema )
|
17
17
|
end
|
18
18
|
|
19
|
+
def schemas( environment )
|
20
|
+
shard_collection( environment ).schemas
|
21
|
+
end
|
22
|
+
|
19
23
|
def add_shard( environment, shard_definition )
|
20
24
|
shard_collection( environment ).add_shard( shard_definition )
|
21
25
|
end
|
@@ -45,4 +49,4 @@ module ActiveShard
|
|
45
49
|
|
46
50
|
end
|
47
51
|
|
48
|
-
end
|
52
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'database_cleaner/active_record/base'
|
2
|
+
|
3
|
+
module DatabaseCleaner
|
4
|
+
module ActiveShard
|
5
|
+
def self.acailable_strategies
|
6
|
+
%w[truncation transaction]
|
7
|
+
end
|
8
|
+
|
9
|
+
module Base
|
10
|
+
include ::DatabaseCleaner::Generic::Base
|
11
|
+
|
12
|
+
private
|
13
|
+
def connection(schema, shard)
|
14
|
+
::ActiveRecord::Base.connection_handler.connection_pool(schema, shard).connection
|
15
|
+
end
|
16
|
+
|
17
|
+
def for_each_shard
|
18
|
+
::ActiveShard.schemas.each do |schema|
|
19
|
+
::ActiveShard.shards_by_schema(schema).each do |shard|
|
20
|
+
yield connection(schema, shard.name)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'database_cleaner/active_record/transaction'
|
2
|
+
|
3
|
+
module DatabaseCleaner::ActiveShard
|
4
|
+
class Transaction
|
5
|
+
include ::DatabaseCleaner::ActiveShard::Base
|
6
|
+
|
7
|
+
def start
|
8
|
+
for_each_shard do |c|
|
9
|
+
c.increment_open_transactions
|
10
|
+
c.begin_db_transaction
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def clean
|
15
|
+
for_each_shard do |c|
|
16
|
+
if c.open_transactions > 0
|
17
|
+
c.rollback_db_transaction
|
18
|
+
c.decrement_open_transactions
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'database_cleaner/active_record/truncation'
|
2
|
+
|
3
|
+
module DatabaseCleaner::ActiveShard
|
4
|
+
class Truncation
|
5
|
+
include ::DatabaseCleaner::ActiveShard::Base
|
6
|
+
include ::DatabaseCleaner::Generic::Truncation
|
7
|
+
|
8
|
+
def clean
|
9
|
+
for_each_shard do |c|
|
10
|
+
c.disable_referential_integrity do
|
11
|
+
c.truncate_tables(tables_to_truncate(c))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def tables_to_truncate(connection)
|
18
|
+
(@only || connection.tables) - @tables_to_exclude - connection.views
|
19
|
+
end
|
20
|
+
|
21
|
+
# overwritten
|
22
|
+
def migration_storage_name
|
23
|
+
'schema_migrations'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'active_record/fixtures'
|
2
|
+
module ActiveShard
|
3
|
+
class Fixtures
|
4
|
+
class << self
|
5
|
+
def create_fixtures(fixtures_dir, *args)
|
6
|
+
schemas = ActiveShard.schemas.map(&:to_sym) & Dir.entries(fixtures_dir).map(&:to_sym)
|
7
|
+
|
8
|
+
self.by_schema.merge!(
|
9
|
+
schemas.inject({}) do |hash, schema|
|
10
|
+
hash[schema] = Schema.new(schema, File.join(fixtures_dir, schema.to_s), *args)
|
11
|
+
hash
|
12
|
+
end
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def reset!
|
17
|
+
schemas.each(&:reset!)
|
18
|
+
|
19
|
+
@by_schema = {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def by_schema
|
23
|
+
@by_schema ||= {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def schemas
|
27
|
+
by_schema.values
|
28
|
+
end
|
29
|
+
|
30
|
+
def [](key)
|
31
|
+
by_schema[key]
|
32
|
+
end
|
33
|
+
|
34
|
+
def setup_fixture_accessors obj
|
35
|
+
schemas.each do |schema|
|
36
|
+
schema.setup_fixture_accessors obj
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Schema
|
42
|
+
attr_reader :schema
|
43
|
+
|
44
|
+
def initialize(schema, schema_fixtures_dir, *args)
|
45
|
+
@schema = schema
|
46
|
+
|
47
|
+
shards = ActiveShard.shards_by_schema(schema).map{|s| s.name.to_sym} & Dir.entries(schema_fixtures_dir).map(&:to_sym)
|
48
|
+
|
49
|
+
self.by_shard.merge!(
|
50
|
+
shards.inject({}) do |hash, shard|
|
51
|
+
hash[shard] = Shard.new(schema, shard, File.join(schema_fixtures_dir, shard.to_s), *args)
|
52
|
+
hash
|
53
|
+
end
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
def reset!
|
58
|
+
shards.each(&:reset!)
|
59
|
+
end
|
60
|
+
|
61
|
+
def by_shard
|
62
|
+
@by_shard ||= {}
|
63
|
+
end
|
64
|
+
|
65
|
+
def shards
|
66
|
+
by_shard.values
|
67
|
+
end
|
68
|
+
|
69
|
+
def [](key)
|
70
|
+
by_shard[key]
|
71
|
+
end
|
72
|
+
|
73
|
+
def setup_fixture_accessors obj
|
74
|
+
shards.each do |shard|
|
75
|
+
shard.setup_fixture_accessors obj
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class Shard
|
81
|
+
attr_reader :schema
|
82
|
+
attr_reader :shard
|
83
|
+
attr_reader :fixture_names
|
84
|
+
attr_reader :fixtures
|
85
|
+
|
86
|
+
def initialize(schema, shard, shard_fixtures_dir, *args)
|
87
|
+
@schema = schema
|
88
|
+
@shard = shard
|
89
|
+
|
90
|
+
@fixtures_dir = shard_fixtures_dir
|
91
|
+
@fixture_names = Dir[File.join(@fixtures_dir, '*.yml')].map {|f| File.basename(f, '.yml') }
|
92
|
+
|
93
|
+
@fixtures = ::Fixtures.create_fixtures(@fixtures_dir, @fixture_names, *args) do
|
94
|
+
connection
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def connection
|
99
|
+
@connection ||= ::ActiveRecord::Base.connection_handler.connection_pool(@schema,@shard).connection
|
100
|
+
end
|
101
|
+
|
102
|
+
def setup_fixture_accessors obj
|
103
|
+
fixture_names = self.fixture_names
|
104
|
+
loaded_fixtures = ::Fixtures.cache_for_connection(connection)
|
105
|
+
|
106
|
+
fixture_names.each do |table_name|
|
107
|
+
table_name = table_name.to_s.tr('./', '_')
|
108
|
+
|
109
|
+
obj.class.send(:define_method, table_name) do |*fixtures|
|
110
|
+
force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload
|
111
|
+
|
112
|
+
@fixture_cache ||= {}
|
113
|
+
@fixture_cache[table_name] ||= {}
|
114
|
+
|
115
|
+
instances = fixtures.map do |fixture|
|
116
|
+
@fixture_cache[table_name].delete(fixture) if force_reload
|
117
|
+
if loaded_fixtures[table_name][fixture.to_s]
|
118
|
+
@fixture_cache[table_name][fixture] ||= loaded_fixtures[table_name][fixture.to_s].find
|
119
|
+
else
|
120
|
+
raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
instances.size == 1 ? instances.first : instances
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def reset!
|
130
|
+
::Fixtures.reset_cache connection
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
data/lib/active_shard/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_shard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2012-01-10 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
17
|
-
requirement: &
|
17
|
+
requirement: &86568210 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,7 +22,7 @@ dependencies:
|
|
22
22
|
version: 3.0.0
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *86568210
|
26
26
|
description: ActiveShard is a library that implements flexible sharding in ActiveRecord
|
27
27
|
and Rails.
|
28
28
|
email:
|
@@ -35,12 +35,17 @@ files:
|
|
35
35
|
- README.md
|
36
36
|
- lib/active_shard/version.rb
|
37
37
|
- lib/active_shard/shard_definition.rb
|
38
|
+
- lib/active_shard/database_cleaner.rb
|
38
39
|
- lib/active_shard/active_record.rb
|
39
40
|
- lib/active_shard/railtie.rb
|
40
41
|
- lib/active_shard/scope_manager.rb
|
41
42
|
- lib/active_shard/config.rb
|
42
43
|
- lib/active_shard/shard_collection.rb
|
44
|
+
- lib/active_shard/database_cleaner/truncation.rb
|
45
|
+
- lib/active_shard/database_cleaner/transaction.rb
|
46
|
+
- lib/active_shard/database_cleaner/base.rb
|
43
47
|
- lib/active_shard/rails/database.rake
|
48
|
+
- lib/active_shard/fixtures.rb
|
44
49
|
- lib/active_shard/active_record/schema_connection_proxy.rb
|
45
50
|
- lib/active_shard/active_record/sharded_base.rb
|
46
51
|
- lib/active_shard/active_record/connection_specification_adapter.rb
|