custom_seeds 0.1.0 → 0.3.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/.gitignore +1 -0
- data/Gemfile.lock +2 -1
- data/README.md +12 -0
- data/bin/rseed +32 -0
- data/custom_seeds.gemspec +2 -2
- data/lib/custom_seeds/seed_file.rb +64 -11
- data/lib/custom_seeds/seed_list.rb +2 -2
- data/lib/custom_seeds/version.rb +1 -1
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e38b6613018be59c8754faa324c14bd806094b2cf32da6e9d8035e9187bb2bff
|
4
|
+
data.tar.gz: e7ba26d4d6f84f44f8d224043a960df3e2472da93fcdd46699ab7ed4f4d49016
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 399e0ee4e87f51637aa49f5944ecf0a268f20a00094f6c4a78897596abce04ce4d7f57d3fe9642082f011d59f03f6782e6156ad237a14fa8467ad63acb1a777e
|
7
|
+
data.tar.gz: f5b4542a641a5a3510ac03c0b662e875f9d83d02a37e77b8e4e25d0ea044b72d01cd5192141d78609845decaf43f0346b2d98cef15a078e8b2df8bc3c7a67093
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -45,6 +45,18 @@ CustomSeeds.define do
|
|
45
45
|
end
|
46
46
|
```
|
47
47
|
|
48
|
+
### CLI
|
49
|
+
|
50
|
+
Run seeds using the `rseed` command:
|
51
|
+
|
52
|
+
$ rseed
|
53
|
+
$ rseed db/seeds/users/
|
54
|
+
$ rseed db/seeds/users/followed_products.rb
|
55
|
+
|
56
|
+
Run all seeds:
|
57
|
+
|
58
|
+
### Rake
|
59
|
+
|
48
60
|
Run the file using the rake task:
|
49
61
|
|
50
62
|
```bash
|
data/bin/rseed
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'custom_seeds'
|
6
|
+
require 'optparse'
|
7
|
+
|
8
|
+
options = {}
|
9
|
+
OptionParser.new do |opts|
|
10
|
+
opts.banner = 'Usage: rseed [options] FILE'
|
11
|
+
|
12
|
+
opts.on('-v', '--verbose', 'Log all sql commands run') do
|
13
|
+
options[:verbose] = true
|
14
|
+
end
|
15
|
+
|
16
|
+
opts.on('-d', '--dry-run', 'Rollback all changes after test run') do
|
17
|
+
options[:dry_run] = true
|
18
|
+
end
|
19
|
+
end.parse!
|
20
|
+
|
21
|
+
CustomSeeds::SeedFile.options(options)
|
22
|
+
|
23
|
+
file_path = ARGV[0]
|
24
|
+
|
25
|
+
# if argv[0] is a directory...
|
26
|
+
if !file_path || File.directory?(file_path)
|
27
|
+
list = CustomSeeds::SeedList.new(directory: ARGV[0])
|
28
|
+
list.each { |seed| load(seed.filename) }
|
29
|
+
else
|
30
|
+
# ...otherwise, load the file
|
31
|
+
load ARGV[0]
|
32
|
+
end
|
data/custom_seeds.gemspec
CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.files = `[ -d ".git" ] > /dev/null && type git > /dev/null && git ls-files -z`.split("\x0").reject do |f|
|
14
14
|
f.match(%r{^(test|spec|features)/})
|
15
15
|
end
|
16
|
-
spec.bindir = '
|
17
|
-
spec.executables = spec.files.grep(%r{^
|
16
|
+
spec.bindir = 'bin'
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.require_paths = ['lib']
|
19
19
|
|
20
20
|
spec.add_dependency 'progress_bar', '1.3.1'
|
@@ -17,11 +17,27 @@ module CustomSeeds
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def initialize
|
20
|
+
@dry_run = self.class.options[:dry_run]
|
21
|
+
@verbose = self.class.options[:verbose]
|
20
22
|
@records = []
|
21
23
|
@build_block = ->(_record) { raise 'No build block defined' }
|
22
24
|
@log_block = nil
|
23
25
|
end
|
24
26
|
|
27
|
+
def self.options(value = nil)
|
28
|
+
return @options if value.nil?
|
29
|
+
|
30
|
+
@options = value
|
31
|
+
end
|
32
|
+
|
33
|
+
def dry_run?
|
34
|
+
@dry_run
|
35
|
+
end
|
36
|
+
|
37
|
+
def verbose?
|
38
|
+
@verbose
|
39
|
+
end
|
40
|
+
|
25
41
|
def colorize(string, color_code)
|
26
42
|
"\e[#{MAP[color_code]}m#{string}\e[0m"
|
27
43
|
end
|
@@ -51,6 +67,12 @@ module CustomSeeds
|
|
51
67
|
@before_block = block
|
52
68
|
end
|
53
69
|
|
70
|
+
def after(&block)
|
71
|
+
return @after_block if block.nil?
|
72
|
+
|
73
|
+
@after_block = block
|
74
|
+
end
|
75
|
+
|
54
76
|
# memoize block
|
55
77
|
def let(name, &block)
|
56
78
|
@memoized ||= {}
|
@@ -76,21 +98,52 @@ module CustomSeeds
|
|
76
98
|
@log_block = block
|
77
99
|
end
|
78
100
|
|
79
|
-
def
|
80
|
-
|
81
|
-
|
101
|
+
def log_sql_statements(&block)
|
102
|
+
sql_statements = []
|
103
|
+
return block.call unless verbose?
|
82
104
|
|
83
|
-
|
84
|
-
|
105
|
+
subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |_, _, _, _, details|
|
106
|
+
sql_statements << details[:sql] unless details[:name] == 'SCHEMA'
|
107
|
+
end
|
85
108
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
109
|
+
block.call
|
110
|
+
ensure
|
111
|
+
ActiveSupport::Notifications.unsubscribe(subscriber)
|
112
|
+
sql_statements.each { |sql| puts sql }
|
113
|
+
end
|
114
|
+
|
115
|
+
def transaction(&block)
|
116
|
+
puts colorize('🚧 Dry Run. No changes will be applied.', :green) if dry_run?
|
117
|
+
|
118
|
+
ActiveRecord::Base.transaction do
|
119
|
+
block.call
|
120
|
+
|
121
|
+
raise ActiveRecord::Rollback if dry_run?
|
91
122
|
end
|
92
123
|
|
93
|
-
puts colorize('
|
124
|
+
puts colorize('🚧 Dry run complete', :green) if dry_run?
|
125
|
+
end
|
126
|
+
|
127
|
+
def run
|
128
|
+
log_sql_statements do
|
129
|
+
transaction do
|
130
|
+
before&.call
|
131
|
+
progress_bar = ProgressBar.new(records.size)
|
132
|
+
|
133
|
+
records.each do |record|
|
134
|
+
build_block.call(record)
|
135
|
+
|
136
|
+
if log_block
|
137
|
+
puts colorize(log_block.call(record), :blue)
|
138
|
+
else
|
139
|
+
progress_bar.increment!
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
after&.call
|
144
|
+
puts colorize('✅ Seeding completed', :green)
|
145
|
+
end
|
146
|
+
end
|
94
147
|
end
|
95
148
|
end
|
96
149
|
end
|
data/lib/custom_seeds/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_seeds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Wheeler
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: progress_bar
|
@@ -83,15 +83,18 @@ dependencies:
|
|
83
83
|
description:
|
84
84
|
email:
|
85
85
|
- mwheeler@g2.com
|
86
|
-
executables:
|
86
|
+
executables:
|
87
|
+
- rseed
|
87
88
|
extensions: []
|
88
89
|
extra_rdoc_files: []
|
89
90
|
files:
|
91
|
+
- ".gitignore"
|
90
92
|
- ".tool-versions"
|
91
93
|
- Gemfile
|
92
94
|
- Gemfile.lock
|
93
95
|
- README.md
|
94
96
|
- Rakefile
|
97
|
+
- bin/rseed
|
95
98
|
- custom_seeds.gemspec
|
96
99
|
- lib/custom_seeds.rb
|
97
100
|
- lib/custom_seeds/railtie.rb
|