activerecord-turntable 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93edb726b99bdd459ea374868a9454b6517adcfb
4
- data.tar.gz: 9b48a0dee236417ace3df3982dadf96cd2466558
3
+ metadata.gz: 7a4bd6cbf61673b4f11bba18e22ff888a694f09a
4
+ data.tar.gz: b94e9d04efa41f8b58f5b65bdcc51bd4cb2a890c
5
5
  SHA512:
6
- metadata.gz: 90def0476116ac0dce790fadc78d182a82d9211dd10ceb71d68ffee33473b829492c54ffea76b46887fda18d97f09053f6898ea0ac3d0027083600d427082f3f
7
- data.tar.gz: 65de6cddde8f9c708b55f190efcc8648119114cc3a87b1bc64500968861186e7bbd09eda6a860051cbf847964d4614e7c7718b7c02eaaa4d10501ff4ee8bb605
6
+ metadata.gz: 4c41774f64e47dde724ecd71b6eabe913c165d40ed159d73b867e0447ae64313437f0db4a4dcb98fb0e85f467ba3985b8a1974c6c97b558a9b793f115e093071
7
+ data.tar.gz: 63184562e69d48c2b2ae73b70878d1b01e850c1caf5ff11fd7a21ebe3c5e157b12f4a84e79da4cb94b198fa82193aa86328f7b493078aaf5ac7644c9f59345c5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## activerecord-turntable 2.1.1 ##
2
+
3
+ ### Bugfixes
4
+
5
+ * Fix `ActiveRecord::Fixtures` doesn't working
6
+ * Fix `ActiveRecord::Base.clear_active_connections!` to release connections established by turntable at development env
7
+
1
8
  ## activerecord-turntable 2.1.0 ##
2
9
 
3
10
  Support activerecord 4.2.0
@@ -2,11 +2,18 @@
2
2
  # force TestFixtures to begin transaction with all shards.
3
3
  #
4
4
  require 'active_record/fixtures'
5
+
5
6
  module ActiveRecord
6
7
  class FixtureSet
8
+ extend ActiveRecord::Turntable::Util
9
+
7
10
  def self.create_fixtures(fixtures_directory, fixture_set_names, class_names = {}, config = ActiveRecord::Base)
8
11
  fixture_set_names = Array(fixture_set_names).map(&:to_s)
9
- class_names = ClassCache.new class_names, config
12
+ class_names = if ar41_or_later?
13
+ ClassCache.new class_names, config
14
+ else
15
+ class_names = class_names.stringify_keys
16
+ end
10
17
 
11
18
  # FIXME: Apparently JK uses this.
12
19
  connection = block_given? ? yield : ActiveRecord::Base.connection
@@ -20,8 +27,12 @@ module ActiveRecord
20
27
  fixtures_map = {}
21
28
 
22
29
  fixture_sets = files_to_read.map do |fs_name|
23
- klass = class_names[fs_name]
24
- conn = klass ? klass.connection : connection
30
+ klass = if ar41_or_later?
31
+ class_names[fs_name]
32
+ else
33
+ class_names[fs_name] || default_fixture_model_name(fs_name)
34
+ end
35
+ conn = klass.is_a?(String) ? connection : klass.connection
25
36
  fixtures_map[fs_name] = new( # ActiveRecord::FixtureSet.new
26
37
  conn,
27
38
  fs_name,
@@ -29,9 +40,13 @@ module ActiveRecord
29
40
  ::File.join(fixtures_directory, fs_name))
30
41
  end
31
42
 
32
- update_all_loaded_fixtures fixtures_map
43
+ if ar42_or_later?
44
+ update_all_loaded_fixtures fixtures_map
45
+ else
46
+ all_loaded_fixtures.update(fixtures_map)
47
+ end
33
48
 
34
- ActiveRecord::Turntable::Base.force_transaction_all_shards!(:requires_new => true) do
49
+ ActiveRecord::Base.force_transaction_all_shards!(:requires_new => true) do
35
50
  fixture_sets.each do |fs|
36
51
  conn = fs.model_class.respond_to?(:connection) ? fs.model_class.connection : connection
37
52
  table_rows = fs.table_rows
@@ -63,7 +78,7 @@ module ActiveRecord
63
78
  end
64
79
 
65
80
  module TestFixtures
66
- extend ActiveRecord::Turntable::Util
81
+ include ActiveRecord::Turntable::Util
67
82
 
68
83
  def setup_fixtures(config = ActiveRecord::Base)
69
84
  return unless !ActiveRecord::Base.configurations.blank?
@@ -38,6 +38,16 @@ module ActiveRecord::Turntable
38
38
  end
