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 +4 -4
- data/lib/fixture_seed/loader.rb +23 -47
- data/lib/fixture_seed/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d3cd5453c25f8d850aa253af3a3be318d4f321d67ac8d08a47894bcaebc762a
|
4
|
+
data.tar.gz: 38f74d96fc45637798f95d0ab92948f9e399a6b447e96ea3d515862ebc61424a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eecab5f96b32f9311b2c453c7cdff9a1fc6a0000f0295310022bf3279dec380cb8509d81bc53a9b88d4691d55bd1ecb0ebc34982169d3533f273da7b790a209f
|
7
|
+
data.tar.gz: 3119f7c0827b348723a9534d9740802d47c35be613b4b883ac144837bae7d18983808d86eef1b361cde4d7fc745bdfa85e034d03e38a8823c6695f5824d1c6bc
|
data/lib/fixture_seed/loader.rb
CHANGED
@@ -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"] ||
|
12
|
+
fixtures_path ||= ENV["FIXTURES_PATH"] || DEFAULT_FIXTURES_PATH
|
10
13
|
fixtures_dir = Rails.root.join(fixtures_path)
|
11
14
|
|
12
|
-
unless
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
19
|
-
logger.info "[FixtureSeed] Loading fixtures from #{fixtures_path}"
|
20
|
+
private
|
20
21
|
|
21
|
-
|
22
|
-
|
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
|
25
|
+
Rails.logger&.info "[FixtureSeed] Fixtures directory not found: #{fixtures_path}"
|
26
|
+
false
|
28
27
|
end
|
29
28
|
|
30
|
-
|
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
|
36
|
+
Rails.logger&.info "[FixtureSeed] Found tables: #{table_names.join(', ')}"
|
38
37
|
|
39
|
-
|
40
|
-
|
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
|
-
|
45
|
-
|
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
|
-
|
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
|
data/lib/fixture_seed/version.rb
CHANGED