fixture_seed 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4b756a4004e83e2c69aac4a24fc69391e646fafa774d79b547b65b8d6fa58df7
4
+ data.tar.gz: 7790869a088bd54a43a5881da34885c7b79c9ea0360c6601ef0314e34d925067
5
+ SHA512:
6
+ metadata.gz: 70a2b5e3514bef02dccec7ad3ecb97b7cc7742236838531ba7688b8f54884c3b57cd796dbea9828ac261453ba896c308088b9a308f2cdbc49fce1e406247961e
7
+ data.tar.gz: 469c9859bd06c963550e6426df5c6cf2805f795abacf3a56863b6b695302d5a2fb8edc07b1c4d87770f2f51e5721693a1e5e530ba6cd8efec36e7afcf6168294
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-05-11
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Devin AI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # FixtureSeed
2
+
3
+ FixtureSeed is a Rails gem that automatically loads YAML fixture files from the `db/fixtures/` directory when running `rails db:seed`. It loads the fixtures in alphabetical order and handles foreign key constraint errors by retrying failed inserts after all other fixtures are loaded.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fixture_seed'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Once the gem is installed, it will automatically hook into Rails' `db:seed` task. You don't need to modify your `seeds.rb` file.
22
+
23
+ ### Directory Structure
24
+
25
+ Place your YAML fixture files in the `db/fixtures/` directory:
26
+
27
+ ```
28
+ db/
29
+ fixtures/
30
+ posts.yml
31
+ users.yml
32
+ ```
33
+
34
+ ### Fixture Format
35
+
36
+ The fixture files should be named after the table they correspond to (e.g., `users.yml` for the `users` table).
37
+
38
+ The content of the fixture files should follow this format:
39
+
40
+ ```yaml
41
+ # users.yml
42
+ user1:
43
+ id: 1
44
+ name: "John Doe"
45
+ email: "john@example.com"
46
+
47
+ user2:
48
+ id: 2
49
+ name: "Jane Smith"
50
+ email: "jane@example.com"
51
+ ```
52
+
53
+ The labels (e.g., `user1`, `user2`) should follow the pattern of the table name in singular form followed by a number.
54
+
55
+ ### Loading Order
56
+
57
+ Fixtures are loaded in alphabetical order by filename. However, if a fixture fails to load due to foreign key constraints, it will be retried after all other fixtures have been processed.
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/komagata/fixture_seed.
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FixtureSeed
4
+ class Railtie < Rails::Railtie
5
+ initializer "fixture_seed.initialize" do
6
+ Rails.logger.info "[FixtureSeed] Railtie initialized"
7
+ end
8
+
9
+ rake_tasks do
10
+ if Rake::Task.task_defined?("db:seed")
11
+ Rake::Task["db:seed"].enhance do
12
+ Rails.logger.info "[FixtureSeed] Starting to load fixtures through db:seed enhancement"
13
+ FixtureSeed.load_fixtures
14
+ Rails.logger.info "[FixtureSeed] Finished loading fixtures"
15
+ end
16
+ else
17
+ Rails.logger.warn "[FixtureSeed] db:seed task not found, fixture loading enhancement not applied"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FixtureSeed
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "fixture_seed/version"
4
+ require_relative "fixture_seed/railtie" if defined?(Rails::Railtie)
5
+
6
+ module FixtureSeed
7
+ class Error < StandardError; end
8
+
9
+ class << self
10
+ def load_fixtures
11
+ fixture_dir = Rails.root.join("db/fixtures").to_s
12
+ files_to_load = Dir.glob(File.join(fixture_dir, "*.yml")).sort
13
+
14
+ file_names = files_to_load.map { |f| File.basename(f) }.join(", ")
15
+ Rails.logger.info "[FixtureSeed] Found #{files_to_load.size} fixture files in #{fixture_dir}: #{file_names}"
16
+
17
+ # Load all fixtures with retry logic
18
+ unloaded_files = process_fixtures(fixture_dir, files_to_load)
19
+
20
+ # Report any fixtures that still failed
21
+ if unloaded_files.empty?
22
+ Rails.logger.info "[FixtureSeed] All fixtures loaded successfully."
23
+ else
24
+ message = "The following fixtures could not be loaded: #{unloaded_files.join(', ')}"
25
+ Rails.logger.warn "[FixtureSeed] #{message}"
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def process_fixtures(fixture_dir, files)
32
+ remaining_files = files.dup
33
+ previous_count = remaining_files.size + 1
34
+
35
+ # Continue until all fixtures are loaded or no progress is made
36
+ while !remaining_files.empty? && remaining_files.size < previous_count
37
+ previous_count = remaining_files.size
38
+ remaining_files = process_batch(fixture_dir, files, remaining_files)
39
+ end
40
+
41
+ remaining_files
42
+ end
43
+
44
+ def process_batch(fixture_dir, original_files, current_files)
45
+ still_failed = []
46
+
47
+ current_files.each do |file|
48
+ log_loading(file)
49
+ load_single_fixture(fixture_dir, file)
50
+ log_success(file, original_files, current_files)
51
+ rescue ActiveRecord::InvalidForeignKey, ActiveRecord::StatementInvalid => e
52
+ log_failure(file, e, original_files, current_files)
53
+ still_failed << file
54
+ end
55
+
56
+ still_failed
57
+ end
58
+
59
+ def load_single_fixture(_fixture_dir, file)
60
+ table_name = File.basename(file, ".yml")
61
+ yaml_data = YAML.load_file(file)
62
+
63
+ model_class = begin
64
+ table_name.classify.constantize
65
+ rescue StandardError
66
+ nil
67
+ end
68
+
69
+ if model_class
70
+ yaml_data.each do |_fixture_name, attributes|
71
+ model_class.create!(attributes)
72
+ rescue StandardError => e
73
+ raise ActiveRecord::InvalidForeignKey, e.message
74
+ end
75
+ else
76
+ Rails.logger.warn "[FixtureSeed] Model for table #{table_name} not found"
77
+ end
78
+ end
79
+
80
+ def log_loading(file)
81
+ table_name = File.basename(file, ".yml")
82
+ Rails.logger.info "[FixtureSeed] Loading fixture #{File.basename(file)}..."
83
+ end
84
+
85
+ def log_success(file, original_files, current_files)
86
+ is_retry = original_files.size != current_files.size
87
+ if is_retry
88
+ Rails.logger.info "[FixtureSeed] Successfully loaded fixture #{File.basename(file)} on retry."
89
+ else
90
+ Rails.logger.info "[FixtureSeed] Loaded fixture #{File.basename(file)}"
91
+ end
92
+ end
93
+
94
+ def log_failure(file, error, original_files, current_files)
95
+ is_retry = original_files.size != current_files.size
96
+ filename = File.basename(file)
97
+ if is_retry
98
+ Rails.logger.warn "[FixtureSeed] Still failed to load fixture #{filename}: " \
99
+ "#{error.message}. Will retry later."
100
+ else
101
+ Rails.logger.warn "[FixtureSeed] Failed to load fixture #{filename}: " \
102
+ "#{error.message}. Will retry later."
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,4 @@
1
+ module FixtureSeed
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fixture_seed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masaki Komagata
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '5.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '5.0'
26
+ description: fixture_seed is a Rails gem that automatically loads YAML fixtures from
27
+ db/fixtures directory in alphabetical order when running rails db:seed, with error
28
+ handling for foreign key constraints.
29
+ email:
30
+ - komagata@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - CHANGELOG.md
36
+ - LICENSE.txt
37
+ - README.md
38
+ - lib/fixture_seed.rb
39
+ - lib/fixture_seed/railtie.rb
40
+ - lib/fixture_seed/version.rb
41
+ - sig/fixture_seed.rbs
42
+ homepage: https://github.com/komagata/fixture_seed
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ homepage_uri: https://github.com/komagata/fixture_seed
47
+ source_code_uri: https://github.com/komagata/fixture_seed
48
+ changelog_uri: https://github.com/komagata/fixture_seed/blob/main/CHANGELOG.md
49
+ rubygems_mfa_required: 'true'
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.6.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.6.7
65
+ specification_version: 4
66
+ summary: A Rails gem to automatically load fixtures from db/fixtures directory during
67
+ db:seed
68
+ test_files: []