active_record_shards 3.0.0.beta1 → 3.0.0.beta2
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 +7 -0
- data/lib/active_record_shards.rb +6 -10
- data/lib/active_record_shards/association_collection_connection_selection.rb +1 -6
- data/lib/active_record_shards/connection_specification.rb +1 -1
- data/lib/active_record_shards/connection_switcher.rb +5 -18
- data/lib/active_record_shards/default_slave_patches.rb +37 -44
- data/lib/active_record_shards/migration.rb +4 -11
- data/lib/active_record_shards/model.rb +0 -4
- data/lib/active_record_shards/tasks.rb +1 -1
- metadata +13 -43
- data/lib/active_record_shards/arel_engine.rb +0 -13
- data/test/configuration_parser_test.rb +0 -98
- data/test/connection_switching_test.rb +0 -515
- data/test/cowardly_migration/20110824010215_cowardly_migration.rb +0 -8
- data/test/database.yml +0 -73
- data/test/database_parse_test.yml +0 -21
- data/test/failure_migration/20110824010215_failure_migration.rb +0 -14
- data/test/helper.rb +0 -57
- data/test/migrations/20110824010216_shard_migration.rb +0 -12
- data/test/migrations/20110829215912_account_migration.rb +0 -13
- data/test/migrator_test.rb +0 -113
- data/test/models.rb +0 -38
- data/test/schema.rb +0 -36
data/test/database.yml
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
test:
|
2
|
-
adapter: mysql2
|
3
|
-
encoding: utf8
|
4
|
-
database: ars_test
|
5
|
-
username: root
|
6
|
-
password:
|
7
|
-
host: 127.0.0.1
|
8
|
-
shard_names: ['0', '1']
|
9
|
-
|
10
|
-
test_slave:
|
11
|
-
adapter: mysql2
|
12
|
-
encoding: utf8
|
13
|
-
database: ars_test_slave
|
14
|
-
username: root
|
15
|
-
password:
|
16
|
-
host: 127.0.0.1
|
17
|
-
|
18
|
-
test_shard_0:
|
19
|
-
adapter: mysql2
|
20
|
-
encoding: utf8
|
21
|
-
database: ars_test_shard0
|
22
|
-
username: root
|
23
|
-
password:
|
24
|
-
host: 127.0.0.1
|
25
|
-
|
26
|
-
test_shard_0_slave:
|
27
|
-
adapter: mysql2
|
28
|
-
encoding: utf8
|
29
|
-
database: ars_test_shard0_slave
|
30
|
-
username: root
|
31
|
-
password:
|
32
|
-
host: 127.0.0.1
|
33
|
-
|
34
|
-
test_shard_1:
|
35
|
-
adapter: mysql2
|
36
|
-
encoding: utf8
|
37
|
-
database: ars_test_shard1
|
38
|
-
username: root
|
39
|
-
password:
|
40
|
-
host: 127.0.0.1
|
41
|
-
|
42
|
-
test_shard_1_slave:
|
43
|
-
adapter: mysql2
|
44
|
-
encoding: utf8
|
45
|
-
database: ars_test_shard1_slave
|
46
|
-
username: root
|
47
|
-
password:
|
48
|
-
host: 127.0.0.1
|
49
|
-
|
50
|
-
test2:
|
51
|
-
adapter: mysql2
|
52
|
-
encoding: utf8
|
53
|
-
database: ars_test2
|
54
|
-
username: root
|
55
|
-
password:
|
56
|
-
host: 127.0.0.1
|
57
|
-
|
58
|
-
test2_slave:
|
59
|
-
adapter: mysql2
|
60
|
-
encoding: utf8
|
61
|
-
database: ars_test2_slave
|
62
|
-
username: root
|
63
|
-
password:
|
64
|
-
host: 127.0.0.1
|
65
|
-
|
66
|
-
alternative:
|
67
|
-
adapter: mysql2
|
68
|
-
encoding: utf8
|
69
|
-
database: ars_test_alternative
|
70
|
-
username: root
|
71
|
-
password:
|
72
|
-
host: 127.0.0.1
|
73
|
-
shard_names: ['0', '1']
|
@@ -1,21 +0,0 @@
|
|
1
|
-
test:
|
2
|
-
adapter: mysql
|
3
|
-
encoding: utf8
|
4
|
-
database: ars_test
|
5
|
-
port: 123
|
6
|
-
username: root
|
7
|
-
password:
|
8
|
-
host: main_host
|
9
|
-
slave:
|
10
|
-
host: main_slave_host
|
11
|
-
shards:
|
12
|
-
a:
|
13
|
-
database: ars_test_shard_a
|
14
|
-
host: shard_a_host
|
15
|
-
slave:
|
16
|
-
host: shard_a_slave_host
|
17
|
-
b:
|
18
|
-
database: ars_test_shard_b
|
19
|
-
host: shard_b_host
|
20
|
-
slave:
|
21
|
-
database: ars_test_shard_b_slave
|
@@ -1,14 +0,0 @@
|
|
1
|
-
class FailureMigration < ActiveRecord::Migration
|
2
|
-
shard :all
|
3
|
-
|
4
|
-
def self.up
|
5
|
-
$fail_at_two ||= 0
|
6
|
-
$fail_at_two += 1
|
7
|
-
raise "FAIL FAIL FAIL" if $fail_at_two == 2
|
8
|
-
add_column :tickets, :sharded_column, :integer
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.down
|
12
|
-
remove_column :tickets, :sharded_column
|
13
|
-
end
|
14
|
-
end
|
data/test/helper.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
Bundler.require
|
3
|
-
|
4
|
-
require 'minitest/autorun'
|
5
|
-
MiniTest::Reporters.use! MiniTest::Reporters::DefaultReporter.new
|
6
|
-
|
7
|
-
if defined?(Debugger)
|
8
|
-
::Debugger.start
|
9
|
-
::Debugger.settings[:autoeval] = true if ::Debugger.respond_to?(:settings)
|
10
|
-
end
|
11
|
-
|
12
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
13
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
14
|
-
require 'active_record_shards'
|
15
|
-
require 'logger'
|
16
|
-
|
17
|
-
RAILS_ENV = "test"
|
18
|
-
|
19
|
-
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/test.log")
|
20
|
-
ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
21
|
-
|
22
|
-
def init_schema
|
23
|
-
ActiveRecord::Base.configurations.each do |name, conf|
|
24
|
-
`echo "drop DATABASE if exists #{conf['database']}" | mysql --user=#{conf['username']}`
|
25
|
-
`echo "create DATABASE #{conf['database']}" | mysql --user=#{conf['username']}`
|
26
|
-
ActiveRecord::Base.establish_connection(name)
|
27
|
-
load(File.dirname(__FILE__) + "/schema.rb")
|
28
|
-
end
|
29
|
-
end
|
30
|
-
init_schema
|
31
|
-
|
32
|
-
require 'models'
|
33
|
-
|
34
|
-
require 'active_support/test_case'
|
35
|
-
class Minitest::Spec
|
36
|
-
def clear_databases
|
37
|
-
ActiveRecord::Base.configurations.each do |name, conf|
|
38
|
-
ActiveRecord::Base.establish_connection(name)
|
39
|
-
ActiveRecord::Base.connection.execute("DELETE FROM accounts")
|
40
|
-
ActiveRecord::Base.connection.execute("DELETE FROM tickets")
|
41
|
-
end
|
42
|
-
ActiveRecord::Base.establish_connection(RAILS_ENV)
|
43
|
-
end
|
44
|
-
before { clear_databases }
|
45
|
-
|
46
|
-
def assert_using_master_db
|
47
|
-
assert_using_database('ars_test')
|
48
|
-
end
|
49
|
-
|
50
|
-
def assert_using_slave_db
|
51
|
-
assert_using_database('ars_test_slave')
|
52
|
-
end
|
53
|
-
|
54
|
-
def assert_using_database(db_name, model = ActiveRecord::Base)
|
55
|
-
assert_equal(db_name, model.connection.current_database)
|
56
|
-
end
|
57
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
#This is to recreate the deleted onetime payment for DealFish
|
2
|
-
class ShardMigration < ActiveRecord::Migration
|
3
|
-
shard :all
|
4
|
-
|
5
|
-
def self.up
|
6
|
-
add_column :emails, :sharded_column, :integer
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.down
|
10
|
-
remove_column :emails, :sharded_column
|
11
|
-
end
|
12
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
# See ticket 130214
|
2
|
-
# Setting up an odd initial three year term for box.net to be paid by credit card by the normal means
|
3
|
-
class AccountMigration < ActiveRecord::Migration
|
4
|
-
shard :none
|
5
|
-
|
6
|
-
def self.up
|
7
|
-
add_column :accounts, :non_sharded_column, :integer
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.down
|
11
|
-
remove_column :accounts, :non_sharded_column
|
12
|
-
end
|
13
|
-
end
|
data/test/migrator_test.rb
DELETED
@@ -1,113 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class CowardlyMigration < ActiveRecord::Migration
|
4
|
-
def self.up
|
5
|
-
"not gonna happen"
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.down
|
9
|
-
"uh uh"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
describe ActiveRecord::Migrator do
|
14
|
-
before do
|
15
|
-
init_schema
|
16
|
-
end
|
17
|
-
|
18
|
-
it "migrates" do
|
19
|
-
migration_path = File.join(File.dirname(__FILE__), "/migrations")
|
20
|
-
ActiveRecord::Migrator.migrate(migration_path)
|
21
|
-
ActiveRecord::Base.on_all_shards do
|
22
|
-
assert ActiveRecord::Base.connection.table_exists?(:schema_migrations), "Schema Migrations doesn't exist"
|
23
|
-
assert ActiveRecord::Base.connection.table_exists?(:accounts)
|
24
|
-
assert ActiveRecord::Base.connection.select_value("select version from schema_migrations where version = '20110824010216'")
|
25
|
-
assert ActiveRecord::Base.connection.select_value("select version from schema_migrations where version = '20110829215912'")
|
26
|
-
end
|
27
|
-
|
28
|
-
ActiveRecord::Base.on_all_shards do
|
29
|
-
assert table_has_column?("emails", "sharded_column")
|
30
|
-
assert !table_has_column?("accounts", "non_sharded_column")
|
31
|
-
end
|
32
|
-
|
33
|
-
ActiveRecord::Base.on_shard(nil) do
|
34
|
-
assert !table_has_column?("emails", "sharded_column")
|
35
|
-
assert table_has_column?("accounts", "non_sharded_column")
|
36
|
-
end
|
37
|
-
|
38
|
-
# now test down/ up
|
39
|
-
ActiveRecord::Migrator.run(:down, migration_path, 20110824010216)
|
40
|
-
ActiveRecord::Base.on_all_shards do
|
41
|
-
assert !table_has_column?("emails", "sharded_column")
|
42
|
-
end
|
43
|
-
|
44
|
-
ActiveRecord::Migrator.run(:down, migration_path, 20110829215912)
|
45
|
-
ActiveRecord::Base.on_shard(nil) do
|
46
|
-
assert !table_has_column?("accounts", "non_sharded_column")
|
47
|
-
end
|
48
|
-
|
49
|
-
ActiveRecord::Migrator.run(:up, migration_path, 20110824010216)
|
50
|
-
ActiveRecord::Base.on_all_shards do
|
51
|
-
assert table_has_column?("emails", "sharded_column")
|
52
|
-
end
|
53
|
-
|
54
|
-
ActiveRecord::Migrator.run(:up, migration_path, 20110829215912)
|
55
|
-
ActiveRecord::Base.on_shard(nil) do
|
56
|
-
assert table_has_column?("accounts", "non_sharded_column")
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
it "does not migrate bad migrations" do
|
61
|
-
migration_path = File.join(File.dirname(__FILE__), "/cowardly_migration")
|
62
|
-
exception = nil
|
63
|
-
begin
|
64
|
-
ActiveRecord::Migrator.migrate(migration_path)
|
65
|
-
rescue Exception => e
|
66
|
-
exception = e
|
67
|
-
end
|
68
|
-
assert e
|
69
|
-
end
|
70
|
-
|
71
|
-
it "fails with failing migrations" do
|
72
|
-
# like, if you have to break a migration in the middle somewhere.
|
73
|
-
migration_path = File.join(File.dirname(__FILE__), "/failure_migration")
|
74
|
-
|
75
|
-
assert failure_migration_pending?(migration_path)
|
76
|
-
begin
|
77
|
-
ActiveRecord::Migrator.migrate(migration_path)
|
78
|
-
rescue
|
79
|
-
# after first fail, should still be pending
|
80
|
-
assert failure_migration_pending?(migration_path)
|
81
|
-
retry
|
82
|
-
end
|
83
|
-
|
84
|
-
assert !failure_migration_pending?(migration_path)
|
85
|
-
ActiveRecord::Base.on_all_shards do
|
86
|
-
assert table_has_column?("tickets", "sharded_column")
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe "#shard_status" do
|
91
|
-
it "shows nothing if everything is ok" do
|
92
|
-
ActiveRecord::Migrator.shard_status([1]).must_equal([{}, {}])
|
93
|
-
end
|
94
|
-
|
95
|
-
it "shows missing migrations" do
|
96
|
-
ActiveRecord::Migrator.shard_status([]).must_equal([{}, {nil => [1], "0" => [1], "1" => [1]}])
|
97
|
-
end
|
98
|
-
|
99
|
-
it "shows pending migrations" do
|
100
|
-
ActiveRecord::Migrator.shard_status([1, 2]).must_equal([{nil => [2], "0" => [2], "1" => [2]}, {}])
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
private
|
105
|
-
|
106
|
-
def failure_migration_pending?(migration_path)
|
107
|
-
ActiveRecord::Migrator.new(:up, migration_path).pending_migrations.detect { |f| f.name == "FailureMigration" }
|
108
|
-
end
|
109
|
-
|
110
|
-
def table_has_column?(table, column)
|
111
|
-
!ActiveRecord::Base.connection.select_values("desc #{table}").grep(column).empty?
|
112
|
-
end
|
113
|
-
end
|
data/test/models.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
class Account < ActiveRecord::Base
|
2
|
-
# attributes: id, name, updated_at, created_at
|
3
|
-
not_sharded
|
4
|
-
|
5
|
-
has_many :tickets
|
6
|
-
has_many :account_things
|
7
|
-
end
|
8
|
-
|
9
|
-
class AccountThing < ActiveRecord::Base
|
10
|
-
not_sharded
|
11
|
-
|
12
|
-
if respond_to?(:where)
|
13
|
-
scope :enabled, where(:enabled => true)
|
14
|
-
else
|
15
|
-
named_scope :enabled, :conditions => {:enabled => true}
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class Email < ActiveRecord::Base
|
20
|
-
not_sharded
|
21
|
-
establish_connection_override :alternative
|
22
|
-
end
|
23
|
-
|
24
|
-
class AccountInherited < Account
|
25
|
-
end
|
26
|
-
|
27
|
-
class Ticket < ActiveRecord::Base
|
28
|
-
# attributes: id, title, account_id, updated_at, created_at
|
29
|
-
belongs_to :account
|
30
|
-
end
|
31
|
-
|
32
|
-
class Person < ActiveRecord::Base
|
33
|
-
not_sharded
|
34
|
-
end
|
35
|
-
|
36
|
-
class User < Person
|
37
|
-
end
|
38
|
-
|
data/test/schema.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
ActiveRecord::Migration.verbose = false
|
2
|
-
|
3
|
-
ActiveRecord::Schema.define(:version => 1) do
|
4
|
-
create_table "accounts", :force => true do |t|
|
5
|
-
t.string "name"
|
6
|
-
t.datetime "created_at"
|
7
|
-
t.datetime "updated_at"
|
8
|
-
end
|
9
|
-
|
10
|
-
create_table "account_things", :force => true do |t|
|
11
|
-
t.integer "account_id"
|
12
|
-
t.boolean "enabled", :default => true
|
13
|
-
end
|
14
|
-
|
15
|
-
create_table "emails", :force => true do |t|
|
16
|
-
t.string "from"
|
17
|
-
t.string "to"
|
18
|
-
t.text "mail"
|
19
|
-
t.datetime "created_at"
|
20
|
-
t.datetime "updated_at"
|
21
|
-
end
|
22
|
-
|
23
|
-
create_table "tickets", :force => true do |t|
|
24
|
-
t.string "title"
|
25
|
-
t.integer "account_id"
|
26
|
-
t.datetime "created_at"
|
27
|
-
t.datetime "updated_at"
|
28
|
-
end
|
29
|
-
|
30
|
-
create_table "people", :force => true do |t|
|
31
|
-
t.string "name"
|
32
|
-
t.string "type"
|
33
|
-
t.datetime "created_at"
|
34
|
-
t.datetime "updated_at"
|
35
|
-
end
|
36
|
-
end
|