Skip to content
This repository was archived by the owner on Nov 19, 2025. It is now read-only.

Commit 7d8b24a

Browse files
authored
Merge pull request #127 from zendesk/bquorning/separate-schemas-for-separate-databases
Tests: Use separate schemas for sharded and unsharded
2 parents a50846d + 9a1b400 commit 7d8b24a

File tree

6 files changed

+56
-27
lines changed

6 files changed

+56
-27
lines changed

test/connection_switching_test.rb

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -617,16 +617,27 @@ class ShardedModel < ActiveRecord::Base
617617
end
618618

619619
it "supports implicit joins" do
620-
accounts = Account.includes(:tickets)
621-
accounts = accounts.references(:tickets) if ActiveRecord::VERSION::MAJOR >= 4
622-
assert_equal ["slave_name", "slave_name2"], accounts.order('tickets.id').map(&:name).sort
620+
accounts = Account.includes(:account_things)
621+
accounts = accounts.references(:account_things) if ActiveRecord::VERSION::MAJOR >= 4
622+
assert_equal ["slave_name", "slave_name2"], accounts.order('account_things.id').map(&:name).sort
623623
end
624624

625625
it "supports joins" do
626-
accounts = Account.joins('LEFT OUTER JOIN tickets ON tickets.account_id = accounts.id').map(&:name).sort
626+
accounts = Account.joins('LEFT OUTER JOIN account_things ON account_things.account_id = accounts.id').map(&:name).sort
627627
assert_equal ["slave_name", "slave_name2"], accounts
628628
end
629629

630+
it "does not support implicit joins between an unsharded and a sharded table" do
631+
accounts = Account.includes(:tickets).order('tickets.id')
632+
accounts = accounts.references(:tickets) if ActiveRecord::VERSION::MAJOR >= 4
633+
assert_raises(ActiveRecord::StatementInvalid) { accounts.first }
634+
end
635+
636+
it "does not support explicit joins between an unsharded and a sharded table" do
637+
accounts = Account.joins('LEFT OUTER JOIN tickets ON tickets.account_id = accounts.id')
638+
assert_raises(ActiveRecord::StatementInvalid) { accounts.first }
639+
end
640+
630641
after do
631642
Account.on_slave_by_default = false
632643
Person.on_slave_by_default = false
@@ -643,10 +654,10 @@ class ShardedModel < ActiveRecord::Base
643654
end
644655

645656
it "work on association collections" do
646-
begin
647-
assert_using_master_db
648-
account = Account.create!
657+
assert_using_master_db
658+
account = Account.create!
649659

660+
ActiveRecord::Base.on_shard(0) do
650661
account.tickets.create! title: 'master ticket'
651662

652663
Ticket.on_slave do
@@ -655,11 +666,6 @@ class ShardedModel < ActiveRecord::Base
655666

656667
assert_equal "master ticket", account.tickets.first.title
657668
assert_equal "slave ticket", account.tickets.on_slave.first.title
658-
rescue StandardError
659-
retried ||= 0
660-
retried += 1
661-
puts "Failed in #{__LINE__}##{retried}"
662-
retry if retried < 3
663669
end
664670
end
665671
end

test/helper.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ def connection_exist_method
3131

3232
BaseMigration = (ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration) # rubocop:disable Style/ConstantName
3333

34-
Phenix.configure
35-
3634
require 'active_support/test_case'
3735

3836
# support multiple before/after blocks per example
@@ -96,7 +94,25 @@ def table_has_column?(table, column)
9694
def self.with_phenix
9795
before do
9896
ActiveRecord::Base.stubs(:with_default_shard).yields
97+
98+
# Populate unsharded databases
99+
Phenix.configure do |config|
100+
config.schema_path = File.join(Dir.pwd, 'test', 'unsharded_schema.rb')
101+
config.skip_database = lambda do |name, _|
102+
%w[test_shard_0 test_shard_0_slave test_shard_1 test_shard_1_slave].include?(name)
103+
end
104+
end
99105
Phenix.rise!(with_schema: true)
106+
107+
# Populate sharded databases
108+
Phenix.configure do |config|
109+
config.schema_path = File.join(Dir.pwd, 'test', 'sharded_schema.rb')
110+
config.skip_database = lambda do |name, _|
111+
%w[test test_slave test2 test2_slave].include?(name)
112+
end
113+
end
114+
Phenix.rise!(with_schema: true)
115+
100116
ActiveRecord::Base.unstub(:with_default_shard)
101117
end
102118

