Skip to content

Commit 303fec0

Browse files
authored
Ruby (#30)
* cop * log
1 parent 6457421 commit 303fec0

File tree

2 files changed

+37
-60
lines changed

2 files changed

+37
-60
lines changed

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ jobs:
1010
# See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor
1111
docker:
1212
- image: cimg/rust:1.58.1
13+
environment:
14+
RUST_LOG: info
1315
- image: cimg/postgres:14.0
1416
auth:
1517
username: mydockerhub-user

tests/ruby/tests.rb

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,27 @@
1-
require "active_record"
1+
# frozen_string_literal: true
2+
3+
require 'active_record'
24

35
# Uncomment these two to see all queries.
46
# ActiveRecord.verbose_query_logs = true
57
# ActiveRecord::Base.logger = Logger.new(STDOUT)
68

79
ActiveRecord::Base.establish_connection(
8-
adapter: "postgresql",
9-
host: "127.0.0.1",
10+
adapter: 'postgresql',
11+
host: '127.0.0.1',
1012
port: 6432,
11-
username: "sharding_user",
12-
password: "sharding_user",
13-
database: "rails_dev",
13+
username: 'sharding_user',
14+
password: 'sharding_user',
15+
database: 'rails_dev',
1416
prepared_statements: false, # Transaction mode
15-
advisory_locks: false, # Same
17+
advisory_locks: false # Same
1618
)
1719

18-
class TestTable < ActiveRecord::Base
19-
self.table_name = "test_table"
20-
end
21-
2220
class TestSafeTable < ActiveRecord::Base
23-
self.table_name = "test_safe_table"
21+
self.table_name = 'test_safe_table'
2422
end
2523

26-
class ShouldNeverHappenException < Exception
27-
end
28-
29-
# # Create the table.
30-
class CreateTestTable < ActiveRecord::Migration[7.0]
31-
# Disable transasctions or things will fly out of order!
32-
disable_ddl_transaction!
33-
34-
SHARDS = 3
35-
36-
def change
37-
SHARDS.times do |x|
38-
# This will make this migration reversible!
39-
reversible do
40-
connection.execute "SET SHARD TO '#{x.to_i}'"
41-
connection.execute "SET SERVER ROLE TO 'primary'"
42-
end
43-
44-
# Always wrap the entire migration inside a transaction. If that's not possible,
45-
# execute a `SET SHARD` command before every statement and make sure AR doesn't need
46-
# to load database information beforehand (i.e. it's not the first query in the migration).
47-
connection.transaction do
48-
create_table :test_table, if_not_exists: true do |t|
49-
t.string :name
50-
t.string :description
51-
52-
t.timestamps
53-
end
54-
end
55-
end
56-
end
24+
class ShouldNeverHappenException < RuntimeError
5725
end
5826

5927
class CreateSafeShardedTable < ActiveRecord::Migration[7.0]
@@ -85,53 +53,60 @@ def down
8553
SHARDS.times do |x|
8654
connection.execute "SET SHARD TO '#{x.to_i}'"
8755
connection.execute "SET SERVER ROLE TO 'primary'"
88-
connection.execute "DROP TABLE test_safe_table CASCADE"
56+
connection.execute 'DROP TABLE test_safe_table CASCADE'
8957
end
9058
end
9159
end
9260

93-
20.times do
94-
begin
95-
CreateTestTable.migrate(:down)
96-
rescue Exception
97-
puts "Tables don't exist yet"
98-
end
61+
SHARDS = 3
9962

63+
2.times do
10064
begin
10165
CreateSafeShardedTable.migrate(:down)
10266
rescue Exception
10367
puts "Tables don't exist yet"
10468
end
10569

106-
CreateTestTable.migrate(:up)
10770
CreateSafeShardedTable.migrate(:up)
10871

109-
3.times do |x|
72+
SHARDS.times do |x|
11073
TestSafeTable.connection.execute "SET SHARD TO '#{x.to_i}'"
11174
TestSafeTable.connection.execute "SET SERVER ROLE TO 'primary'"
112-
TestSafeTable.connection.execute "TRUNCATE #{TestTable.table_name}"
75+
TestSafeTable.connection.execute "TRUNCATE #{TestSafeTable.table_name}"
11376
end
11477

115-
10.times do |x|
78+
# Equivalent to Makara's stick_to_master! except it sticks until it's changed.
79+
TestSafeTable.connection.execute "SET SERVER ROLE TO 'primary'"
80+
81+
200.times do |x|
11682
x += 1 # Postgres ids start at 1
11783
TestSafeTable.connection.execute "SET SHARDING KEY TO '#{x.to_i}'"
118-
TestSafeTable.connection.execute "SET SERVER ROLE TO 'primary'"
11984
TestSafeTable.create(id: x, name: "something_special_#{x.to_i}", description: "It's a surprise!")
12085
end
12186

122-
10.times do |x|
87+
TestSafeTable.connection.execute "SET SERVER ROLE TO 'replica'"
88+
89+
100.times do |x|
12390
x += 1 # 0 confuses our sharding function
12491
TestSafeTable.connection.execute "SET SHARDING KEY TO '#{x.to_i}'"
125-
TestSafeTable.connection.execute "SET SERVER ROLE TO 'replica'"
92+
TestSafeTable.find_by_id(x).id
93+
end
94+
95+
# Will use the query parser to direct reads to replicas
96+
TestSafeTable.connection.execute "SET SERVER ROLE TO 'auto'"
97+
98+
100.times do |x|
99+
x += 101
100+
TestSafeTable.connection.execute "SET SHARDING KEY TO '#{x.to_i}'"
126101
TestSafeTable.find_by_id(x).id
127102
end
128103
end
129104

130105
# Test wrong shard
131106
TestSafeTable.connection.execute "SET SHARD TO '1'"
132107
begin
133-
TestSafeTable.create(id: 5, name: "test", description: "test description")
134-
raise ShouldNeverHappenException("Uh oh")
108+
TestSafeTable.create(id: 5, name: 'test', description: 'test description')
109+
raise ShouldNeverHappenException('Uh oh')
135110
rescue ActiveRecord::StatementInvalid
136-
puts "OK"
111+
puts 'OK'
137112
end

0 commit comments

Comments
 (0)