39
39
  end
40
40
 
41
+ %w(active_connection?).each do |name|
42
+ define_method(name.to_sym) do |*args|
43
+ @proxy.master.connection_pool.send(name.to_sym) ||
44
+ @proxy.seq.connection_pool.try(name.to_sym) if @proxy.respond_to?(:seq) ||
45
+ @proxy.shards.values.any? do |pool|
46
+ pool.connection_pool.send(name.to_sym)
47
+ end
48
+ end
49
+ end
50
+
41
51
  %w(disconnect! release_connection clear_all_connections! clear_active_connections! clear_reloadable_connections! clear_stale_cached_connections! verify_active_connections!).each do |name|
42
52
  define_method(name.to_sym) do
43
53
  @proxy.master.connection_pool.send(name.to_sym)
@@ -1,48 +1,46 @@
1
1
  module ActiveRecord::Turntable
2
2
  class Shard
3
+ module Connections; end
4
+
3
5
  DEFAULT_CONFIG = {
4
6
  "connection" => (defined?(Rails) ? Rails.env : "development")
5
7
  }.with_indifferent_access
6
8
 
9
+ attr_reader :name
10
+
7
11
  def initialize(shard_spec)
8
12
  @config = DEFAULT_CONFIG.merge(shard_spec)
9
13
  @name = @config["connection"]
14
+ ActiveRecord::Base.turntable_connections[name] = connection_pool
10
15
  end
11
16
 
12
17
  def connection_pool
13
- @connection_pool ||= retrieve_connection_pool
18
+ connection_klass.connection_pool
14
19
  end
15
20
 
16
21
  def connection
17
- connection = connection_pool.connection
18
- connection.turntable_shard_name = name
19
- connection
20
- end
21
-
22
- def name
23
- @name
22
+ connection_pool.connection.tap do |conn|
23
+ conn.turntable_shard_name ||= name
24
+ end
24
25
  end
25
26
 
26
27
  private
27
28
 
28
- def retrieve_connection_pool
29
- ActiveRecord::Base.turntable_connections[name] ||=
30
- begin
31
- config = ActiveRecord::Base.configurations[Rails.env]["shards"][name]
32
- raise ArgumentError, "Unknown database config: #{name}, have #{ActiveRecord::Base.configurations.inspect}" unless config
33
- ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec_for(config))
34
- end
29
+ def connection_klass
30
+ @connection_klass ||= create_connection_class
35
31
  end
36
32
 
37
- def spec_for(config)
38
- begin
39
- require "active_record/connection_adapters/#{config['adapter']}_adapter"
40
- rescue LoadError => e
41
- raise "Please install the #{config['adapter']} adapter: `gem install activerecord-#{config['adapter']}-adapter` (#{e})"
33
+ def create_connection_class
34
+ if Connections.const_defined?(name.classify)
35
+ klass = Connections.const_get(name.classify)
36
+ else
37
+ klass = Class.new(ActiveRecord::Base)
38
+ Connections.const_set(name.classify, klass)
39
+ klass.abstract_class = true
42
40
  end
43
- adapter_method = "#{config['adapter']}_connection"
44
-
45
- ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(config, adapter_method)
41
+ klass.remove_connection
42
+ klass.establish_connection ActiveRecord::Base.connection_pool.spec.config[:shards][name].with_indifferent_access
43
+ klass
46
44
  end
47
45
  end
48
46
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Turntable
3
- VERSION = "2.1.0"
3
+ VERSION = "2.1.1"
4
4
  end