test/migrator_test.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44
describe ActiveRecord::Migrator do
55
with_phenix
66

7+
before { ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym) }
8+
79
it "migrates" do
810
migration_path = File.join(File.dirname(__FILE__), "/migrations")
11+
12+
refute ActiveRecord::Base.current_shard_id
913
ActiveRecord::Migrator.migrate(migration_path)
14+
1015
ActiveRecord::Base.on_all_shards do
1116
assert ActiveRecord::Base.connection.public_send(connection_exist_method, :schema_migrations), "Schema Migrations doesn't exist"
12-
assert ActiveRecord::Base.connection.public_send(connection_exist_method, :accounts)
17+
assert ActiveRecord::Base.connection.public_send(connection_exist_method, :tickets)
18+
refute ActiveRecord::Base.connection.public_send(connection_exist_method, :accounts)
1319
assert ActiveRecord::Base.connection.select_value("select version from schema_migrations where version = '20110824010216'")
1420
assert ActiveRecord::Base.connection.select_value("select version from schema_migrations where version = '20110829215912'")
1521
end
1622

1723
ActiveRecord::Base.on_all_shards do
1824
assert table_has_column?("tickets", "sharded_column")
19-
assert !table_has_column?("accounts", "non_sharded_column")
2025
end
2126

2227
ActiveRecord::Base.on_shard(nil) do
23-
assert !table_has_column?("tickets", "sharded_column")
2428
assert table_has_column?("accounts", "non_sharded_column")
2529
end
2630

test/schema_dumper_extension_test.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
with_phenix
1010

1111
before do
12+
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
13+
1214
# create shard-specific columns
1315
ActiveRecord::Migrator.migrations_paths = [File.join(File.dirname(__FILE__), "/migrations")]
1416
ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths)
@@ -25,18 +27,15 @@
2527

2628
ActiveRecord::Base.on_all_shards do
2729
assert ActiveRecord::Base.connection.public_send(connection_exist_method, :schema_migrations), "Schema Migrations doesn't exist"
28-
assert ActiveRecord::Base.connection.public_send(connection_exist_method, :accounts)
2930
assert ActiveRecord::Base.connection.select_value("select version from schema_migrations where version = '20110824010216'")
3031
assert ActiveRecord::Base.connection.select_value("select version from schema_migrations where version = '20110829215912'")
3132
end
3233

3334
ActiveRecord::Base.on_all_shards do
3435
assert table_has_column?("tickets", "sharded_column")
35-
assert !table_has_column?("accounts", "non_sharded_column")
3636
end
3737

3838
ActiveRecord::Base.on_shard(nil) do
39-
assert !table_has_column?("tickets", "sharded_column")
4039
assert table_has_column?("accounts", "non_sharded_column")
4140
end
4241
end

test/sharded_schema.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
ActiveRecord::Migration.verbose = false
3+
4+
ActiveRecord::Schema.define(version: 1) do
5+
create_table "tickets", force: true do |t|
6+
t.string "title"
7+
t.integer "account_id"
8+
t.datetime "created_at"
9+
t.datetime "updated_at"
10+
end
11+
end

test/schema.rb renamed to test/unsharded_schema.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818
t.integer "person_id"
1919
end
2020

21-
create_table "tickets", force: true do |t|
22-
t.string "title"
23-
t.integer "account_id"
24-
t.datetime "created_at"
25-
t.datetime "updated_at"
26-
end
27-
2821
create_table "people", force: true do |t|
2922
t.string "name"
3023
t.string "type"

0 commit comments

Comments
 (0)