custom_seeds 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c18e6ab268e271ea6b6e850d485ae3ddafe97c2b35aaf28eccfee628215498c
4
- data.tar.gz: 4f23f88fe7db39949b6cb99a49fdb53c7d7f5367f808a2f8207a1b95f6fc2e27
3
+ metadata.gz: e38b6613018be59c8754faa324c14bd806094b2cf32da6e9d8035e9187bb2bff
4
+ data.tar.gz: e7ba26d4d6f84f44f8d224043a960df3e2472da93fcdd46699ab7ed4f4d49016
5
5
  SHA512:
6
- metadata.gz: c25f3fcfe10bea51714723bc3ecf21f344de803253b393f42e72f0d01ff59e563e57e43183d62f6dc978e2bc971265ad8683825c1f9cd18923c621e7f67436c9
7
- data.tar.gz: ed8a157977947610ed55fb446ba911bc5d2eb451cda744dd191660a2f2ab0694631e9ac5eef99f7dae0a00f79a7bab9d3b1f3a7f135244367d72a31036250cd5
6
+ metadata.gz: 399e0ee4e87f51637aa49f5944ecf0a268f20a00094f6c4a78897596abce04ce4d7f57d3fe9642082f011d59f03f6782e6156ad237a14fa8467ad63acb1a777e
7
+ data.tar.gz: f5b4542a641a5a3510ac03c0b662e875f9d83d02a37e77b8e4e25d0ea044b72d01cd5192141d78609845decaf43f0346b2d98cef15a078e8b2df8bc3c7a67093
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- custom_seeds (0.1.0)
4
+ custom_seeds (0.2.0)
5
5
  progress_bar (= 1.3.1)
6
6
 
7
7
  GEM
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 CHANGED
@@ -3,9 +3,27 @@
3
3
 
4
4
  require 'bundler/setup'
5
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]
6
24
 
7
25
  # if argv[0] is a directory...
8
- if !ARGV[0] || File.directory?(ARGV[0])
26
+ if !file_path || File.directory?(file_path)
9
27
  list = CustomSeeds::SeedList.new(directory: ARGV[0])
10
28
  list.each { |seed| load(seed.filename) }
11
29
  else
@@ -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 run
80
- before&.call
81
- progress_bar = ProgressBar.new(records.size)
101
+ def log_sql_statements(&block)
102
+ sql_statements = []
103
+ return block.call unless verbose?
82
104
 
83
- records.each do |record|
84
- build_block.call(record)
105
+ subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |_, _, _, _, details|
106
+ sql_statements << details[:sql] unless details[:name] == 'SCHEMA'
107
+ end
85
108
 
86
- if log_block
87
- puts colorize(log_block.call(record), :blue)
88
- else
89
- progress_bar.increment!
90
- end
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(' Seeding completed', :green)
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
@@ -1,3 +1,3 @@
1
1
  module CustomSeeds
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Wheeler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-15 00:00:00.000000000 Z
11
+ date: 2024-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: progress_bar
@@ -88,6 +88,7 @@ executables:
88
88
  extensions: []
89
89
  extra_rdoc_files: []
90
90
  files:
91
+ - ".gitignore"
91
92
  - ".tool-versions"
92
93
  - Gemfile
93
94
  - Gemfile.lock