fixture_seed 0.3.3 → 0.4.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 258818336d471c6967feffaa6ff8d3f8c804eb80d13ccb01d2340a5a1399e18f
4
- data.tar.gz: c92f625903bacbc71214ce0d0212c2124304c28a4eabc22d45104a6366a58f66
3
+ metadata.gz: 8d3cd5453c25f8d850aa253af3a3be318d4f321d67ac8d08a47894bcaebc762a
4
+ data.tar.gz: 38f74d96fc45637798f95d0ab92948f9e399a6b447e96ea3d515862ebc61424a
5
5
  SHA512:
6
- metadata.gz: 4b0adc02173d70371db9bb4bade155cdb482ad440a82536184b367c9ea1dc6b6336ecb6523801da0596434ece2e95c74ca0e5f558c361a628e9db37aa2cf6d74
7
- data.tar.gz: 25330e811961ac59f65eba112bf0075e39c97a0efef989eebe76123141cabd575ebef6cbefb91cb6a6674b5753f507ad1d1eec6739df0aa095ad4791a2996cf0
6
+ metadata.gz: eecab5f96b32f9311b2c453c7cdff9a1fc6a0000f0295310022bf3279dec380cb8509d81bc53a9b88d4691d55bd1ecb0ebc34982169d3533f273da7b790a209f
7
+ data.tar.gz: 3119f7c0827b348723a9534d9740802d47c35be613b4b883ac144837bae7d18983808d86eef1b361cde4d7fc745bdfa85e034d03e38a8823c6695f5824d1c6bc
@@ -1,75 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_record"
4
+ require "active_record/fixtures"
4
5
 
5
6
  module FixtureSeed
6
7
  class Loader
8
+ DEFAULT_FIXTURES_PATH = "db/fixtures"
9
+
7
10
  class << self
8
11
  def load_fixtures(fixtures_path = nil)
9
- fixtures_path ||= ENV["FIXTURES_PATH"] || "db/fixtures"
12
+ fixtures_path ||= ENV["FIXTURES_PATH"] || DEFAULT_FIXTURES_PATH
10
13
  fixtures_dir = Rails.root.join(fixtures_path)
11
14
 
12
- unless Dir.exist?(fixtures_dir)
13
- logger = Rails.logger || Logger.new($stdout)
14
- logger.info "[FixtureSeed] Fixtures directory not found: #{fixtures_path}"
15
- return
16
- end
15
+ return unless fixtures_directory_exists?(fixtures_dir, fixtures_path)
16
+
17
+ load_fixture_files(fixtures_dir, fixtures_path)
18
+ end
17
19
 
18
- logger = Rails.logger || Logger.new($stdout)
19
- logger.info "[FixtureSeed] Loading fixtures from #{fixtures_path}"
20
+ private
20
21
 
21
- ActiveRecord::Base.transaction do
22
- with_foreign_keys_disabled do
23
- load_fixture_files(fixtures_dir, logger)
24
- end
25
- end
22
+ def fixtures_directory_exists?(fixtures_dir, fixtures_path)
23
+ return true if Dir.exist?(fixtures_dir)
26
24
 
27
- logger.info "[FixtureSeed] Finished loading fixtures"
25
+ Rails.logger&.info "[FixtureSeed] Fixtures directory not found: #{fixtures_path}"
26
+ false
28
27
  end
29
28
 
30
- private
29
+ def load_fixture_files(fixtures_dir, fixtures_path)
30
+ Rails.logger&.info "[FixtureSeed] Loading fixtures from #{fixtures_path}"
31
31
 
32
- def load_fixture_files(fixtures_dir, logger)
33
32
  fixture_files = Dir.glob("#{fixtures_dir}/*.yml")
34
33
  return if fixture_files.empty?
35
34
 
36
35
  table_names = fixture_files.map { |f| File.basename(f, ".yml") }
37
- logger.info "[FixtureSeed] Found tables: #{table_names.join(', ')}"
36
+ Rails.logger&.info "[FixtureSeed] Found tables: #{table_names.join(', ')}"
38
37
 
39
- table_names.each do |table_name|
40
- ActiveRecord::FixtureSet.create_fixtures(fixtures_dir.to_s, [table_name])
38
+ # Check if FixtureSet is available
39
+ unless defined?(ActiveRecord::FixtureSet)
40
+ Rails.logger&.error "[FixtureSeed] ActiveRecord::FixtureSet is not available. Please ensure you're using Rails 4.0+ or include 'active_record/fixtures'."
41
+ return
41
42
  end
42
- end
43
43
 
44
- def with_foreign_keys_disabled
45
- adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
46
-
47
- disable_foreign_keys(adapter_name)
48
- yield
49
- ensure
50
- enable_foreign_keys(adapter_name)
51
- end
52
-
53
- def disable_foreign_keys(adapter_name)
54
- case adapter_name
55
- when /postgresql/
56
- ActiveRecord::Base.connection.execute("SET session_replication_role = replica;")
57
- when /sqlite/
58
- ActiveRecord::Base.connection.execute("PRAGMA foreign_keys = OFF;")
59
- when /mysql/
60
- ActiveRecord::Base.connection.execute("SET FOREIGN_KEY_CHECKS = 0;")
44
+ ActiveRecord::Base.connection.disable_referential_integrity do
45
+ ActiveRecord::FixtureSet.create_fixtures(fixtures_dir.to_s, table_names)
61
46
  end
62
- end
63
47
 
64
- def enable_foreign_keys(adapter_name)
65
- case adapter_name
66
- when /postgresql/
67
- ActiveRecord::Base.connection.execute("SET session_replication_role = DEFAULT;")
68
- when /sqlite/
69
- ActiveRecord::Base.connection.execute("PRAGMA foreign_keys = ON;")
70
- when /mysql/
71
- ActiveRecord::Base.connection.execute("SET FOREIGN_KEY_CHECKS = 1;")
72
- end
48
+ Rails.logger&.info "[FixtureSeed] Finished loading fixtures"
73
49
  end
74
50
  end
75
51
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FixtureSeed
4
- VERSION = "0.3.3"
4
+ VERSION = "0.4.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixture_seed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masaki Komagata