5
5
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ require 'active_record'
4
+ require 'active_record/turntable/active_record_ext/fixtures'
5
+
6
+ describe ActiveRecord::FixtureSet do
7
+ before(:all) do
8
+ reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
9
+ end
10
+
11
+ before do
12
+ establish_connection_to(:test)
13
+ truncate_shard
14
+ end
15
+
16
+ let(:fixtures_root) { File.join(File.dirname(__FILE__), "../../../fixtures") }
17
+ let(:fixture_file) { File.join(fixtures_root, "cards.yml") }
18
+ let(:cards) { YAML.load(ERB.new(IO.read(fixture_file)).result) }
19
+
20
+ describe ".create_fixtures" do
21
+ subject { ActiveRecord::FixtureSet.create_fixtures(fixtures_root, "cards") }
22
+ it { is_expected.to be_instance_of(Array) }
23
+ it "creates card records" do
24
+ expect {subject}.to change {Card.count}.from(0).to(cards.size)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ require 'active_record'
4
+ require 'active_record/turntable/active_record_ext/fixtures'
5
+
6
+ describe ActiveRecord::TestFixtures do
7
+ before(:all) do
8
+ reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
9
+ end
10
+
11
+ before do
12
+ establish_connection_to(:test)
13
+ truncate_shard
14
+ end
15
+
16
+ let(:fixtures_root) { File.join(File.dirname(__FILE__), "../../../fixtures") }
17
+ let(:fixture_file) { File.join(fixtures_root, "cards.yml") }
18
+ let(:test_fixture_class) { Class.new(ActiveSupport::TestCase) { include ActiveRecord::TestFixtures } }
19
+ let(:test_fixture) { test_fixture_class.new("test") }
20
+ let(:cards) { YAML.load(ERB.new(IO.read(fixture_file)).result) }
21
+
22
+ before do
23
+ test_fixture_class.fixture_path = fixtures_root
24
+ end
25
+
26
+ describe "#setup_fixtures" do
27
+ after do
28
+ test_fixture.teardown_fixtures
29
+ end
30
+
31
+ subject { test_fixture.setup_fixtures }
32
+ it { expect { subject }.not_to raise_error }
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ card_1:
2
+ id: 1
3
+ name: card_1
4
+ hp: 10
5
+ mp: 10
6
+
7
+ card_2:
8
+ id: 2
9
+ name: card_2
10
+ hp: 20
11
+ mp: 20
@@ -19,6 +19,7 @@ module TurntableHelper
19
19
 
20
20
  def truncate_shard
21
21
  ActiveRecord::Base.descendants.each do |klass|
22
+ next if klass.abstract_class?
22
23
  klass.delete_all
23
24
  end
24
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-turntable
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gussan
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-19 00:00:00.000000000 Z
12
+ date: 2015-01-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -407,9 +407,11 @@ files:
407
407
  - spec/active_record/turntable/active_record_ext/association_preloader_spec.rb
408
408
  - spec/active_record/turntable/active_record_ext/association_spec.rb
409
409
  - spec/active_record/turntable/active_record_ext/clever_load_spec.rb
410
+ - spec/active_record/turntable/active_record_ext/fixture_set_spec.rb
410
411
  - spec/active_record/turntable/active_record_ext/locking_optimistic_spec.rb
411
412
  - spec/active_record/turntable/active_record_ext/migration_spec.rb
412
413
  - spec/active_record/turntable/active_record_ext/persistence_spec.rb
414
+ - spec/active_record/turntable/active_record_ext/test_fixtures_spec.rb
413
415
  - spec/active_record/turntable/algorithm/range_algorithm_spec.rb
414
416
  - spec/active_record/turntable/algorithm/range_bsearch_algorithm_spec.rb
415
417
  - spec/active_record/turntable/algorithm_spec.rb
@@ -432,6 +434,7 @@ files:
432
434
  - spec/config/turntable.yml
433
435
  - spec/fabricators/.gitkeep
434
436
  - spec/fabricators/turntable_fabricator.rb
437
+ - spec/fixtures/cards.yml
435
438
  - spec/migrations/.gitkeep
436
439
  - spec/migrations/001_create_users.rb
437
440
  - spec/migrations/002_create_user_statuses.rb
@@ -474,9 +477,11 @@ test_files:
474
477
  - spec/active_record/turntable/active_record_ext/association_preloader_spec.rb
475
478
  - spec/active_record/turntable/active_record_ext/association_spec.rb
476
479
  - spec/active_record/turntable/active_record_ext/clever_load_spec.rb
480
+ - spec/active_record/turntable/active_record_ext/fixture_set_spec.rb
477
481
  - spec/active_record/turntable/active_record_ext/locking_optimistic_spec.rb
478
482
  - spec/active_record/turntable/active_record_ext/migration_spec.rb
479
483
  - spec/active_record/turntable/active_record_ext/persistence_spec.rb
484
+ - spec/active_record/turntable/active_record_ext/test_fixtures_spec.rb
480
485
  - spec/active_record/turntable/algorithm/range_algorithm_spec.rb
481
486
  - spec/active_record/turntable/algorithm/range_bsearch_algorithm_spec.rb
482
487
  - spec/active_record/turntable/algorithm_spec.rb
@@ -499,6 +504,7 @@ test_files:
499
504
  - spec/config/turntable.yml
500
505
  - spec/fabricators/.gitkeep
501
506
  - spec/fabricators/turntable_fabricator.rb
507
+ - spec/fixtures/cards.yml
502
508
  - spec/migrations/.gitkeep
503
509
  - spec/migrations/001_create_users.rb
504
510
  - spec/migrations/002_create_user_statuses.rb