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 +4 -4
- data/lib/fixture_seed/loader.rb +18 -49
- 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: ebd3fa8a23bf3af3fb3a2d9fecac6e345487bf092108f11fc0a8cb045a09afaa
|
4
|
+
data.tar.gz: c8583cf26d2ac182be6a188c1d37952f18f12526c5b0e8337df971e87f85b3e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7361848bfd75c8f13850490a284e552c35858438d39d7394ca2c7f8b12d7595adc6fffe4e8bebac3af21a610bf43fea0c5ba90a1b0add874faeb54144ae39a99
|
7
|
+
data.tar.gz: 573e7b22694aaa5d3b05892e05a53692ff291f61dcc70c2756e453e75b660d1024f351a7e3106ff5a923da287a5f87ed47eb1a3e941973bd17f002b40193dc07
|
data/lib/fixture_seed/loader.rb
CHANGED
@@ -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"] ||
|
11
|
+
fixtures_path ||= ENV["FIXTURES_PATH"] || DEFAULT_FIXTURES_PATH
|
10
12
|
fixtures_dir = Rails.root.join(fixtures_path)
|
11
13
|
|
12
|
-
unless
|
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
|
-
|
19
|
-
|
16
|
+
load_fixture_files(fixtures_dir, fixtures_path)
|
17
|
+
end
|
20
18
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
24
|
+
Rails.logger&.info "[FixtureSeed] Fixtures directory not found: #{fixtures_path}"
|
25
|
+
false
|
28
26
|
end
|
29
27
|
|
30
|
-
|
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
|
35
|
+
Rails.logger&.info "[FixtureSeed] Found tables: #{table_names.join(', ')}"
|
38
36
|
|
39
|
-
|
40
|
-
ActiveRecord::FixtureSet.create_fixtures(fixtures_dir.to_s,
|
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
|
-
|
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
|
data/lib/fixture_seed/version.rb
CHANGED