seed_builder 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c653970b40ff1a4129b1ba9fb9c798ce53286bd1333651942110fa1d32da1819
4
+ data.tar.gz: bd88dfb06863cf199d17e9d03b70ed210c93411b6b1bd8d3e1ad24ceca16293c
5
+ SHA512:
6
+ metadata.gz: 53d483a3dd474f3bbde578f8913da62310a81b9db77f1b52c068dc1ed4c838f0c33d974bdc4dbf86939820b95de95a92527492bbdc8654a718a628e488c86d92
7
+ data.tar.gz: 260dbbb289c1b7e2c2ad65934cbd297dfff1287b3e4cb8d319a843f8efd3ac83117f6eaf4a5c47ea023971332a8e0f044f181f6a1397419971a6347130ebd7b0
data/.editorconfig ADDED
@@ -0,0 +1,14 @@
1
+ # Editor configuration, see https://editorconfig.org
2
+ root = true
3
+
4
+ [*]
5
+ charset = utf-8
6
+ indent_style = space
7
+ indent_size = 2
8
+ insert_final_newline = true
9
+ trim_trailing_whitespace = true
10
+ quote_type = double
11
+
12
+ [*.md]
13
+ max_line_length = off
14
+ trim_trailing_whitespace = false
@@ -0,0 +1,35 @@
1
+ name: test
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ rspec:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby: [ ruby, jruby, truffleruby ]
11
+ steps:
12
+ - uses: actions/checkout@v3
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: ${{ matrix.ruby }}
16
+ bundler-cache: true
17
+ - run: bundle exec rspec
18
+
19
+ coverage:
20
+ runs-on: ubuntu-latest
21
+ steps:
22
+ - uses: actions/checkout@v3
23
+ - uses: ruby/setup-ruby@v1
24
+ with:
25
+ bundler-cache: true
26
+ - run: bundle exec rspec
27
+ - uses: codecov/codecov-action@v5
28
+ continue-on-error: true
29
+ env:
30
+ CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
31
+ - uses: codacy/codacy-coverage-reporter-action@v1.3.0
32
+ continue-on-error: true
33
+ with:
34
+ project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
35
+ coverage-reports: "coverage/coverage.xml"
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+ /vendor/bundle
10
+
11
+ .byebug_history
12
+
13
+ /coverage
14
+ /coverage/*
15
+
16
+ /spec/examples.txt
17
+ /tmp/*
18
+
19
+ .DS_Store
20
+ .idea
21
+ .lh
22
+
23
+ *.local
24
+ *.gem
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.4
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 1.0.0
2
+
3
+ * Initial version
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,258 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ seed_builder (1.0.0)
5
+ activerecord (> 5)
6
+ activesupport (> 5)
7
+ rails (> 5)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actioncable (8.0.0)
13
+ actionpack (= 8.0.0)
14
+ activesupport (= 8.0.0)
15
+ nio4r (~> 2.0)
16
+ websocket-driver (>= 0.6.1)
17
+ zeitwerk (~> 2.6)
18
+ actionmailbox (8.0.0)
19
+ actionpack (= 8.0.0)
20
+ activejob (= 8.0.0)
21
+ activerecord (= 8.0.0)
22
+ activestorage (= 8.0.0)
23
+ activesupport (= 8.0.0)
24
+ mail (>= 2.8.0)
25
+ actionmailer (8.0.0)
26
+ actionpack (= 8.0.0)
27
+ actionview (= 8.0.0)
28
+ activejob (= 8.0.0)
29
+ activesupport (= 8.0.0)
30
+ mail (>= 2.8.0)
31
+ rails-dom-testing (~> 2.2)
32
+ actionpack (8.0.0)
33
+ actionview (= 8.0.0)
34
+ activesupport (= 8.0.0)
35
+ nokogiri (>= 1.8.5)
36
+ rack (>= 2.2.4)
37
+ rack-session (>= 1.0.1)
38
+ rack-test (>= 0.6.3)
39
+ rails-dom-testing (~> 2.2)
40
+ rails-html-sanitizer (~> 1.6)
41
+ useragent (~> 0.16)
42
+ actiontext (8.0.0)
43
+ actionpack (= 8.0.0)
44
+ activerecord (= 8.0.0)
45
+ activestorage (= 8.0.0)
46
+ activesupport (= 8.0.0)
47
+ globalid (>= 0.6.0)
48
+ nokogiri (>= 1.8.5)
49
+ actionview (8.0.0)
50
+ activesupport (= 8.0.0)
51
+ builder (~> 3.1)
52
+ erubi (~> 1.11)
53
+ rails-dom-testing (~> 2.2)
54
+ rails-html-sanitizer (~> 1.6)
55
+ activejob (8.0.0)
56
+ activesupport (= 8.0.0)
57
+ globalid (>= 0.3.6)
58
+ activemodel (8.0.0)
59
+ activesupport (= 8.0.0)
60
+ activerecord (8.0.0)
61
+ activemodel (= 8.0.0)
62
+ activesupport (= 8.0.0)
63
+ timeout (>= 0.4.0)
64
+ activestorage (8.0.0)
65
+ actionpack (= 8.0.0)
66
+ activejob (= 8.0.0)
67
+ activerecord (= 8.0.0)
68
+ activesupport (= 8.0.0)
69
+ marcel (~> 1.0)
70
+ activesupport (8.0.0)
71
+ base64
72
+ benchmark (>= 0.3)
73
+ bigdecimal
74
+ concurrent-ruby (~> 1.0, >= 1.3.1)
75
+ connection_pool (>= 2.2.5)
76
+ drb
77
+ i18n (>= 1.6, < 2)
78
+ logger (>= 1.4.2)
79
+ minitest (>= 5.1)
80
+ securerandom (>= 0.3)
81
+ tzinfo (~> 2.0, >= 2.0.5)
82
+ uri (>= 0.13.1)
83
+ base64 (0.2.0)
84
+ benchmark (0.4.0)
85
+ bigdecimal (3.1.8)
86
+ bigdecimal (3.1.8-java)
87
+ builder (3.3.0)
88
+ concurrent-ruby (1.3.4)
89
+ connection_pool (2.4.1)
90
+ crass (1.0.6)
91
+ date (3.4.1)
92
+ date (3.4.1-java)
93
+ diff-lcs (1.5.1)
94
+ docile (1.4.1)
95
+ drb (2.2.1)
96
+ erubi (1.13.0)
97
+ globalid (1.2.1)
98
+ activesupport (>= 6.1)
99
+ i18n (1.14.6)
100
+ concurrent-ruby (~> 1.0)
101
+ io-console (0.8.0)
102
+ io-console (0.8.0-java)
103
+ irb (1.14.1)
104
+ rdoc (>= 4.0.0)
105
+ reline (>= 0.4.2)
106
+ jar-dependencies (0.5.0)
107
+ logger (1.6.2)
108
+ loofah (2.23.1)
109
+ crass (~> 1.0.2)
110
+ nokogiri (>= 1.12.0)
111
+ mail (2.8.1)
112
+ mini_mime (>= 0.1.1)
113
+ net-imap
114
+ net-pop
115
+ net-smtp
116
+ marcel (1.0.4)
117
+ mini_mime (1.1.5)
118
+ mini_portile2 (2.8.8)
119
+ minitest (5.25.4)
120
+ net-imap (0.5.1)
121
+ date
122
+ net-protocol
123
+ net-pop (0.1.2)
124
+ net-protocol
125
+ net-protocol (0.2.2)
126
+ timeout
127
+ net-smtp (0.5.0)
128
+ net-protocol
129
+ nio4r (2.7.4)
130
+ nio4r (2.7.4-java)
131
+ nokogiri (1.16.8-aarch64-linux)
132
+ racc (~> 1.4)
133
+ nokogiri (1.16.8-arm-linux)
134
+ racc (~> 1.4)
135
+ nokogiri (1.16.8-arm64-darwin)
136
+ racc (~> 1.4)
137
+ nokogiri (1.16.8-java)
138
+ racc (~> 1.4)
139
+ nokogiri (1.16.8-x86-linux)
140
+ racc (~> 1.4)
141
+ nokogiri (1.16.8-x86_64-darwin)
142
+ racc (~> 1.4)
143
+ nokogiri (1.16.8-x86_64-linux)
144
+ racc (~> 1.4)
145
+ psych (5.2.1)
146
+ date
147
+ stringio
148
+ psych (5.2.1-java)
149
+ date
150
+ jar-dependencies (>= 0.1.7)
151
+ racc (1.8.1)
152
+ racc (1.8.1-java)
153
+ rack (3.1.8)
154
+ rack-session (2.0.0)
155
+ rack (>= 3.0.0)
156
+ rack-test (2.1.0)
157
+ rack (>= 1.3)
158
+ rackup (2.2.1)
159
+ rack (>= 3)
160
+ rails (8.0.0)
161
+ actioncable (= 8.0.0)
162
+ actionmailbox (= 8.0.0)
163
+ actionmailer (= 8.0.0)
164
+ actionpack (= 8.0.0)
165
+ actiontext (= 8.0.0)
166
+ actionview (= 8.0.0)
167
+ activejob (= 8.0.0)
168
+ activemodel (= 8.0.0)
169
+ activerecord (= 8.0.0)
170
+ activestorage (= 8.0.0)
171
+ activesupport (= 8.0.0)
172
+ bundler (>= 1.15.0)
173
+ railties (= 8.0.0)
174
+ rails-dom-testing (2.2.0)
175
+ activesupport (>= 5.0.0)
176
+ minitest
177
+ nokogiri (>= 1.6)
178
+ rails-html-sanitizer (1.6.1)
179
+ loofah (~> 2.21)
180
+ nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
181
+ railties (8.0.0)
182
+ actionpack (= 8.0.0)
183
+ activesupport (= 8.0.0)
184
+ irb (~> 1.13)
185
+ rackup (>= 1.0.0)
186
+ rake (>= 12.2)
187
+ thor (~> 1.0, >= 1.2.2)
188
+ zeitwerk (~> 2.6)
189
+ rake (13.2.1)
190
+ rdoc (6.8.1)
191
+ psych (>= 4.0.0)
192
+ reline (0.5.12)
193
+ io-console (~> 0.5)
194
+ rexml (3.3.9)
195
+ rspec (3.13.0)
196
+ rspec-core (~> 3.13.0)
197
+ rspec-expectations (~> 3.13.0)
198
+ rspec-mocks (~> 3.13.0)
199
+ rspec-core (3.13.2)
200
+ rspec-support (~> 3.13.0)
201
+ rspec-expectations (3.13.3)
202
+ diff-lcs (>= 1.2.0, < 2.0)
203
+ rspec-support (~> 3.13.0)
204
+ rspec-mocks (3.13.2)
205
+ diff-lcs (>= 1.2.0, < 2.0)
206
+ rspec-support (~> 3.13.0)
207
+ rspec-support (3.13.2)
208
+ securerandom (0.4.0)
209
+ simplecov (0.22.0)
210
+ docile (~> 1.1)
211
+ simplecov-html (~> 0.11)
212
+ simplecov_json_formatter (~> 0.1)
213
+ simplecov-cobertura (2.1.0)
214
+ rexml
215
+ simplecov (~> 0.19)
216
+ simplecov-html (0.13.1)
217
+ simplecov_json_formatter (0.1.4)
218
+ sqlite3 (2.4.0)
219
+ mini_portile2 (~> 2.8.0)
220
+ sqlite3 (2.4.0-aarch64-linux-gnu)
221
+ sqlite3 (2.4.0-arm-linux-gnu)
222
+ sqlite3 (2.4.0-arm64-darwin)
223
+ sqlite3 (2.4.0-x86-linux-gnu)
224
+ sqlite3 (2.4.0-x86_64-darwin)
225
+ sqlite3 (2.4.0-x86_64-linux-gnu)
226
+ stringio (3.1.2)
227
+ thor (1.3.2)
228
+ timeout (0.4.2)
229
+ tzinfo (2.0.6)
230
+ concurrent-ruby (~> 1.0)
231
+ uri (1.0.2)
232
+ useragent (0.16.11)
233
+ websocket-driver (0.7.6)
234
+ websocket-extensions (>= 0.1.0)
235
+ websocket-driver (0.7.6-java)
236
+ websocket-extensions (>= 0.1.0)
237
+ websocket-extensions (0.1.5)
238
+ zeitwerk (2.7.1)
239
+
240
+ PLATFORMS
241
+ aarch64-linux
242
+ arm-linux
243
+ arm64-darwin
244
+ universal-java-11
245
+ x86-linux
246
+ x86_64-darwin
247
+ x86_64-linux
248
+
249
+ DEPENDENCIES
250
+ bundler (~> 2)
251
+ rspec (~> 3)
252
+ seed_builder!
253
+ simplecov (~> 0.21)
254
+ simplecov-cobertura (~> 2)
255
+ sqlite3 (~> 2.4)
256
+
257
+ BUNDLED WITH
258
+ 2.5.20
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 amkisko
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # seed_builder.rb
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/seed_builder.svg)](https://badge.fury.io/rb/seed_builder) [![Test Status](https://github.com/amkisko/seed_builder.rb/actions/workflows/test.yml/badge.svg)](https://github.com/amkisko/seed_builder.rb/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/amkisko/seed_builder.rb/graph/badge.svg?token=57R6OHOJDQ)](https://codecov.io/gh/amkisko/seed_builder.rb)
4
+
5
+ Seed builder for ActiveRecord. Includes seeds loader and generator.
6
+
7
+ Sponsored by [Kisko Labs](https://www.kiskolabs.com).
8
+
9
+ ## Install
10
+
11
+ Using Bundler:
12
+ ```sh
13
+ bundle add seed_builder
14
+ ```
15
+
16
+ Using RubyGems:
17
+ ```sh
18
+ gem install seed_builder
19
+ ```
20
+
21
+ ## Gemfile
22
+
23
+ ```ruby
24
+ group :development do
25
+ gem "seed_builder"
26
+ end
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ### Run seeds
32
+
33
+ ```sh
34
+ bin/rails db:seed
35
+ ```
36
+
37
+ ### Generate seed file
38
+
39
+ ```sh
40
+ bin/rails g seed create_users
41
+ ```
42
+
43
+ ## Configuration
44
+
45
+ ### Load default seeds.rb file
46
+
47
+ By default seed file `db/seeds.rb` is loaded.
48
+
49
+ To turn off loading default seeds.rb file:
50
+
51
+ ```ruby
52
+ SeedBuilder.config.load_default_seeds = false
53
+ ```
54
+
55
+ ### Set seed directory
56
+
57
+ Absolute path will be resolved by using `Rails.root`.
58
+
59
+ ```ruby
60
+ SeedBuilder.config.seeds_relative_path = "db/seeds"
61
+ ```
62
+
63
+ ### Turn off test script generation
64
+
65
+ ```ruby
66
+ SeedBuilder.config.generate_spec = false
67
+ ```
68
+
69
+ ## Specification checklist
70
+
71
+ - [x] User can generate seed file under `db/seeds` directory with common format
72
+ - [x] User can generate seed file with test script included
73
+ - [x] User can run all seeds
74
+ - [ ] User can run specific seed file
75
+
76
+ ## Limitations & explanations
77
+
78
+ - Gem patches `ActiveRecord::Tasks::DatabaseTasks.seed_loader` to have custom loader
79
+ - ActiveRecord migrations generator is used to generate seed files
80
+ - Seeded data is not reversible, there is no point to implement it as logic can be complex and operations on data might lead to irreversible changes
81
+ - Seed file is not a migration, although as good practice is to keep it idempotent, e.g. by checking uniqueness of seeded records
82
+
83
+ ## Contributing
84
+
85
+ Bug reports and pull requests are welcome on GitHub at https://github.com/amkisko/seed_builder.rb
86
+
87
+ Contribution policy:
88
+ - New features are not necessarily added to the gem
89
+ - Pull request should have test coverage for affected parts
90
+ - Pull request should have changelog entry
91
+ - It might take up to 2 calendar weeks to review and merge critical fixes
92
+ - It might take up to 6 calendar months to review and merge pull request
93
+ - It might take up to 1 calendar year to review an issue
94
+
95
+ ## Publishing
96
+
97
+ Prefer using script `usr/bin/release.sh`, it will ensure that repository is synced and after publishing gem will create a tag.
98
+
99
+ ```sh
100
+ GEM_VERSION=$(grep -Eo "VERSION\s*=\s*\".+\"" lib/seed_builder.rb | grep -Eo "[0-9.]{5,}")
101
+ rm seed_builder-*.gem
102
+ gem build seed_builder.gemspec
103
+ gem push seed_builder-$GEM_VERSION.gem
104
+ git tag $GEM_VERSION && git push --tags
105
+ ```
106
+
107
+ ## License
108
+
109
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,13 @@
1
+ require "rails/generators"
2
+ require "rails/generators/active_record"
3
+ require "rails/generators/active_record/migration/migration_generator"
4
+
5
+ class SeedGenerator < ActiveRecord::Generators::MigrationGenerator
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def create_migration_file
9
+ set_local_assigns!
10
+ validate_file_name!
11
+ migration_template "seed.rb", "#{SeedBuilder.config.seeds_path}/#{file_name}.rb"
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ class <%= migration_class_name %>
2
+ def change
3
+
4
+ end
5
+ end
6
+ <%- if SeedBuilder.config.generate_spec? %>
7
+
8
+ # TESTING:
9
+ # - bin/rspec <%= SeedBuilder.config.seeds_relative_path %>/<%= migration_number %>_<%= migration_file_name %>.rb
10
+ if Rails.env.test? && Object.const_defined?(:RSpec)
11
+ require "rails_helper"
12
+
13
+ RSpec.describe <%= migration_class_name %>, type: :seed do
14
+ subject(:change) { described_class.new.change }
15
+
16
+ it do
17
+ expect { change }.not_to raise_error
18
+ end
19
+ end
20
+ end
21
+ <%- end %>
@@ -0,0 +1,3 @@
1
+ if Object.const_defined?(:ActiveRecord)
2
+ ActiveRecord::Tasks::DatabaseTasks.seed_loader = SeedBuilder::Loader.new
3
+ end
@@ -0,0 +1,37 @@
1
+ module SeedBuilder
2
+ class Config
3
+ attr_writer :default_seeds_path
4
+ def default_seeds_path
5
+ @default_seeds_path ||= Rails.root.join("db/seeds.rb")
6
+ end
7
+
8
+ attr_writer :seeds_path
9
+ def seeds_path
10
+ @seeds_path ||= Rails.root.join(seeds_relative_path)
11
+ end
12
+
13
+ def seeds_path_glob
14
+ "#{seeds_path}/*.rb"
15
+ end
16
+
17
+ attr_writer :seeds_relative_path
18
+ def seeds_relative_path
19
+ @seeds_relative_path ||= "db/seeds"
20
+ end
21
+
22
+ attr_writer :load_default_seeds
23
+ def load_default_seeds?
24
+ @load_default_seeds ||= true
25
+ end
26
+
27
+ attr_writer :load_seeds
28
+ def load_seeds?
29
+ @load_seeds ||= true
30
+ end
31
+
32
+ attr_writer :generate_spec
33
+ def generate_spec?
34
+ @generate_spec ||= true
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,47 @@
1
+ module SeedBuilder
2
+ class Loader
3
+ def load_seed
4
+ initial_logger = Rails.logger
5
+
6
+ Rails.logger = Logger.new($stdout)
7
+ Rails.logger.level = Logger::INFO
8
+
9
+ ActiveRecord::Base.connection.schema_cache.clear!
10
+ ActiveRecord::Base.descendants.each(&:reset_column_information)
11
+
12
+ default_seeds_rb = SeedBuilder.config.default_seeds_path
13
+ if File.exist?(default_seeds_rb) && SeedBuilder.config.load_default_seeds?
14
+ require default_seeds_rb
15
+ end
16
+
17
+ base_path = SeedBuilder.config.seeds_path
18
+ if File.exist?(base_path) && SeedBuilder.config.load_seeds?
19
+ Dir[SeedBuilder.config.seeds_path_glob]
20
+ .map { |f| File.basename(f, ".*") }
21
+ .each do |seed_path|
22
+ require "#{base_path}/#{seed_path}"
23
+ timestamp, klass_name = seed_path.scan(/^([0-9]+)_(.+)$/).first
24
+ next if klass_name.blank?
25
+
26
+ klass = klass_name.camelize.constantize
27
+ puts "== #{timestamp} #{klass.name}: seeding"
28
+ started_at = Time.current
29
+ seed_instance = klass.new
30
+ if seed_instance.respond_to?(:change)
31
+ seed_instance.change
32
+ else
33
+ raise "Seed #{klass.name} does not respond to :change"
34
+ end
35
+ puts "== #{timestamp} #{klass.name}: seeded (#{(Time.current - started_at).round(4)}s)"
36
+ rescue ActiveRecord::RecordInvalid => e
37
+ puts "Seeding #{klass.name} failed: #{e.record.errors.full_messages}"
38
+ raise e
39
+ end
40
+ else
41
+ puts "Seed directory #{base_path} does not exist"
42
+ end
43
+
44
+ Rails.logger = initial_logger
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,14 @@
1
+ require "seed_builder/config"
2
+ require "seed_builder/loader"
3
+
4
+ module SeedBuilder
5
+ VERSION = "1.0.0".freeze
6
+
7
+ def self.config
8
+ @config ||= Config.new
9
+ end
10
+
11
+ def self.configure
12
+ yield config
13
+ end
14
+ end
@@ -0,0 +1,42 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "seed_builder"
3
+ gem.version = File.read(File.expand_path("../lib/seed_builder.rb", __FILE__)).match(/VERSION\s*=\s*"(.*?)"/)[1]
4
+
5
+ repository_url = "https://github.com/amkisko/seed_builder.rb"
6
+ root_files = %w[CHANGELOG.md LICENSE.md README.md]
7
+ root_files << "#{gem.name}.gemspec"
8
+
9
+ gem.license = "MIT"
10
+
11
+ gem.platform = Gem::Platform::RUBY
12
+
13
+ gem.authors = ["Andrei Makarov"]
14
+ gem.email = ["andrei@kiskolabs.com"]
15
+ gem.homepage = repository_url
16
+ gem.summary = "Seed builder with loader and generator"
17
+ gem.description = gem.summary
18
+ gem.metadata = {
19
+ "homepage" => repository_url,
20
+ "source_code_uri" => repository_url,
21
+ "bug_tracker_uri" => "#{repository_url}/issues",
22
+ "changelog_uri" => "#{repository_url}/blob/main/CHANGELOG.md",
23
+ "rubygems_mfa_required" => "true"
24
+ }
25
+
26
+ gem.files = `git ls-files`.split("\n")
27
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
28
+ gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
29
+
30
+ gem.required_ruby_version = ">= 3"
31
+ gem.require_paths = ["lib"]
32
+
33
+ gem.add_dependency "rails", "> 5"
34
+ gem.add_dependency "activerecord", "> 5"
35
+ gem.add_dependency "activesupport", "> 5"
36
+
37
+ gem.add_development_dependency "bundler", "~> 2"
38
+ gem.add_development_dependency "rspec", "~> 3"
39
+ gem.add_development_dependency "simplecov", "~> 0.21"
40
+ gem.add_development_dependency "simplecov-cobertura", "~> 2"
41
+ gem.add_development_dependency "sqlite3", "~> 2.4"
42
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define(version: 2024_12_06_200411) do
2
+ create_table :seed_builder_users, force: true do |t|
3
+ t.string :email
4
+ end
5
+
6
+ create_table :seed_users, force: true do |t|
7
+ t.string :email
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class CreateUsers
2
+ def change
3
+ SeedBuilderUser.find_or_create_by(email: "test@example.com")
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ SeedUser.find_or_create_by(email: "test@example.com")
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ require "generators/seed_generator"
4
+
5
+ describe SeedGenerator, type: :generator do
6
+ include FileUtils
7
+
8
+ subject(:generator) { described_class.start params }
9
+ let(:destination_root) { File.expand_path("../../tmp/rspec", __FILE__) }
10
+
11
+ let(:params) { ["create_users"] }
12
+
13
+ before do
14
+ mkdir_p destination_root
15
+
16
+ allow(Rails).to receive(:root).and_return(rails_root(destination_root))
17
+ end
18
+
19
+ after do
20
+ rm_rf destination_root
21
+ end
22
+
23
+ it "creates a migration file" do
24
+ generator
25
+ seed_files = Dir[rails_root.join("db/seeds/*_create_users.rb")]
26
+ expect(seed_files).not_to be_empty
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ describe SeedBuilder::Config do
4
+ subject(:config) { described_class.new }
5
+
6
+ before do
7
+ allow(Rails).to receive(:root).and_return(rails_root)
8
+ end
9
+
10
+ it "has default seeds path" do
11
+ expect(config.seeds_relative_path).to eq "db/seeds"
12
+ end
13
+
14
+ it "has default load_seeds" do
15
+ expect(config.load_seeds?).to be true
16
+ end
17
+
18
+ it "has default load_default_seeds" do
19
+ expect(config.load_default_seeds?).to be true
20
+ end
21
+
22
+ it "has default generate_spec" do
23
+ expect(config.generate_spec?).to be true
24
+ end
25
+
26
+ it "has default seeds_path" do
27
+ expect(config.seeds_path).to eq Rails.root.join("db/seeds")
28
+ end
29
+
30
+ it "has default seeds_path_glob" do
31
+ expect(config.seeds_path_glob).to eq Rails.root.join("db/seeds/*.rb").to_s
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ class SeedBuilderUser < ActiveRecord::Base; end
4
+ class SeedUser < SeedBuilderUser; end
5
+
6
+ describe SeedBuilder::Loader do
7
+ subject(:loader) { described_class.new }
8
+
9
+ describe "#load_seed" do
10
+ let(:load_default_seeds) { true }
11
+ let(:load_seeds) { true }
12
+ let(:logger) { Logger.new($stdout) }
13
+
14
+ before do
15
+ allow(Rails).to receive(:root).and_return(rails_root)
16
+ allow(Rails).to receive(:env).and_return(rails_env)
17
+ allow(Rails).to receive(:logger).and_return(logger)
18
+ allow(Rails).to receive(:logger=)
19
+
20
+ SeedBuilder.configure do |config|
21
+ config.seeds_path = File.expand_path("../../fixtures/seeds", __FILE__)
22
+ config.default_seeds_path = File.expand_path("../../fixtures/seeds.rb", __FILE__)
23
+ config.load_default_seeds = load_default_seeds
24
+ config.load_seeds = load_seeds
25
+ end
26
+ loader.load_seed
27
+ end
28
+
29
+ it "loads the seeds" do
30
+ expect(SeedUser.count).to eq 1
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe SeedBuilder do
4
+ let(:gem_specification) { Gem::Specification.load(File.expand_path("../../seed_builder.gemspec", __FILE__)) }
5
+
6
+ it "has a version number" do
7
+ expect(described_class::VERSION).to eq gem_specification.version.to_s
8
+ end
9
+
10
+ let(:changelog_file) { File.expand_path("../../CHANGELOG.md", __FILE__) }
11
+ it "has changelog for the version" do
12
+ expect(File.exist?(changelog_file)).to be true
13
+ expect(File.read(changelog_file)).to include("# #{gem_specification.version.to_s}")
14
+ end
15
+
16
+ let(:license_file) { File.expand_path("../../LICENSE.md", __FILE__) }
17
+ it "has license" do
18
+ expect(File.exist?(license_file)).to be true
19
+ end
20
+
21
+ let(:readme_file) { File.expand_path("../../README.md", __FILE__) }
22
+ it "has readme" do
23
+ expect(File.exist?(readme_file)).to be true
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+
3
+ require "simplecov"
4
+ SimpleCov.start do
5
+ add_filter "/spec/"
6
+ add_filter { |source_file| source_file.lines.count < 5 }
7
+ end
8
+
9
+ require "simplecov-cobertura"
10
+ SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
11
+
12
+ require "active_record"
13
+
14
+ require "seed_builder"
15
+
16
+ Dir[File.expand_path("support/**/*.rb", __dir__)].each { |f| require_relative f }
17
+
18
+ RSpec.configure do |config|
19
+ include RailsHelpers
20
+
21
+ config.before(:suite) do
22
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
23
+ load File.expand_path("../fixtures/schema.rb", __FILE__)
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ module RailsHelpers
2
+ def rails_root(destination_root = File.expand_path("../../tmp/rspec", __dir__))
3
+ Class.new do
4
+ def initialize(destination_root)
5
+ @destination_root = destination_root
6
+ end
7
+
8
+ def to_s
9
+ @destination_root
10
+ end
11
+
12
+ def join(*args)
13
+ File.join(@destination_root, *args)
14
+ end
15
+ end.new(destination_root)
16
+ end
17
+
18
+ def rails_env(env = "development")
19
+ Class.new do
20
+ def initialize(env)
21
+ @env = env.to_s
22
+ end
23
+
24
+ def development?
25
+ @env == "development"
26
+ end
27
+
28
+ def test?
29
+ @env == "test"
30
+ end
31
+
32
+ def production?
33
+ @env == "production"
34
+ end
35
+
36
+ def to_s
37
+ @env
38
+ end
39
+ end.new(env)
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ #!/bin/bash
2
+
3
+ set -e
4
+
5
+ e() {
6
+ GREEN='\033[0;32m'
7
+ NC='\033[0m'
8
+ echo -e "${GREEN}$1${NC}"
9
+ eval "$1"
10
+ }
11
+
12
+ e "bundle"
13
+ e "bundle exec rspec"
14
+
15
+ if [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]]; then
16
+ echo -e "\033[1;31mgit working directory not clean, please commit your changes first \033[0m"
17
+ exit 1
18
+ fi
19
+
20
+ GEM_NAME="seed_builder"
21
+ VERSION=$(grep -Eo "VERSION\s*=\s*\".+\"" lib/seed_builder.rb | grep -Eo "[0-9.]{5,}")
22
+ GEM_FILE="$GEM_NAME-$VERSION.gem"
23
+
24
+ e "gem build $GEM_NAME.gemspec"
25
+ e "gem push $GEM_FILE"
26
+
27
+ e "git tag $VERSION && git push --tags"
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seed_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Makarov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: '5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: '5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">"
46
+ - !ruby/object:Gem::Version
47
+ version: '5'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: '5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.21'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.21'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov-cobertura
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.4'
125
+ description: Seed builder with loader and generator
126
+ email:
127
+ - andrei@kiskolabs.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".editorconfig"
133
+ - ".github/workflows/test.yml"
134
+ - ".gitignore"
135
+ - ".ruby-version"
136
+ - CHANGELOG.md
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - LICENSE.md
140
+ - README.md
141
+ - lib/generators/seed_generator.rb
142
+ - lib/generators/templates/seed.rb.tt
143
+ - lib/rails_config.rb
144
+ - lib/seed_builder.rb
145
+ - lib/seed_builder/config.rb
146
+ - lib/seed_builder/loader.rb
147
+ - seed_builder.gemspec
148
+ - spec/fixtures/schema.rb
149
+ - spec/fixtures/seeds.rb
150
+ - spec/fixtures/seeds/20241206200111_create_users.rb
151
+ - spec/generator_spec.rb
152
+ - spec/seed_builder/config_spec.rb
153
+ - spec/seed_builder/loader_spec.rb
154
+ - spec/seed_builder_spec.rb
155
+ - spec/spec_helper.rb
156
+ - spec/support/rails_helpers.rb
157
+ - usr/bin/release.sh
158
+ homepage: https://github.com/amkisko/seed_builder.rb
159
+ licenses:
160
+ - MIT
161
+ metadata:
162
+ homepage: https://github.com/amkisko/seed_builder.rb
163
+ source_code_uri: https://github.com/amkisko/seed_builder.rb
164
+ bug_tracker_uri: https://github.com/amkisko/seed_builder.rb/issues
165
+ changelog_uri: https://github.com/amkisko/seed_builder.rb/blob/main/CHANGELOG.md
166
+ rubygems_mfa_required: 'true'
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '3'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubygems_version: 3.5.11
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: Seed builder with loader and generator
186
+ test_files:
187
+ - spec/fixtures/schema.rb
188
+ - spec/fixtures/seeds.rb
189
+ - spec/fixtures/seeds/20241206200111_create_users.rb
190
+ - spec/generator_spec.rb
191
+ - spec/seed_builder/config_spec.rb
192
+ - spec/seed_builder/loader_spec.rb
193
+ - spec/seed_builder_spec.rb
194
+ - spec/spec_helper.rb
195
+ - spec/support/rails_helpers.rb