db_seeder 0.1.0 → 0.1.1
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/CHANGELOG.md +5 -0
- data/README.md +7 -0
- data/lib/db_seeder/configuration.rb +2 -1
- data/lib/db_seeder/exporter.rb +8 -1
- data/lib/db_seeder/sanitizer.rb +55 -0
- data/lib/db_seeder/version.rb +1 -1
- data/lib/db_seeder.rb +1 -0
- metadata +3 -3
- data/TESTING.md +0 -163
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9aabbaf10a33eebe75af86d800adfb4b9a13d6624ed67558bdc9683058470102
|
|
4
|
+
data.tar.gz: 3d99d26b854246024507b7a86b2dd5cfae22a6fbf8e969cb526bb56ecd28f57d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5ca48031ebcf9afa471ce385c654c4206e6cbdf986f315bff5ff02ebd87273e0b59714246a71015a1cc25450e40aba11d47120bea7e2cd01dc29762ba071e50d
|
|
7
|
+
data.tar.gz: 4968f06027c3ae8bda7c2356d7c5fc0ff830b11544fa2901b46e22152d5c8adcdbee93b641078690d0cef337d09263fc572297b69e074f3be440f5a76693a85d
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -64,6 +64,12 @@ DbSeeder.configure do |config|
|
|
|
64
64
|
config.excluded_tables = %w[schema_migrations ar_internal_metadata sessions]
|
|
65
65
|
config.batch_size = 1000
|
|
66
66
|
config.format = :ruby
|
|
67
|
+
config.sanitize_rules = {
|
|
68
|
+
users: {
|
|
69
|
+
email: :random_email,
|
|
70
|
+
name: ->(_v) { "Anonymous User" }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
67
73
|
end
|
|
68
74
|
```
|
|
69
75
|
|
|
@@ -81,6 +87,7 @@ DbSeeder.generate
|
|
|
81
87
|
- `excluded_tables`: Tables to skip (default: schema_migrations, ar_internal_metadata)
|
|
82
88
|
- `batch_size`: Records per batch (default: 1000)
|
|
83
89
|
- `format`: Output format (default: :ruby)
|
|
90
|
+
- `sanitize_rules`: Per-table attribute sanitization rules (default: {})
|
|
84
91
|
|
|
85
92
|
## Development
|
|
86
93
|
|
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
module DbSeeder
|
|
4
4
|
class Configuration
|
|
5
|
-
attr_accessor :output_path, :excluded_tables, :batch_size, :format
|
|
5
|
+
attr_accessor :output_path, :excluded_tables, :batch_size, :format, :sanitize_rules
|
|
6
6
|
|
|
7
7
|
def initialize
|
|
8
8
|
@output_path = "db/seeds"
|
|
9
9
|
@excluded_tables = %w[schema_migrations ar_internal_metadata]
|
|
10
10
|
@batch_size = 1000
|
|
11
11
|
@format = :ruby
|
|
12
|
+
@sanitize_rules = {}
|
|
12
13
|
end
|
|
13
14
|
end
|
|
14
15
|
end
|
data/lib/db_seeder/exporter.rb
CHANGED
|
@@ -7,6 +7,7 @@ module DbSeeder
|
|
|
7
7
|
def initialize(model, config)
|
|
8
8
|
@model = model
|
|
9
9
|
@config = config
|
|
10
|
+
@sanitizer = DbSeeder::Sanitizer.new(config)
|
|
10
11
|
end
|
|
11
12
|
|
|
12
13
|
def export
|
|
@@ -20,7 +21,13 @@ module DbSeeder
|
|
|
20
21
|
private
|
|
21
22
|
|
|
22
23
|
def serialize_record(record)
|
|
23
|
-
|
|
24
|
+
raw =
|
|
25
|
+
record
|
|
26
|
+
.attributes
|
|
27
|
+
.except("id", "created_at", "updated_at")
|
|
28
|
+
.symbolize_keys
|
|
29
|
+
|
|
30
|
+
@sanitizer.sanitize(model, raw)
|
|
24
31
|
end
|
|
25
32
|
end
|
|
26
33
|
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
module DbSeeder
|
|
6
|
+
class Sanitizer
|
|
7
|
+
def initialize(config)
|
|
8
|
+
@rules = config.sanitize_rules || {}
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def sanitize(model, attributes)
|
|
12
|
+
model_key = model_config_key(model)
|
|
13
|
+
model_rules = @rules[model_key] || {}
|
|
14
|
+
|
|
15
|
+
attributes.each_with_object({}) do |(key, value), result|
|
|
16
|
+
rule = model_rules[key.to_sym]
|
|
17
|
+
result[key] = apply_rule(rule, value)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def model_config_key(model)
|
|
24
|
+
if model.respond_to?(:table_name) && model.table_name
|
|
25
|
+
model.table_name.to_sym
|
|
26
|
+
elsif model.respond_to?(:name) && model.name && !model.name.empty?
|
|
27
|
+
model.name.underscore.to_sym
|
|
28
|
+
else
|
|
29
|
+
model.to_s.underscore.to_sym
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def apply_rule(rule, value)
|
|
34
|
+
return value if rule.nil?
|
|
35
|
+
|
|
36
|
+
if rule.respond_to?(:call)
|
|
37
|
+
return rule.call(value) if rule.arity <= 1
|
|
38
|
+
return rule.call(value, rule_context(value))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
case rule
|
|
42
|
+
when :null
|
|
43
|
+
nil
|
|
44
|
+
when :random_email
|
|
45
|
+
"user_#{SecureRandom.hex(4)}@example.com"
|
|
46
|
+
else
|
|
47
|
+
value
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def rule_context(_value)
|
|
52
|
+
{}
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
data/lib/db_seeder/version.rb
CHANGED
data/lib/db_seeder.rb
CHANGED
|
@@ -4,6 +4,7 @@ require_relative "db_seeder/version"
|
|
|
4
4
|
require_relative "db_seeder/configuration"
|
|
5
5
|
require_relative "db_seeder/generator"
|
|
6
6
|
require_relative "db_seeder/exporter"
|
|
7
|
+
require_relative "db_seeder/sanitizer"
|
|
7
8
|
require_relative "db_seeder/formatter"
|
|
8
9
|
require_relative "db_seeder/railtie" if defined?(Rails::Railtie)
|
|
9
10
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: db_seeder
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Claudia P. R. Soto
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2025-12-
|
|
11
|
+
date: 2025-12-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|
|
@@ -67,13 +67,13 @@ files:
|
|
|
67
67
|
- LICENSE.txt
|
|
68
68
|
- README.md
|
|
69
69
|
- Rakefile
|
|
70
|
-
- TESTING.md
|
|
71
70
|
- lib/db_seeder.rb
|
|
72
71
|
- lib/db_seeder/configuration.rb
|
|
73
72
|
- lib/db_seeder/exporter.rb
|
|
74
73
|
- lib/db_seeder/formatter.rb
|
|
75
74
|
- lib/db_seeder/generator.rb
|
|
76
75
|
- lib/db_seeder/railtie.rb
|
|
76
|
+
- lib/db_seeder/sanitizer.rb
|
|
77
77
|
- lib/db_seeder/version.rb
|
|
78
78
|
- lib/tasks/db_seeder.rake
|
|
79
79
|
- sig/db_seeder.rbs
|
data/TESTING.md
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
# Testing Guide - DbSeeder
|
|
2
|
-
|
|
3
|
-
## ✅ Automated Tests
|
|
4
|
-
|
|
5
|
-
The gem includes automated tests with RSpec that verify all functionality:
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
cd /Users/claudiapatriciarojassoto/Development/db_seeder
|
|
9
|
-
bundle exec rspec
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
**Result:** 6 examples, 0 failures ✅
|
|
13
|
-
|
|
14
|
-
### Included tests:
|
|
15
|
-
|
|
16
|
-
1. **Basic configuration**
|
|
17
|
-
- ✅ Has a version number
|
|
18
|
-
- ✅ Configurable settings
|
|
19
|
-
- ✅ Correct defaults
|
|
20
|
-
|
|
21
|
-
2. **Seed generation**
|
|
22
|
-
- ✅ Generates files for specific tables
|
|
23
|
-
- ✅ Generates valid Ruby code with `create!`
|
|
24
|
-
- ✅ Creates directories automatically
|
|
25
|
-
- ✅ Handles UTF-8 correctly
|
|
26
|
-
|
|
27
|
-
## 🧪 Generated Output Example
|
|
28
|
-
|
|
29
|
-
When you run the gem with a `products` table:
|
|
30
|
-
|
|
31
|
-
```ruby
|
|
32
|
-
# frozen_string_literal: true
|
|
33
|
-
|
|
34
|
-
# Seeds for Product
|
|
35
|
-
|
|
36
|
-
Product.create!(
|
|
37
|
-
name: 'Laptop',
|
|
38
|
-
price: 999.99,
|
|
39
|
-
stock: 10
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
Product.create!(
|
|
43
|
-
name: 'Mouse',
|
|
44
|
-
price: 29.99,
|
|
45
|
-
stock: 50
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
Product.create!(
|
|
49
|
-
name: 'Keyboard',
|
|
50
|
-
price: 79.99,
|
|
51
|
-
stock: 25
|
|
52
|
-
)
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## 🚀 How to Test in a Real Rails App
|
|
56
|
-
|
|
57
|
-
### 1. Install the gem locally
|
|
58
|
-
|
|
59
|
-
```bash
|
|
60
|
-
cd /Users/claudiapatriciarojassoto/Development/db_seeder
|
|
61
|
-
gem install ./db_seeder-0.1.0.gem
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
### 2. In your Rails application
|
|
65
|
-
|
|
66
|
-
Add to your `Gemfile`:
|
|
67
|
-
|
|
68
|
-
```ruby
|
|
69
|
-
gem 'db_seeder', path: '/Users/claudiapatriciarojassoto/Development/db_seeder'
|
|
70
|
-
# Or after publishing:
|
|
71
|
-
# gem 'db_seeder', '~> 0.1.0'
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### 3. Run bundle install
|
|
75
|
-
|
|
76
|
-
```bash
|
|
77
|
-
bundle install
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### 4. Configure (optional)
|
|
81
|
-
|
|
82
|
-
Create `config/initializers/db_seeder.rb`:
|
|
83
|
-
|
|
84
|
-
```ruby
|
|
85
|
-
DbSeeder.configure do |config|
|
|
86
|
-
config.output_path = "db/seeds/generated"
|
|
87
|
-
config.excluded_tables = %w[schema_migrations ar_internal_metadata sessions]
|
|
88
|
-
config.batch_size = 1000
|
|
89
|
-
end
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### 5. Generate seeds
|
|
93
|
-
|
|
94
|
-
```bash
|
|
95
|
-
# All tables:
|
|
96
|
-
rake db:seed:generate
|
|
97
|
-
|
|
98
|
-
# Specific table:
|
|
99
|
-
rake db:seed:generate TABLE=users
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### 6. Verify the output
|
|
103
|
-
|
|
104
|
-
```bash
|
|
105
|
-
ls db/seeds/
|
|
106
|
-
cat db/seeds/users.rb
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
## 📋 Functionality Checklist
|
|
110
|
-
|
|
111
|
-
- ✅ Exports data from ActiveRecord
|
|
112
|
-
- ✅ Generates clean Ruby code
|
|
113
|
-
- ✅ Handles multiple data types (String, Integer, Decimal, DateTime)
|
|
114
|
-
- ✅ Excludes system tables automatically
|
|
115
|
-
- ✅ Configurable and extensible
|
|
116
|
-
- ✅ Processes in batches for large tables
|
|
117
|
-
- ✅ Supports UTF-8 and special characters
|
|
118
|
-
- ✅ Rails integration via Railtie
|
|
119
|
-
- ✅ Ready-to-use Rake tasks
|
|
120
|
-
|
|
121
|
-
## 🎯 Tested Use Cases
|
|
122
|
-
|
|
123
|
-
1. **Tables with simple data** ✅
|
|
124
|
-
2. **Special characters and UTF-8** ✅
|
|
125
|
-
3. **Multiple column types** ✅
|
|
126
|
-
4. **Automatic directory creation** ✅
|
|
127
|
-
5. **Custom configuration** ✅
|
|
128
|
-
|
|
129
|
-
## 📊 Test Coverage
|
|
130
|
-
|
|
131
|
-
```
|
|
132
|
-
DbSeeder
|
|
133
|
-
has a version number ✅
|
|
134
|
-
.configure
|
|
135
|
-
yields configuration ✅
|
|
136
|
-
.configuration
|
|
137
|
-
returns default configuration ✅
|
|
138
|
-
|
|
139
|
-
DbSeeder::Generator
|
|
140
|
-
#generate
|
|
141
|
-
generates seed file for specified table ✅
|
|
142
|
-
generates seed files with valid Ruby code ✅
|
|
143
|
-
creates output directory if it doesn't exist ✅
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
## 🔧 Recommended Manual Verification
|
|
147
|
-
|
|
148
|
-
If you want to do additional testing in a real Rails app:
|
|
149
|
-
|
|
150
|
-
1. Create a new Rails app for testing
|
|
151
|
-
2. Generate some models with data
|
|
152
|
-
3. Install db_seeder
|
|
153
|
-
4. Run the generation
|
|
154
|
-
5. Verify that the generated files are valid
|
|
155
|
-
6. Test running the seeds on a clean database
|
|
156
|
-
|
|
157
|
-
## ✨ Final Result
|
|
158
|
-
|
|
159
|
-
The gem **works correctly** and is ready for:
|
|
160
|
-
- ✅ Development use
|
|
161
|
-
- ✅ Publishing to RubyGems
|
|
162
|
-
- ✅ Use in real Rails projects
|
|
163
|
-
|