active_record_shards 2.4.2 → 2.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,6 +31,30 @@ module ActiveRecord
31
31
  end
32
32
  end
33
33
  end
34
+
35
+ # don't allow Migrator class to cache versions
36
+ def migrated
37
+ self.class.get_all_versions
38
+ end
39
+
40
+ # list of pending migrations is any migrations that haven't run on all shards.
41
+ def pending_migrations
42
+ migration_counts = Hash.new(0)
43
+ ActiveRecord::Base.on_shard(nil) do
44
+ migrated.each { |v| migration_counts[v] += 1 }
45
+ end
46
+
47
+ shard_count = 1
48
+ ActiveRecord::Base.on_all_shards do
49
+ migrated.each { |v| migration_counts[v] += 1 }
50
+ shard_count += 1
51
+ end
52
+
53
+ migrations.select do |m|
54
+ count = migration_counts[m.version]
55
+ count.nil? || count < shard_count
56
+ end
57
+ end
34
58
  end
35
59
  end
36
60
 
@@ -75,19 +75,28 @@ class MigratorTest < ActiveSupport::TestCase
75
75
 
76
76
  def test_failing_migration
77
77
  # like, if you have to break a migration in the middle somewhere.
78
+ migration_path = File.join(File.dirname(__FILE__), "/failure_migration")
79
+
80
+ assert failure_migration_pending?(migration_path)
78
81
  begin
79
- migration_path = File.join(File.dirname(__FILE__), "/failure_migration")
80
82
  ActiveRecord::Migrator.migrate(migration_path)
81
83
  rescue
84
+ # after first fail, should still be pending
85
+ assert failure_migration_pending?(migration_path)
82
86
  retry
83
87
  end
84
88
 
89
+ assert !failure_migration_pending?(migration_path)
85
90
  ActiveRecord::Base.on_all_shards do
86
91
  assert table_has_column?("tickets", "sharded_column")
87
92
  end
88
93
  end
89
94
 
90
95
  private
96
+ def failure_migration_pending?(migration_path)
97
+ ActiveRecord::Migrator.new(:up, migration_path).pending_migrations.detect { |f| f.name == "FailureMigration" }
98
+ end
99
+
91
100
  def table_has_column?(table, column)
92
101
  ActiveRecord::Base.connection.select_value("show create table #{table}") =~ /#{column}/
93
102
  end