fixture_seed 0.3.2 → 0.4.0

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: d9b6507b1b52b920c3bfe48360d9cd3ea5238dfb34acea9885870ca689453976
4
- data.tar.gz: 97c87834365464edc70e06bf26aafc21f973316f6837d406d93776654f02c99f
3
+ metadata.gz: ebd3fa8a23bf3af3fb3a2d9fecac6e345487bf092108f11fc0a8cb045a09afaa
4
+ data.tar.gz: c8583cf26d2ac182be6a188c1d37952f18f12526c5b0e8337df971e87f85b3e2
5
5
  SHA512:
6
- metadata.gz: 2eae6c110eb7fc6b71304beba4a71ee8fe13395f7f3a7ee66841337c77fad1e3d38d096f3724b18f7390353c987ba8fdb32e44ba2e20349e2029d5fb30bd82ad
7
- data.tar.gz: ed10aeece08fac55ee820a80bf0a34e15acd7a9298d24b06aa87676541e87d7bcc6990a48c518e1e4ae6309fa876ff360bce7aed04517aa1edc94576ff3d1af0
6
+ metadata.gz: 7361848bfd75c8f13850490a284e552c35858438d39d7394ca2c7f8b12d7595adc6fffe4e8bebac3af21a610bf43fea0c5ba90a1b0add874faeb54144ae39a99
7
+ data.tar.gz: 573e7b22694aaa5d3b05892e05a53692ff291f61dcc70c2756e453e75b660d1024f351a7e3106ff5a923da287a5f87ed47eb1a3e941973bd17f002b40193dc07
@@ -4,72 +4,41 @@ require "active_record"
4
4
 
5
5
  module FixtureSeed
6
6
  class Loader
7
+ DEFAULT_FIXTURES_PATH = "db/fixtures"
8
+
7
9
  class << self
8
10
  def load_fixtures(fixtures_path = nil)
9
- fixtures_path ||= ENV["FIXTURES_PATH"] || "db/fixtures"
11
+ fixtures_path ||= ENV["FIXTURES_PATH"] || DEFAULT_FIXTURES_PATH
10
12
  fixtures_dir = Rails.root.join(fixtures_path)
11
13
 
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
14
+ return unless fixtures_directory_exists?(fixtures_dir, fixtures_path)
17
15
 
18
- logger = Rails.logger || Logger.new($stdout)
19
- logger.info "[FixtureSeed] Loading fixtures from #{fixtures_path}"
16
+ load_fixture_files(fixtures_dir, fixtures_path)
17
+ end
20
18
 
21
- ActiveRecord::Base.transaction do
22
- with_foreign_keys_disabled do
23
- load_fixture_files(fixtures_dir, logger)
24
- end
25
- end
19
+ private
20
+
21
+ def fixtures_directory_exists?(fixtures_dir, fixtures_path)
22
+ return true if Dir.exist?(fixtures_dir)
26
23
 
27
- logger.info "[FixtureSeed] Finished loading fixtures"
24
+ Rails.logger&.info "[FixtureSeed] Fixtures directory not found: #{fixtures_path}"
25
+ false
28
26
  end
29
27
 
30
- private
28
+ def load_fixture_files(fixtures_dir, fixtures_path)
29
+ Rails.logger&.info "[FixtureSeed] Loading fixtures from #{fixtures_path}"
31
30
 
32
- def load_fixture_files(fixtures_dir, logger)
33
31
  fixture_files = Dir.glob("#{fixtures_dir}/*.yml")
34
32
  return if fixture_files.empty?
35
33
 
36
34
  table_names = fixture_files.map { |f| File.basename(f, ".yml") }
37
- logger.info "[FixtureSeed] Found tables: #{table_names.join(', ')}"
35
+ Rails.logger&.info "[FixtureSeed] Found tables: #{table_names.join(', ')}"
38
36
 
39
- table_names.each do |table_name|
40
- ActiveRecord::FixtureSet.create_fixtures(fixtures_dir.to_s, [table_name])
37
+ ActiveRecord::Base.connection.disable_referential_integrity do
38
+ ActiveRecord::FixtureSet.create_fixtures(fixtures_dir.to_s, table_names)
41
39
  end
42
- end
43
40
 
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;")
61
- end
62
- end
63
-
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
41
+ Rails.logger&.info "[FixtureSeed] Finished loading fixtures"
73
42
  end
74
43
  end
75
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FixtureSeed
4
- VERSION = "0.3.2"
4
+ VERSION = "0.4.0"
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.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masaki Komagata