ar-octopus 0.8.2 → 0.8.3
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 +9 -9
- data/.rspec +1 -1
- data/.rubocop.yml +46 -0
- data/.rubocop_todo.yml +52 -0
- data/.ruby-version +1 -1
- data/.travis.yml +3 -9
- data/Appraisals +4 -0
- data/Rakefile +17 -16
- data/ar-octopus.gemspec +22 -16
- data/gemfiles/rails41.gemfile +7 -0
- data/init.rb +1 -1
- data/lib/ar-octopus.rb +1 -1
- data/lib/octopus.rb +38 -37
- data/lib/octopus/abstract_adapter.rb +0 -2
- data/lib/octopus/association.rb +8 -6
- data/lib/octopus/association_shard_tracking.rb +80 -81
- data/lib/octopus/collection_association.rb +7 -5
- data/lib/octopus/collection_proxy.rb +11 -9
- data/lib/octopus/has_and_belongs_to_many_association.rb +5 -3
- data/lib/octopus/load_balancing.rb +3 -2
- data/lib/octopus/load_balancing/round_robin.rb +12 -8
- data/lib/octopus/migration.rb +117 -108
- data/lib/octopus/model.rb +130 -134
- data/lib/octopus/persistence.rb +1 -1
- data/lib/octopus/proxy.rb +345 -339
- data/lib/octopus/railtie.rb +2 -2
- data/lib/octopus/relation_proxy.rb +6 -1
- data/lib/octopus/scope_proxy.rb +38 -36
- data/lib/octopus/shard_tracking.rb +36 -35
- data/lib/octopus/shard_tracking/attribute.rb +12 -14
- data/lib/octopus/shard_tracking/dynamic.rb +7 -3
- data/lib/octopus/singular_association.rb +5 -3
- data/lib/octopus/slave_group.rb +10 -8
- data/lib/octopus/version.rb +1 -1
- data/rails/init.rb +1 -1
- data/sample_app/autotest/discover.rb +2 -2
- data/sample_app/config/application.rb +1 -1
- data/sample_app/config/boot.rb +1 -1
- data/sample_app/config/environments/test.rb +1 -1
- data/sample_app/config/initializers/session_store.rb +1 -1
- data/sample_app/config/initializers/wrap_parameters.rb +1 -1
- data/sample_app/config/routes.rb +1 -1
- data/sample_app/db/migrate/20100720210335_create_sample_users.rb +2 -2
- data/sample_app/db/schema.rb +10 -10
- data/sample_app/db/seeds.rb +3 -3
- data/sample_app/features/step_definitions/seeds_steps.rb +4 -4
- data/sample_app/features/step_definitions/web_steps.rb +3 -4
- data/sample_app/features/support/env.rb +3 -4
- data/sample_app/features/support/paths.rb +4 -4
- data/sample_app/spec/spec_helper.rb +3 -3
- data/spec/migrations/10_create_users_using_replication.rb +3 -3
- data/spec/migrations/11_add_field_in_all_slaves.rb +3 -3
- data/spec/migrations/12_create_users_using_block.rb +7 -7
- data/spec/migrations/13_create_users_using_block_and_using.rb +4 -4
- data/spec/migrations/14_create_users_on_shards_of_a_group_with_versions.rb +2 -2
- data/spec/migrations/15_create_user_on_shards_of_default_group_with_versions.rb +2 -2
- data/spec/migrations/1_create_users_on_master.rb +3 -3
- data/spec/migrations/2_create_users_on_canada.rb +3 -3
- data/spec/migrations/3_create_users_on_both_shards.rb +3 -3
- data/spec/migrations/4_create_users_on_shards_of_a_group.rb +3 -3
- data/spec/migrations/5_create_users_on_multiples_groups.rb +2 -2
- data/spec/migrations/6_raise_exception_with_invalid_shard_name.rb +3 -3
- data/spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb +3 -3
- data/spec/migrations/8_raise_exception_with_invalid_group_name.rb +3 -3
- data/spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb +4 -4
- data/spec/octopus/association_shard_tracking_spec.rb +413 -417
- data/spec/octopus/collection_proxy_spec.rb +6 -5
- data/spec/octopus/log_subscriber_spec.rb +4 -4
- data/spec/octopus/migration_spec.rb +48 -48
- data/spec/octopus/model_spec.rb +267 -292
- data/spec/octopus/octopus_spec.rb +40 -41
- data/spec/octopus/proxy_spec.rb +124 -124
- data/spec/octopus/relation_proxy_spec.rb +32 -32
- data/spec/octopus/replicated_slave_grouped_spec.rb +23 -23
- data/spec/octopus/replication_spec.rb +61 -66
- data/spec/octopus/scope_proxy_spec.rb +56 -10
- data/spec/octopus/sharded_replicated_slave_grouped_spec.rb +29 -29
- data/spec/octopus/sharded_spec.rb +10 -10
- data/spec/spec_helper.rb +6 -6
- data/spec/support/active_record/connection_adapters/modify_config_adapter.rb +1 -3
- data/spec/support/database_connection.rb +2 -2
- data/spec/support/database_models.rb +16 -17
- data/spec/support/octopus_helper.rb +19 -21
- data/spec/support/query_count.rb +1 -3
- data/spec/support/shared_contexts.rb +3 -3
- data/spec/tasks/octopus.rake_spec.rb +10 -10
- metadata +43 -26
@@ -1,15 +1,16 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Octopus::CollectionProxy do
|
4
|
-
describe
|
4
|
+
describe 'method dispatch' do
|
5
5
|
before :each do
|
6
6
|
@client = Client.using(:canada).create!
|
7
7
|
@client.items << Item.using(:canada).create!
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
11
|
-
@client.items.size.
|
12
|
-
|
10
|
+
it 'computes the size of the collection without loading it' do
|
11
|
+
expect(@client.items.size).to eq(1)
|
12
|
+
|
13
|
+
expect(@client.items.loaded?).to be false
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Octopus::LogSubscriber, :shards => [:canada] do
|
4
4
|
before :each do
|
@@ -12,8 +12,8 @@ describe Octopus::LogSubscriber, :shards => [:canada] do
|
|
12
12
|
ActiveRecord::Base.logger = nil
|
13
13
|
end
|
14
14
|
|
15
|
-
it
|
16
|
-
User.using(:canada).create!(:name =>
|
17
|
-
@out.string.
|
15
|
+
it 'should add to the default logger the shard name the query was sent to' do
|
16
|
+
User.using(:canada).create!(:name => 'test')
|
17
|
+
expect(@out.string).to match(/Shard: canada/)
|
18
18
|
end
|
19
19
|
end
|
@@ -1,112 +1,112 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Octopus::Migration do
|
4
|
-
it
|
4
|
+
it 'should run just in the master shard' do
|
5
5
|
OctopusHelper.migrating_to_version 1 do
|
6
|
-
User.using(:master).find_by_name(
|
7
|
-
User.using(:canada).find_by_name(
|
6
|
+
expect(User.using(:master).find_by_name('Master')).not_to be_nil
|
7
|
+
expect(User.using(:canada).find_by_name('Master')).to be_nil
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
it
|
11
|
+
it 'should run on specific shard' do
|
12
12
|
OctopusHelper.migrating_to_version 2 do
|
13
|
-
User.using(:master).find_by_name(
|
14
|
-
User.using(:canada).find_by_name(
|
13
|
+
expect(User.using(:master).find_by_name('Sharding')).to be_nil
|
14
|
+
expect(User.using(:canada).find_by_name('Sharding')).not_to be_nil
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
18
|
+
it 'should run on specifieds shards' do
|
19
19
|
OctopusHelper.migrating_to_version 3 do
|
20
|
-
User.using(:brazil).find_by_name(
|
21
|
-
User.using(:canada).find_by_name(
|
20
|
+
expect(User.using(:brazil).find_by_name('Both')).not_to be_nil
|
21
|
+
expect(User.using(:canada).find_by_name('Both')).not_to be_nil
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
25
|
+
it 'should run on specified group' do
|
26
26
|
OctopusHelper.migrating_to_version 4 do
|
27
|
-
User.using(:canada).find_by_name(
|
28
|
-
User.using(:brazil).find_by_name(
|
29
|
-
User.using(:russia).find_by_name(
|
27
|
+
expect(User.using(:canada).find_by_name('Group')).not_to be_nil
|
28
|
+
expect(User.using(:brazil).find_by_name('Group')).not_to be_nil
|
29
|
+
expect(User.using(:russia).find_by_name('Group')).not_to be_nil
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
it
|
33
|
+
it 'should run once per shard' do
|
34
34
|
OctopusHelper.migrating_to_version 5 do
|
35
|
-
User.using(:canada).where(:name => 'MultipleGroup').size.
|
36
|
-
User.using(:brazil).where(:name => 'MultipleGroup').size.
|
37
|
-
User.using(:russia).where(:name => 'MultipleGroup').size.
|
35
|
+
expect(User.using(:canada).where(:name => 'MultipleGroup').size).to eq(1)
|
36
|
+
expect(User.using(:brazil).where(:name => 'MultipleGroup').size).to eq(1)
|
37
|
+
expect(User.using(:russia).where(:name => 'MultipleGroup').size).to eq(1)
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
it
|
41
|
+
it 'should create users inside block' do
|
42
42
|
OctopusHelper.migrating_to_version 12 do
|
43
|
-
User.using(:brazil).where(:name => 'UsingBlock1').size.
|
44
|
-
User.using(:brazil).where(:name => 'UsingBlock2').size.
|
45
|
-
User.using(:canada).where(:name => 'UsingCanada').size.
|
46
|
-
User.using(:canada).where(:name => 'UsingCanada2').size.
|
43
|
+
expect(User.using(:brazil).where(:name => 'UsingBlock1').size).to eq(1)
|
44
|
+
expect(User.using(:brazil).where(:name => 'UsingBlock2').size).to eq(1)
|
45
|
+
expect(User.using(:canada).where(:name => 'UsingCanada').size).to eq(1)
|
46
|
+
expect(User.using(:canada).where(:name => 'UsingCanada2').size).to eq(1)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
it
|
50
|
+
it 'should send the query to the correct shard' do
|
51
51
|
OctopusHelper.migrating_to_version 13 do
|
52
|
-
User.using(:brazil).where(:name => 'Brazil').size.
|
53
|
-
User.using(:brazil).where(:name => 'Canada').size.
|
54
|
-
User.using(:canada).where(:name => 'Brazil').size.
|
55
|
-
User.using(:canada).where(:name => 'Canada').size.
|
52
|
+
expect(User.using(:brazil).where(:name => 'Brazil').size).to eq(1)
|
53
|
+
expect(User.using(:brazil).where(:name => 'Canada').size).to eq(0)
|
54
|
+
expect(User.using(:canada).where(:name => 'Brazil').size).to eq(0)
|
55
|
+
expect(User.using(:canada).where(:name => 'Canada').size).to eq(1)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
describe
|
60
|
-
it
|
59
|
+
describe 'when using replication' do
|
60
|
+
it 'should run writes on master when you use replication' do
|
61
61
|
OctopusHelper.using_environment :production_replicated do
|
62
62
|
OctopusHelper.migrating_to_version 10 do
|
63
|
-
Cat.find_by_name(
|
63
|
+
expect(Cat.find_by_name('Replication')).to be_nil
|
64
64
|
end
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
|
-
it
|
68
|
+
it 'should run in all shards, master or another shards' do
|
69
69
|
OctopusHelper.using_environment :production_replicated do
|
70
70
|
OctopusHelper.migrating_to_version 11 do
|
71
|
-
[:slave4, :slave1, :slave2, :slave3].each do |
|
72
|
-
Cat.find_by_name(
|
71
|
+
[:slave4, :slave1, :slave2, :slave3].each do |_sym|
|
72
|
+
expect(Cat.find_by_name('Slaves')).not_to be_nil
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
it
|
79
|
+
it 'should store the migration versions in each shard' do
|
80
80
|
class SchemaMigration < ActiveRecord::Base; end
|
81
81
|
|
82
82
|
OctopusHelper.migrating_to_version 14 do
|
83
|
-
Octopus.using(:canada) { ActiveRecord::Migrator.get_all_versions }.
|
84
|
-
Octopus.using(:brazil) { ActiveRecord::Migrator.get_all_versions }.
|
85
|
-
Octopus.using(:russia) { ActiveRecord::Migrator.get_all_versions }.
|
83
|
+
expect(Octopus.using(:canada) { ActiveRecord::Migrator.get_all_versions }).to include(14)
|
84
|
+
expect(Octopus.using(:brazil) { ActiveRecord::Migrator.get_all_versions }).to include(14)
|
85
|
+
expect(Octopus.using(:russia) { ActiveRecord::Migrator.get_all_versions }).to include(14)
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
|
-
it
|
89
|
+
it 'should run the migrations on shards that are missing them' do
|
90
90
|
class SchemaMigration < ActiveRecord::Base; end
|
91
91
|
|
92
92
|
Octopus.using(:master) { SchemaMigration.create(:version => 14) }
|
93
93
|
Octopus.using(:canada) { SchemaMigration.create(:version => 14) }
|
94
94
|
|
95
95
|
OctopusHelper.migrating_to_version 14 do
|
96
|
-
Octopus.using(:canada) { ActiveRecord::Migrator.get_all_versions }.
|
97
|
-
Octopus.using(:brazil) { ActiveRecord::Migrator.get_all_versions }.
|
98
|
-
Octopus.using(:russia) { ActiveRecord::Migrator.get_all_versions }.
|
96
|
+
expect(Octopus.using(:canada) { ActiveRecord::Migrator.get_all_versions }).to include(14)
|
97
|
+
expect(Octopus.using(:brazil) { ActiveRecord::Migrator.get_all_versions }).to include(14)
|
98
|
+
expect(Octopus.using(:russia) { ActiveRecord::Migrator.get_all_versions }).to include(14)
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
102
|
-
describe
|
103
|
-
it
|
102
|
+
describe 'when using a default_migration_group' do
|
103
|
+
it 'should run migrations on all shards in the default_migration_group' do
|
104
104
|
OctopusHelper.using_environment :octopus_with_default_migration_group do
|
105
105
|
OctopusHelper.migrating_to_version 15 do
|
106
|
-
Octopus.using(:master) { ActiveRecord::Migrator.get_all_versions }.
|
107
|
-
Octopus.using(:canada) { ActiveRecord::Migrator.get_all_versions }.
|
108
|
-
Octopus.using(:brazil) { ActiveRecord::Migrator.get_all_versions }.
|
109
|
-
Octopus.using(:russia) { ActiveRecord::Migrator.get_all_versions }.
|
106
|
+
expect(Octopus.using(:master) { ActiveRecord::Migrator.get_all_versions }).not_to include(15)
|
107
|
+
expect(Octopus.using(:canada) { ActiveRecord::Migrator.get_all_versions }).to include(15)
|
108
|
+
expect(Octopus.using(:brazil) { ActiveRecord::Migrator.get_all_versions }).to include(15)
|
109
|
+
expect(Octopus.using(:russia) { ActiveRecord::Migrator.get_all_versions }).to include(15)
|
110
110
|
end
|
111
111
|
end
|
112
112
|
end
|
data/spec/octopus/model_spec.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Octopus::Model do
|
4
|
-
describe
|
5
|
-
it
|
6
|
-
User.using(:canada).
|
4
|
+
describe '#using method' do
|
5
|
+
it 'should return self after calling the #using method' do
|
6
|
+
expect(User.using(:canada)).to be_a(Octopus::ScopeProxy)
|
7
7
|
end
|
8
8
|
|
9
|
-
it
|
9
|
+
it 'should allow to send a block to the master shard' do
|
10
10
|
Octopus.using(:master) do
|
11
|
-
User.create!(:name =>
|
11
|
+
User.create!(:name => 'Block test')
|
12
12
|
end
|
13
13
|
|
14
|
-
User.using(:master).find_by_name(
|
14
|
+
expect(User.using(:master).find_by_name('Block test')).not_to be_nil
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'should allow to pass a string as the shard name to a AR subclass' do
|
18
18
|
User.using('canada').create!(:name => 'Rafael Pilha')
|
19
19
|
|
20
|
-
User.using('canada').find_by_name('Rafael Pilha').
|
20
|
+
expect(User.using('canada').find_by_name('Rafael Pilha')).not_to be_nil
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'should allow to pass a string as the shard name to a block' do
|
@@ -25,192 +25,191 @@ describe Octopus::Model do
|
|
25
25
|
User.create!(:name => 'Rafael Pilha')
|
26
26
|
end
|
27
27
|
|
28
|
-
User.using('canada').find_by_name('Rafael Pilha').
|
28
|
+
expect(User.using('canada').find_by_name('Rafael Pilha')).not_to be_nil
|
29
29
|
end
|
30
30
|
|
31
|
-
it
|
31
|
+
it 'should allow selecting the shards on scope' do
|
32
32
|
User.using(:canada).create!(:name => 'oi')
|
33
|
-
User.using(:canada).count.
|
34
|
-
User.count.
|
33
|
+
expect(User.using(:canada).count).to eq(1)
|
34
|
+
expect(User.count).to eq(0)
|
35
35
|
end
|
36
36
|
|
37
|
-
it
|
37
|
+
it 'should allow selecting the shard using #new' do
|
38
38
|
u = User.using(:canada).new
|
39
|
-
u.name =
|
39
|
+
u.name = 'Thiago'
|
40
40
|
u.save
|
41
41
|
|
42
|
-
User.using(:canada).count.
|
43
|
-
User.using(:brazil).count.
|
42
|
+
expect(User.using(:canada).count).to eq(1)
|
43
|
+
expect(User.using(:brazil).count).to eq(0)
|
44
44
|
|
45
45
|
u1 = User.new
|
46
|
-
u1.name =
|
46
|
+
u1.name = 'Joaquim'
|
47
47
|
u2 = User.using(:canada).new
|
48
|
-
u2.name =
|
49
|
-
u1.save
|
50
|
-
u2.save
|
48
|
+
u2.name = 'Manuel'
|
49
|
+
u1.save
|
50
|
+
u2.save
|
51
51
|
|
52
|
-
User.using(:canada).all.
|
53
|
-
User.all.
|
52
|
+
expect(User.using(:canada).all).to eq([u, u2])
|
53
|
+
expect(User.all).to eq([u1])
|
54
54
|
end
|
55
55
|
|
56
|
-
describe
|
57
|
-
it
|
56
|
+
describe 'multiple calls to the same scope' do
|
57
|
+
it 'works with nil response' do
|
58
58
|
scope = User.using(:canada)
|
59
|
-
scope.count.
|
60
|
-
scope.first.
|
59
|
+
expect(scope.count).to eq(0)
|
60
|
+
expect(scope.first).to be_nil
|
61
61
|
end
|
62
62
|
|
63
|
-
it
|
63
|
+
it 'works with non-nil response' do
|
64
64
|
user = User.using(:canada).create!(:name => 'oi')
|
65
65
|
scope = User.using(:canada)
|
66
|
-
scope.count.
|
67
|
-
scope.first.
|
66
|
+
expect(scope.count).to eq(1)
|
67
|
+
expect(scope.first).to eq(user)
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
-
it
|
71
|
+
it 'should select the correct shard' do
|
72
72
|
User.using(:canada)
|
73
73
|
User.create!(:name => 'oi')
|
74
|
-
User.count.
|
74
|
+
expect(User.count).to eq(1)
|
75
75
|
end
|
76
76
|
|
77
|
-
it
|
78
|
-
ActiveRecord::Base.connection.current_shard.
|
79
|
-
|
77
|
+
it 'should ensure that the connection will be cleaned' do
|
78
|
+
expect(ActiveRecord::Base.connection.current_shard).to eq(:master)
|
79
|
+
expect do
|
80
80
|
Octopus.using(:canada) do
|
81
|
-
|
81
|
+
fail 'Some Exception'
|
82
82
|
end
|
83
|
-
|
84
|
-
end
|
83
|
+
end.to raise_error
|
85
84
|
|
86
|
-
ActiveRecord::Base.connection.current_shard.
|
85
|
+
expect(ActiveRecord::Base.connection.current_shard).to eq(:master)
|
87
86
|
end
|
88
87
|
|
89
|
-
it
|
88
|
+
it 'should allow creating more than one user' do
|
90
89
|
User.using(:canada).create([{ :name => 'America User 1' }, { :name => 'America User 2' }])
|
91
|
-
User.create!(:name =>
|
92
|
-
User.using(:canada).find_by_name(
|
93
|
-
User.using(:canada).find_by_name(
|
94
|
-
User.using(:master).find_by_name(
|
90
|
+
User.create!(:name => 'Thiago')
|
91
|
+
expect(User.using(:canada).find_by_name('America User 1')).not_to be_nil
|
92
|
+
expect(User.using(:canada).find_by_name('America User 2')).not_to be_nil
|
93
|
+
expect(User.using(:master).find_by_name('Thiago')).not_to be_nil
|
95
94
|
end
|
96
95
|
|
97
|
-
it
|
98
|
-
u = User.using(:sqlite_shard).create!(:name =>
|
99
|
-
User.using(:sqlite_shard).find_by_name(
|
96
|
+
it 'should work when you have a SQLite3 shard' do
|
97
|
+
u = User.using(:sqlite_shard).create!(:name => 'Sqlite3')
|
98
|
+
expect(User.using(:sqlite_shard).find_by_name('Sqlite3')).to eq(u)
|
100
99
|
end
|
101
100
|
|
102
|
-
it
|
103
|
-
User.using(:canada).connection
|
104
|
-
User.connection.current_shard.
|
101
|
+
it 'should clean #current_shard from proxy when using execute' do
|
102
|
+
User.using(:canada).connection.execute('select * from users limit 1;')
|
103
|
+
expect(User.connection.current_shard).to eq(:master)
|
105
104
|
end
|
106
105
|
|
107
|
-
it
|
106
|
+
it 'should allow scoping dynamically' do
|
108
107
|
User.using(:canada).using(:master).using(:canada).create!(:name => 'oi')
|
109
|
-
User.using(:canada).using(:master).count.
|
110
|
-
User.using(:master).using(:canada).count.
|
108
|
+
expect(User.using(:canada).using(:master).count).to eq(0)
|
109
|
+
expect(User.using(:master).using(:canada).count).to eq(1)
|
111
110
|
end
|
112
111
|
|
113
|
-
it
|
114
|
-
@user = User.using(:brazil).create!(:name =>
|
112
|
+
it 'should allow find inside blocks' do
|
113
|
+
@user = User.using(:brazil).create!(:name => 'Thiago')
|
115
114
|
|
116
115
|
Octopus.using(:brazil) do
|
117
|
-
User.first.
|
116
|
+
expect(User.first).to eq(@user)
|
118
117
|
end
|
119
118
|
|
120
|
-
User.using(:brazil).find_by_name(
|
119
|
+
expect(User.using(:brazil).find_by_name('Thiago')).to eq(@user)
|
121
120
|
end
|
122
121
|
|
123
|
-
it
|
124
|
-
User.using(:canada).create!(:name =>
|
125
|
-
User.count.
|
122
|
+
it 'should clean the current_shard after executing the current query' do
|
123
|
+
User.using(:canada).create!(:name => 'oi')
|
124
|
+
expect(User.count).to eq(0)
|
126
125
|
end
|
127
126
|
|
128
|
-
it
|
129
|
-
|
130
|
-
User.using(:alone_shard).count.
|
131
|
-
User.using(:canada).count.
|
132
|
-
User.using(:brazil).count.
|
133
|
-
User.count.
|
127
|
+
it 'should support both groups and alone shards' do
|
128
|
+
_u = User.using(:alone_shard).create!(:name => 'Alone')
|
129
|
+
expect(User.using(:alone_shard).count).to eq(1)
|
130
|
+
expect(User.using(:canada).count).to eq(0)
|
131
|
+
expect(User.using(:brazil).count).to eq(0)
|
132
|
+
expect(User.count).to eq(0)
|
134
133
|
end
|
135
134
|
|
136
|
-
it
|
137
|
-
u = User.using(:brazil).create!(:name =>
|
135
|
+
it 'should work with named scopes' do
|
136
|
+
u = User.using(:brazil).create!(:name => 'Thiago')
|
138
137
|
|
139
|
-
User.thiago.using(:brazil).first.
|
140
|
-
User.using(:brazil).thiago.first.
|
138
|
+
expect(User.thiago.using(:brazil).first).to eq(u)
|
139
|
+
expect(User.using(:brazil).thiago.first).to eq(u)
|
141
140
|
|
142
141
|
Octopus.using(:brazil) do
|
143
|
-
User.thiago.first.
|
142
|
+
expect(User.thiago.first).to eq(u)
|
144
143
|
end
|
145
144
|
end
|
146
145
|
|
147
|
-
describe
|
148
|
-
it
|
149
|
-
u = User.using(:alone_shard).create!(:name =>
|
150
|
-
u.current_shard.
|
146
|
+
describe '#current_shard attribute' do
|
147
|
+
it 'should store the attribute when you create or find an object' do
|
148
|
+
u = User.using(:alone_shard).create!(:name => 'Alone')
|
149
|
+
expect(u.current_shard).to eq(:alone_shard)
|
151
150
|
User.using(:canada).create!(:name => 'oi')
|
152
|
-
u = User.using(:canada).find_by_name(
|
153
|
-
u.current_shard.
|
151
|
+
u = User.using(:canada).find_by_name('oi')
|
152
|
+
expect(u.current_shard).to eq(:canada)
|
154
153
|
end
|
155
154
|
|
156
|
-
it
|
157
|
-
5.times { User.using(:alone_shard).create!(:name =>
|
155
|
+
it 'should store the attribute when you find multiple instances' do
|
156
|
+
5.times { User.using(:alone_shard).create!(:name => 'Alone') }
|
158
157
|
|
159
158
|
User.using(:alone_shard).all.each do |u|
|
160
|
-
u.current_shard.
|
159
|
+
expect(u.current_shard).to eq(:alone_shard)
|
161
160
|
end
|
162
161
|
end
|
163
162
|
|
164
|
-
it
|
165
|
-
alone_user = User.using(:alone_shard).create!(:name =>
|
166
|
-
|
167
|
-
alone_user.name =
|
163
|
+
it 'should works when you find, and after that, alter that object' do
|
164
|
+
alone_user = User.using(:alone_shard).create!(:name => 'Alone')
|
165
|
+
_mstr_user = User.using(:master).create!(:name => 'Master')
|
166
|
+
alone_user.name = 'teste'
|
168
167
|
alone_user.save
|
169
|
-
User.using(:master).
|
170
|
-
User.using(:alone_shard).
|
168
|
+
expect(User.using(:master).first.name).to eq('Master')
|
169
|
+
expect(User.using(:alone_shard).first.name).to eq('teste')
|
171
170
|
end
|
172
171
|
|
173
|
-
it
|
174
|
-
User.using(:alone_shard).create!(:name =>
|
175
|
-
u = User.using(:alone_shard).find_by_name(
|
172
|
+
it 'should work for the reload method' do
|
173
|
+
User.using(:alone_shard).create!(:name => 'Alone')
|
174
|
+
u = User.using(:alone_shard).find_by_name('Alone')
|
176
175
|
u.reload
|
177
|
-
u.name.
|
176
|
+
expect(u.name).to eq('Alone')
|
178
177
|
end
|
179
178
|
|
180
|
-
it
|
181
|
-
User.using(:alone_shard).create!(:name =>
|
182
|
-
u = User.using(:alone_shard).find_by_name(
|
179
|
+
it 'should work passing some arguments to reload method' do
|
180
|
+
User.using(:alone_shard).create!(:name => 'Alone')
|
181
|
+
u = User.using(:alone_shard).find_by_name('Alone')
|
183
182
|
u.reload(:lock => true)
|
184
|
-
u.name.
|
183
|
+
expect(u.name).to eq('Alone')
|
185
184
|
end
|
186
185
|
end
|
187
186
|
|
188
|
-
describe
|
189
|
-
it
|
187
|
+
describe 'passing a block' do
|
188
|
+
it 'should allow queries be executed inside the block, ponting to a specific shard' do
|
190
189
|
Octopus.using(:canada) do
|
191
|
-
User.create(:name =>
|
190
|
+
User.create(:name => 'oi')
|
192
191
|
end
|
193
192
|
|
194
|
-
User.using(:canada).count.
|
195
|
-
User.using(:master).count.
|
196
|
-
User.count.
|
193
|
+
expect(User.using(:canada).count).to eq(1)
|
194
|
+
expect(User.using(:master).count).to eq(0)
|
195
|
+
expect(User.count).to eq(0)
|
197
196
|
end
|
198
197
|
|
199
|
-
it
|
198
|
+
it 'should allow execute queries inside a model' do
|
200
199
|
u = User.new
|
201
|
-
u.awesome_queries
|
202
|
-
User.using(:canada).count.
|
203
|
-
User.count.
|
200
|
+
u.awesome_queries
|
201
|
+
expect(User.using(:canada).count).to eq(1)
|
202
|
+
expect(User.count).to eq(0)
|
204
203
|
end
|
205
204
|
end
|
206
205
|
|
207
|
-
describe
|
206
|
+
describe 'raising errors' do
|
208
207
|
it "should raise a error when you specify a shard that doesn't exist" do
|
209
|
-
|
208
|
+
expect { User.using(:crazy_shard).create!(:name => 'Thiago') }.to raise_error('Nonexistent Shard Name: crazy_shard')
|
210
209
|
end
|
211
210
|
end
|
212
211
|
|
213
|
-
describe
|
212
|
+
describe 'equality' do
|
214
213
|
let(:canada1) do
|
215
214
|
u = User.new
|
216
215
|
u.id = 1
|
@@ -232,353 +231,329 @@ describe Octopus::Model do
|
|
232
231
|
u
|
233
232
|
end
|
234
233
|
|
235
|
-
it
|
236
|
-
u = User.using(:brazil).create(:name =>
|
237
|
-
User.using(:brazil).find_by_name(
|
234
|
+
it 'should work with persisted objects' do
|
235
|
+
u = User.using(:brazil).create(:name => 'Mike')
|
236
|
+
expect(User.using(:brazil).find_by_name('Mike')).to eq(u)
|
238
237
|
end
|
239
238
|
|
240
|
-
it
|
241
|
-
canada1.
|
242
|
-
canada1.
|
239
|
+
it 'should check current_shard when determining equality' do
|
240
|
+
expect(canada1).not_to eq(brazil1)
|
241
|
+
expect(canada1).to eq(canada1_dup)
|
243
242
|
end
|
244
243
|
|
245
|
-
it
|
246
|
-
u = User.using(:brazil).create!(:name =>
|
247
|
-
User.using(:brazil).where(:name =>
|
244
|
+
it 'delegates equality check on scopes' do
|
245
|
+
u = User.using(:brazil).create!(:name => 'Mike')
|
246
|
+
expect(User.using(:brazil).where(:name => 'Mike')).to eq([u])
|
248
247
|
end
|
249
248
|
end
|
250
249
|
end
|
251
250
|
|
252
|
-
describe
|
253
|
-
it
|
254
|
-
|
255
|
-
|
256
|
-
User.using(:alone_shard).arel_engine.connection.adapter_name.should == "Mysql2"
|
257
|
-
end
|
251
|
+
describe 'using a postgresql shard' do
|
252
|
+
it 'should update the Arel Engine' do
|
253
|
+
expect(User.using(:postgresql_shard).arel_engine.connection.adapter_name).to eq('PostgreSQL')
|
254
|
+
expect(User.using(:alone_shard).arel_engine.connection.adapter_name).to eq('Mysql2')
|
258
255
|
end
|
259
256
|
|
260
|
-
it
|
261
|
-
u = User.using(:postgresql_shard).create!(:name =>
|
262
|
-
User.using(:postgresql_shard).
|
263
|
-
User.using(:alone_shard).
|
264
|
-
User.connection_handler.connection_pools[
|
257
|
+
it 'should works with writes and reads' do
|
258
|
+
u = User.using(:postgresql_shard).create!(:name => 'PostgreSQL User')
|
259
|
+
expect(User.using(:postgresql_shard).all).to eq([u])
|
260
|
+
expect(User.using(:alone_shard).all).to eq([])
|
261
|
+
User.connection_handler.connection_pools['ActiveRecord::Base'] = User.connection.instance_variable_get(:@shards)[:master]
|
265
262
|
end
|
266
263
|
end
|
267
264
|
|
268
|
-
describe
|
269
|
-
it
|
270
|
-
CustomConnection.connection.current_database.
|
265
|
+
describe 'AR basic methods' do
|
266
|
+
it 'establish_connection' do
|
267
|
+
expect(CustomConnection.connection.current_database).to eq('octopus_shard_2')
|
271
268
|
end
|
272
269
|
|
273
|
-
it
|
274
|
-
Advert.connection.current_database.
|
275
|
-
Advert.create!(:name =>
|
270
|
+
it 'should not mess with custom connection table names' do
|
271
|
+
expect(Advert.connection.current_database).to eq('octopus_shard_1')
|
272
|
+
Advert.create!(:name => 'Teste')
|
276
273
|
end
|
277
274
|
|
278
|
-
it
|
279
|
-
|
275
|
+
it 'increment' do
|
276
|
+
_ = User.using(:brazil).create!(:name => 'Teste', :number => 10)
|
280
277
|
u = User.using(:brazil).find_by_number(10)
|
281
278
|
u.increment(:number)
|
282
|
-
u.save
|
283
|
-
|
279
|
+
u.save
|
280
|
+
expect(User.using(:brazil).find_by_number(11)).not_to be_nil
|
284
281
|
end
|
285
282
|
|
286
|
-
it
|
287
|
-
|
283
|
+
it 'increment!' do
|
284
|
+
_ = User.using(:brazil).create!(:name => 'Teste', :number => 10)
|
288
285
|
u = User.using(:brazil).find_by_number(10)
|
289
286
|
u.increment!(:number)
|
290
|
-
|
287
|
+
expect(User.using(:brazil).find_by_number(11)).not_to be_nil
|
291
288
|
end
|
292
289
|
|
293
|
-
it
|
294
|
-
|
290
|
+
it 'decrement' do
|
291
|
+
_ = User.using(:brazil).create!(:name => 'Teste', :number => 10)
|
295
292
|
u = User.using(:brazil).find_by_number(10)
|
296
293
|
u.decrement(:number)
|
297
|
-
u.save
|
298
|
-
|
294
|
+
u.save
|
295
|
+
expect(User.using(:brazil).find_by_number(9)).not_to be_nil
|
299
296
|
end
|
300
297
|
|
301
|
-
it
|
302
|
-
|
298
|
+
it 'decrement!' do
|
299
|
+
_ = User.using(:brazil).create!(:name => 'Teste', :number => 10)
|
303
300
|
u = User.using(:brazil).find_by_number(10)
|
304
301
|
u.decrement!(:number)
|
305
|
-
|
302
|
+
expect(User.using(:brazil).find_by_number(9)).not_to be_nil
|
306
303
|
end
|
307
304
|
|
308
|
-
it
|
309
|
-
|
305
|
+
it 'toggle' do
|
306
|
+
_ = User.using(:brazil).create!(:name => 'Teste', :admin => false)
|
310
307
|
u = User.using(:brazil).find_by_name('Teste')
|
311
308
|
u.toggle(:admin)
|
312
|
-
u.save
|
313
|
-
|
309
|
+
u.save
|
310
|
+
expect(User.using(:brazil).find_by_name('Teste').admin).to be true
|
314
311
|
end
|
315
312
|
|
316
|
-
it
|
317
|
-
|
313
|
+
it 'toggle!' do
|
314
|
+
_ = User.using(:brazil).create!(:name => 'Teste', :admin => false)
|
318
315
|
u = User.using(:brazil).find_by_name('Teste')
|
319
316
|
u.toggle!(:admin)
|
320
|
-
|
317
|
+
expect(User.using(:brazil).find_by_name('Teste').admin).to be true
|
321
318
|
end
|
322
319
|
|
323
|
-
it
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
User.using(:brazil).
|
320
|
+
it 'count' do
|
321
|
+
_u = User.using(:brazil).create!(:name => 'User1')
|
322
|
+
_v = User.using(:brazil).create!(:name => 'User2')
|
323
|
+
_w = User.using(:brazil).create!(:name => 'User3')
|
324
|
+
expect(User.using(:brazil).where(:name => 'User2').all.count).to eq(1)
|
328
325
|
end
|
329
326
|
|
330
|
-
it
|
331
|
-
|
332
|
-
|
327
|
+
it 'maximum' do
|
328
|
+
_u = User.using(:brazil).create!(:name => 'Teste', :number => 11)
|
329
|
+
_v = User.using(:master).create!(:name => 'Teste', :number => 12)
|
333
330
|
|
334
|
-
User.using(:brazil).maximum(:number).
|
335
|
-
User.using(:master).maximum(:number).
|
331
|
+
expect(User.using(:brazil).maximum(:number)).to eq(11)
|
332
|
+
expect(User.using(:master).maximum(:number)).to eq(12)
|
336
333
|
end
|
337
334
|
|
338
|
-
describe
|
339
|
-
before { User.using(:brazil).create!(:name =>
|
335
|
+
describe 'any?' do
|
336
|
+
before { User.using(:brazil).create!(:name => 'User1') }
|
340
337
|
|
341
|
-
it
|
342
|
-
scope = User.using(:brazil).where(:name =>
|
343
|
-
scope.any
|
338
|
+
it 'works when true' do
|
339
|
+
scope = User.using(:brazil).where(:name => 'User1')
|
340
|
+
expect(scope.any?).to be true
|
344
341
|
end
|
345
342
|
|
346
|
-
it
|
347
|
-
scope = User.using(:brazil).where(:name =>
|
348
|
-
scope.any
|
343
|
+
it 'works when false' do
|
344
|
+
scope = User.using(:brazil).where(:name => 'User2')
|
345
|
+
expect(scope.any?).to be false
|
349
346
|
end
|
350
347
|
end
|
351
348
|
|
352
|
-
it
|
353
|
-
@user = User.using(:brazil).create!(:name =>
|
349
|
+
it 'exists?' do
|
350
|
+
@user = User.using(:brazil).create!(:name => 'User1')
|
354
351
|
|
355
|
-
User.using(:brazil).where(:name =>
|
356
|
-
User.using(:brazil).where(:name =>
|
352
|
+
expect(User.using(:brazil).where(:name => 'User1').exists?).to be true
|
353
|
+
expect(User.using(:brazil).where(:name => 'User2').exists?).to be false
|
357
354
|
end
|
358
355
|
|
359
|
-
describe
|
360
|
-
it
|
361
|
-
@user = User.using(:brazil).create!(:name =>
|
362
|
-
User.using(:brazil).update_all(
|
356
|
+
describe 'touch' do
|
357
|
+
it 'updates updated_at by default' do
|
358
|
+
@user = User.using(:brazil).create!(:name => 'User1')
|
359
|
+
User.using(:brazil).where(:id => @user.id).update_all(:updated_at => Time.now - 3.months)
|
363
360
|
@user.touch
|
364
|
-
@user.reload.updated_at.in_time_zone('GMT').to_date.
|
361
|
+
expect(@user.reload.updated_at.in_time_zone('GMT').to_date).to eq(Time.now.in_time_zone('GMT').to_date)
|
365
362
|
end
|
366
363
|
|
367
|
-
it
|
368
|
-
@user = User.using(:brazil).create!(:name =>
|
369
|
-
User.using(:brazil).update_all(
|
364
|
+
it 'updates passed in attribute name' do
|
365
|
+
@user = User.using(:brazil).create!(:name => 'User1')
|
366
|
+
User.using(:brazil).where(:id => @user.id).update_all(:created_at => Time.now - 3.months)
|
370
367
|
@user.touch(:created_at)
|
371
|
-
@user.reload.created_at.in_time_zone('GMT').to_date.
|
368
|
+
expect(@user.reload.created_at.in_time_zone('GMT').to_date).to eq(Time.now.in_time_zone('GMT').to_date)
|
372
369
|
end
|
373
370
|
end
|
374
371
|
|
375
|
-
describe
|
376
|
-
before { User.using(:brazil).create!(:name =>
|
372
|
+
describe '#pluck' do
|
373
|
+
before { User.using(:brazil).create!(:name => 'User1') }
|
377
374
|
|
378
|
-
it
|
375
|
+
it 'should works from scope proxy' do
|
379
376
|
names = User.using(:brazil).pluck(:name)
|
380
|
-
names.
|
381
|
-
User.using(:master).pluck(:name).
|
377
|
+
expect(names).to eq(['User1'])
|
378
|
+
expect(User.using(:master).pluck(:name)).to eq([])
|
382
379
|
end
|
383
380
|
end
|
384
381
|
|
385
|
-
it
|
386
|
-
@user = User.using(:brazil).create!(:name =>
|
382
|
+
it 'update_column' do
|
383
|
+
@user = User.using(:brazil).create!(:name => 'User1')
|
387
384
|
@user2 = User.using(:brazil).find(@user.id)
|
388
|
-
@user2.update_column(:name,
|
389
|
-
User.using(:brazil).find_by_name(
|
385
|
+
@user2.update_column(:name, 'Joaquim Shard Brazil')
|
386
|
+
expect(User.using(:brazil).find_by_name('Joaquim Shard Brazil')).not_to be_nil
|
390
387
|
end
|
391
388
|
|
392
|
-
it
|
393
|
-
@user = User.using(:brazil).create!(:name =>
|
389
|
+
it 'update_attributes' do
|
390
|
+
@user = User.using(:brazil).create!(:name => 'User1')
|
394
391
|
@user2 = User.using(:brazil).find(@user.id)
|
395
|
-
@user2.update_attributes(:name =>
|
396
|
-
User.using(:brazil).find_by_name(
|
392
|
+
@user2.update_attributes(:name => 'Joaquim')
|
393
|
+
expect(User.using(:brazil).find_by_name('Joaquim')).not_to be_nil
|
397
394
|
end
|
398
395
|
|
399
|
-
it
|
396
|
+
it 'using update_attributes inside a block' do
|
400
397
|
Octopus.using(:brazil) do
|
401
|
-
@user = User.create!(:name =>
|
398
|
+
@user = User.create!(:name => 'User1')
|
402
399
|
@user2 = User.find(@user.id)
|
403
|
-
@user2.update_attributes(:name =>
|
400
|
+
@user2.update_attributes(:name => 'Joaquim')
|
404
401
|
end
|
405
402
|
|
406
|
-
User.find_by_name(
|
407
|
-
User.using(:brazil).find_by_name(
|
403
|
+
expect(User.find_by_name('Joaquim')).to be_nil
|
404
|
+
expect(User.using(:brazil).find_by_name('Joaquim')).not_to be_nil
|
408
405
|
end
|
409
406
|
|
410
|
-
it
|
411
|
-
@user = User.using(:brazil).create!(:name =>
|
407
|
+
it 'update_attribute' do
|
408
|
+
@user = User.using(:brazil).create!(:name => 'User1')
|
412
409
|
@user2 = User.using(:brazil).find(@user.id)
|
413
|
-
@user2.update_attribute(:name,
|
414
|
-
User.using(:brazil).find_by_name(
|
410
|
+
@user2.update_attribute(:name, 'Joaquim')
|
411
|
+
expect(User.using(:brazil).find_by_name('Joaquim')).not_to be_nil
|
415
412
|
end
|
416
413
|
|
417
|
-
it
|
418
|
-
ActiveRecord::Base.include_root_in_json = false
|
414
|
+
it 'as_json' do
|
415
|
+
ActiveRecord::Base.include_root_in_json = false
|
419
416
|
|
420
417
|
Octopus.using(:brazil) do
|
421
|
-
User.create!(:name =>
|
418
|
+
User.create!(:name => 'User1')
|
422
419
|
end
|
423
420
|
|
424
|
-
user = User.using(:brazil).where(:name =>
|
425
|
-
user.as_json(:except => [:created_at, :updated_at, :id]).
|
421
|
+
user = User.using(:brazil).where(:name => 'User1').first
|
422
|
+
expect(user.as_json(:except => [:created_at, :updated_at, :id])).to eq('admin' => nil, 'name' => 'User1', 'number' => nil)
|
426
423
|
end
|
427
424
|
|
428
|
-
it
|
429
|
-
|
425
|
+
it 'transaction' do
|
426
|
+
_u = User.create!(:name => 'Thiago')
|
430
427
|
|
431
|
-
User.using(:brazil).count.
|
432
|
-
User.using(:master).count.
|
428
|
+
expect(User.using(:brazil).count).to eq(0)
|
429
|
+
expect(User.using(:master).count).to eq(1)
|
433
430
|
|
434
431
|
User.using(:brazil).transaction do
|
435
|
-
User.find_by_name(
|
436
|
-
User.create!(:name =>
|
432
|
+
expect(User.find_by_name('Thiago')).to be_nil
|
433
|
+
User.create!(:name => 'Brazil')
|
437
434
|
end
|
438
435
|
|
439
|
-
User.using(:brazil).count.
|
440
|
-
User.using(:master).count.
|
436
|
+
expect(User.using(:brazil).count).to eq(1)
|
437
|
+
expect(User.using(:master).count).to eq(1)
|
441
438
|
end
|
442
439
|
|
443
|
-
describe
|
440
|
+
describe 'deleting a record' do
|
444
441
|
before(:each) do
|
445
|
-
@user = User.using(:brazil).create!(:name =>
|
442
|
+
@user = User.using(:brazil).create!(:name => 'User1')
|
446
443
|
@user2 = User.using(:brazil).find(@user.id)
|
447
444
|
end
|
448
445
|
|
449
|
-
it
|
446
|
+
it 'delete' do
|
450
447
|
@user2.delete
|
451
|
-
|
448
|
+
expect { User.using(:brazil).find(@user2.id) }.to raise_error(ActiveRecord::RecordNotFound)
|
452
449
|
end
|
453
450
|
|
454
451
|
it "delete within block shouldn't lose shard" do
|
455
452
|
Octopus.using(:brazil) do
|
456
453
|
@user2.delete
|
457
|
-
@user3 = User.create(:name =>
|
454
|
+
@user3 = User.create(:name => 'User3')
|
458
455
|
|
459
|
-
User.connection.current_shard.
|
460
|
-
User.find(@user3.id).
|
456
|
+
expect(User.connection.current_shard).to eq(:brazil)
|
457
|
+
expect(User.find(@user3.id)).to eq(@user3)
|
461
458
|
end
|
462
459
|
end
|
463
460
|
|
464
|
-
it
|
461
|
+
it 'destroy' do
|
465
462
|
@user2.destroy
|
466
|
-
|
463
|
+
expect { User.using(:brazil).find(@user2.id) }.to raise_error(ActiveRecord::RecordNotFound)
|
467
464
|
end
|
468
465
|
|
469
466
|
it "destroy within block shouldn't lose shard" do
|
470
467
|
Octopus.using(:brazil) do
|
471
468
|
@user2.destroy
|
472
|
-
@user3 = User.create(:name =>
|
469
|
+
@user3 = User.create(:name => 'User3')
|
473
470
|
|
474
|
-
User.connection.current_shard.
|
475
|
-
User.find(@user3.id).
|
471
|
+
expect(User.connection.current_shard).to eq(:brazil)
|
472
|
+
expect(User.find(@user3.id)).to eq(@user3)
|
476
473
|
end
|
477
474
|
end
|
478
475
|
end
|
479
476
|
end
|
480
477
|
|
481
|
-
describe
|
478
|
+
describe 'when using set_table_name' do
|
482
479
|
it 'should work correctly' do
|
483
|
-
Bacon.using(:brazil).create!(:name =>
|
480
|
+
Bacon.using(:brazil).create!(:name => 'YUMMMYYYY')
|
484
481
|
end
|
485
482
|
|
486
483
|
it 'should work correctly with a block' do
|
487
|
-
Cheese.using(:brazil).create!(:name =>
|
484
|
+
Cheese.using(:brazil).create!(:name => 'YUMMMYYYY')
|
488
485
|
end
|
489
486
|
end
|
490
487
|
|
491
|
-
describe
|
488
|
+
describe 'when using table_name=' do
|
492
489
|
it 'should work correctly' do
|
493
|
-
Ham.using(:brazil).create!(:name =>
|
490
|
+
Ham.using(:brazil).create!(:name => 'YUMMMYYYY')
|
494
491
|
end
|
495
492
|
end
|
496
493
|
|
497
|
-
describe
|
494
|
+
describe 'when using a environment with a single adapter' do
|
498
495
|
it 'should not clean the table name' do
|
499
496
|
OctopusHelper.using_environment :production_fully_replicated do
|
500
|
-
Keyboard.
|
501
|
-
Keyboard.using(:master).create!(:name =>
|
497
|
+
expect(Keyboard).not_to receive(:reset_table_name)
|
498
|
+
Keyboard.using(:master).create!(:name => 'Master Cat')
|
502
499
|
end
|
503
500
|
end
|
504
501
|
end
|
505
502
|
|
506
|
-
describe
|
503
|
+
describe 'when you have joins/include' do
|
507
504
|
before(:each) do
|
508
|
-
@client1 = Client.using(:brazil).create(:name =>
|
505
|
+
@client1 = Client.using(:brazil).create(:name => 'Thiago')
|
509
506
|
|
510
507
|
Octopus.using(:canada) do
|
511
|
-
@client2 = Client.create(:name =>
|
512
|
-
@client3 = Client.create(:name =>
|
513
|
-
@item1 = Item.create(:client => @client2, :name =>
|
514
|
-
@item2 = Item.create(:client => @client2, :name =>
|
515
|
-
@item3 = Item.create(:client => @client3, :name =>
|
516
|
-
@part1 = Part.create(:item => @item1, :name =>
|
517
|
-
@part2 = Part.create(:item => @item1, :name =>
|
518
|
-
@part3 = Part.create(:item => @item2, :name =>
|
508
|
+
@client2 = Client.create(:name => 'Mike')
|
509
|
+
@client3 = Client.create(:name => 'Joao')
|
510
|
+
@item1 = Item.create(:client => @client2, :name => 'Item 1')
|
511
|
+
@item2 = Item.create(:client => @client2, :name => 'Item 2')
|
512
|
+
@item3 = Item.create(:client => @client3, :name => 'Item 3')
|
513
|
+
@part1 = Part.create(:item => @item1, :name => 'Part 1')
|
514
|
+
@part2 = Part.create(:item => @item1, :name => 'Part 2')
|
515
|
+
@part3 = Part.create(:item => @item2, :name => 'Part 3')
|
519
516
|
end
|
520
517
|
|
521
|
-
@item4 = Item.using(:brazil).create(:client => @client1, :name =>
|
522
|
-
end
|
523
|
-
|
524
|
-
it "should work with the rails 2.x syntax" do
|
525
|
-
items = Item.using(:canada).find(:all, :joins => :client, :conditions => { :clients => { :id => @client2.id } })
|
526
|
-
items.should == [@item1, @item2]
|
518
|
+
@item4 = Item.using(:brazil).create(:client => @client1, :name => 'Item 4')
|
527
519
|
end
|
528
520
|
|
529
|
-
it
|
521
|
+
it 'should work using the rails 3.x syntax' do
|
530
522
|
items = Item.using(:canada).joins(:client).where("clients.id = #{@client2.id}").all
|
531
|
-
items.
|
532
|
-
end
|
533
|
-
|
534
|
-
it "should work for include also, rails 2.x syntax" do
|
535
|
-
items = Item.using(:canada).find(:all, :include => :client, :conditions => { :clients => { :id => @client2.id } })
|
536
|
-
items.should == [@item1, @item2]
|
537
|
-
end
|
538
|
-
|
539
|
-
it "should work for include also, rails 3.x syntax" do
|
540
|
-
items = Item.using(:canada).includes(:client).where("clients.id = #{@client2.id}").all
|
541
|
-
items.should == [@item1, @item2]
|
542
|
-
end
|
543
|
-
|
544
|
-
it "should work for multiple includes, with rails 2.x syntax" do
|
545
|
-
parts = Part.using(:canada).find(:all, :include => {:item => :client}, :conditions => {:clients => { :id => @client2.id}})
|
546
|
-
parts.should == [@part1, @part2, @part3]
|
547
|
-
parts.first.item.client.should == @client2
|
523
|
+
expect(items).to eq([@item1, @item2])
|
548
524
|
end
|
549
525
|
|
550
|
-
it
|
551
|
-
|
552
|
-
|
553
|
-
parts.first.item.client.should == @client2
|
526
|
+
it 'should work for include also, rails 3.x syntax' do
|
527
|
+
items = Item.using(:canada).includes(:client).where(:clients => { :id => @client2.id }).all
|
528
|
+
expect(items).to eq([@item1, @item2])
|
554
529
|
end
|
555
530
|
end
|
556
531
|
|
557
|
-
describe
|
558
|
-
it
|
559
|
-
@key = Keyboard.create!(:name =>
|
560
|
-
|
561
|
-
|
532
|
+
describe 'ActiveRecord::Base Validations' do
|
533
|
+
it 'should work correctly when using validations' do
|
534
|
+
@key = Keyboard.create!(:name => 'Key')
|
535
|
+
expect { Keyboard.using(:brazil).create!(:name => 'Key') }.not_to raise_error
|
536
|
+
expect { Keyboard.create!(:name => 'Key') }.to raise_error
|
562
537
|
end
|
563
538
|
|
564
|
-
it
|
565
|
-
@key = Keyboard.using(:brazil).create!(:name =>
|
566
|
-
|
567
|
-
|
539
|
+
it 'should work correctly when using validations with using syntax' do
|
540
|
+
@key = Keyboard.using(:brazil).create!(:name => 'Key')
|
541
|
+
expect { Keyboard.create!(:name => 'Key') }.not_to raise_error
|
542
|
+
expect { Keyboard.using(:brazil).create!(:name => 'Key') }.to raise_error
|
568
543
|
end
|
569
544
|
end
|
570
545
|
|
571
|
-
describe
|
572
|
-
it
|
546
|
+
describe '#replicated_model method' do
|
547
|
+
it 'should be replicated' do
|
573
548
|
OctopusHelper.using_environment :production_replicated do
|
574
|
-
ActiveRecord::Base.connection_proxy.instance_variable_get(:@replicated).
|
549
|
+
expect(ActiveRecord::Base.connection_proxy.instance_variable_get(:@replicated)).to be true
|
575
550
|
end
|
576
551
|
end
|
577
552
|
|
578
|
-
it
|
553
|
+
it 'should mark the Cat model as replicated' do
|
579
554
|
OctopusHelper.using_environment :production_replicated do
|
580
|
-
User.replicated.
|
581
|
-
Cat.replicated.
|
555
|
+
expect(User.replicated).to be_falsey
|
556
|
+
expect(Cat.replicated).to be true
|
582
557
|
end
|
583
558
|
end
|
584
559
|
end
|