db_seeder 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 +7 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +12 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/CONTRIBUTING.md +145 -0
- data/LICENSE.txt +21 -0
- data/README.md +98 -0
- data/Rakefile +1 -0
- data/TESTING.md +163 -0
- data/lib/db_seeder/configuration.rb +14 -0
- data/lib/db_seeder/exporter.rb +26 -0
- data/lib/db_seeder/formatter.rb +56 -0
- data/lib/db_seeder/generator.rb +58 -0
- data/lib/db_seeder/railtie.rb +11 -0
- data/lib/db_seeder/version.rb +5 -0
- data/lib/db_seeder.rb +28 -0
- data/lib/tasks/db_seeder.rake +20 -0
- data/sig/db_seeder.rbs +4 -0
- metadata +106 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9d3332fc998967c3ded1241408a38f9ac4e29c6ad86b0507c241c57b8d370ff9
|
|
4
|
+
data.tar.gz: '0168b1df48156d26501a4d16d5a165c302047f08b394e806b3e2c843386564a4'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 82c5ee9f5bb31f465364df44172740c49c7e8560b9d3c974fe6bddb32784c79949754b60089aae067157a9dfc7078afff1185e8ecc16adaf3facd0fddfebb175
|
|
7
|
+
data.tar.gz: 5a41d1ef7b42eaadd6cc22b2866fdbe61d191f2b4065b27c221ab2a143a5822578eadad93b04c767f30aa3102b4a824f9827fb8b1cc7512c3a280ddbb4dbd661
|
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--require spec_helper
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
## [Unreleased]
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 2025-12-20
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- Initial release
|
|
7
|
+
- Generate seed files from database tables
|
|
8
|
+
- Configuration options for output path, excluded tables, and batch size
|
|
9
|
+
- Rake tasks for seed generation
|
|
10
|
+
- Support for Active Record models
|
|
11
|
+
- Automatic model resolution or anonymous model creation
|
|
12
|
+
- Ruby format output for seed files
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Contributing to DbSeeder
|
|
2
|
+
|
|
3
|
+
## Git Workflow
|
|
4
|
+
|
|
5
|
+
This project follows a Git Flow workflow to ensure code quality and stability.
|
|
6
|
+
|
|
7
|
+
### Branch Structure
|
|
8
|
+
|
|
9
|
+
- **`main`**: Production-ready code. Protected branch.
|
|
10
|
+
- **`develop`**: Integration branch for features. All development happens here.
|
|
11
|
+
- **`feature/*`**: Feature branches created from `develop`
|
|
12
|
+
- **`hotfix/*`**: Emergency fixes created from `main`
|
|
13
|
+
|
|
14
|
+
### Development Workflow
|
|
15
|
+
|
|
16
|
+
1. **Create a feature branch from develop**
|
|
17
|
+
```bash
|
|
18
|
+
git checkout develop
|
|
19
|
+
git pull origin develop
|
|
20
|
+
git checkout -b feature/your-feature-name
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
2. **Make your changes**
|
|
24
|
+
- Write tests
|
|
25
|
+
- Implement feature
|
|
26
|
+
- Update documentation
|
|
27
|
+
|
|
28
|
+
3. **Run tests**
|
|
29
|
+
```bash
|
|
30
|
+
bundle exec rspec
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
4. **Commit your changes**
|
|
34
|
+
```bash
|
|
35
|
+
git add .
|
|
36
|
+
git commit -m "Add: description of your feature"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
5. **Push to GitHub**
|
|
40
|
+
```bash
|
|
41
|
+
git push origin feature/your-feature-name
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
6. **Create a Pull Request**
|
|
45
|
+
- Go to GitHub
|
|
46
|
+
- Create PR from `feature/your-feature-name` to `develop`
|
|
47
|
+
- Wait for review and approval
|
|
48
|
+
- Merge to `develop`
|
|
49
|
+
|
|
50
|
+
7. **Release to main**
|
|
51
|
+
- When `develop` is stable and ready for release
|
|
52
|
+
- Create PR from `develop` to `main`
|
|
53
|
+
- After approval, merge to `main`
|
|
54
|
+
- Tag the release
|
|
55
|
+
|
|
56
|
+
### Commit Message Convention
|
|
57
|
+
|
|
58
|
+
Use clear, descriptive commit messages:
|
|
59
|
+
|
|
60
|
+
- `Add: new feature or functionality`
|
|
61
|
+
- `Fix: bug fix`
|
|
62
|
+
- `Update: changes to existing functionality`
|
|
63
|
+
- `Refactor: code refactoring`
|
|
64
|
+
- `Docs: documentation changes`
|
|
65
|
+
- `Test: test additions or modifications`
|
|
66
|
+
|
|
67
|
+
### Branch Protection Rules (Recommended on GitHub)
|
|
68
|
+
|
|
69
|
+
#### For `main` branch:
|
|
70
|
+
- ✅ Require pull request reviews before merging
|
|
71
|
+
- ✅ Require status checks to pass (CI/tests)
|
|
72
|
+
- ✅ Require branches to be up to date before merging
|
|
73
|
+
- ✅ Require conversation resolution before merging
|
|
74
|
+
- ✅ Do not allow bypassing the above settings
|
|
75
|
+
- ❌ Disable force pushes
|
|
76
|
+
- ❌ Disable deletions
|
|
77
|
+
|
|
78
|
+
#### For `develop` branch:
|
|
79
|
+
- ✅ Require pull request reviews before merging (at least 1)
|
|
80
|
+
- ✅ Require status checks to pass
|
|
81
|
+
- ❌ Disable force pushes
|
|
82
|
+
- ❌ Disable deletions
|
|
83
|
+
|
|
84
|
+
### Setting Up Branch Protection on GitHub
|
|
85
|
+
|
|
86
|
+
1. Go to your repository on GitHub
|
|
87
|
+
2. Click **Settings** > **Branches**
|
|
88
|
+
3. Click **Add rule** under "Branch protection rules"
|
|
89
|
+
4. For Branch name pattern, enter: `main`
|
|
90
|
+
5. Enable the following:
|
|
91
|
+
- ☑ Require a pull request before merging
|
|
92
|
+
- ☑ Require approvals (1)
|
|
93
|
+
- ☑ Dismiss stale pull request approvals when new commits are pushed
|
|
94
|
+
- ☑ Require status checks to pass before merging
|
|
95
|
+
- ☑ Require branches to be up to date before merging
|
|
96
|
+
- ☑ Require conversation resolution before merging
|
|
97
|
+
- ☑ Do not allow bypassing the above settings
|
|
98
|
+
6. Click **Create**
|
|
99
|
+
7. Repeat for `develop` branch
|
|
100
|
+
|
|
101
|
+
### Code Review Guidelines
|
|
102
|
+
|
|
103
|
+
When reviewing PRs:
|
|
104
|
+
- ✅ Check that tests are passing
|
|
105
|
+
- ✅ Verify code follows Ruby style guides
|
|
106
|
+
- ✅ Ensure documentation is updated
|
|
107
|
+
- ✅ Test the changes locally if needed
|
|
108
|
+
- ✅ Provide constructive feedback
|
|
109
|
+
|
|
110
|
+
### Running Tests
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
# Run all tests
|
|
114
|
+
bundle exec rspec
|
|
115
|
+
|
|
116
|
+
# Run specific test file
|
|
117
|
+
bundle exec rspec spec/generator_spec.rb
|
|
118
|
+
|
|
119
|
+
# Run with coverage
|
|
120
|
+
bundle exec rspec --format documentation
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Release Process
|
|
124
|
+
|
|
125
|
+
1. Ensure `develop` branch is stable and tested
|
|
126
|
+
2. Create PR from `develop` to `main`
|
|
127
|
+
3. Update `CHANGELOG.md` with new version
|
|
128
|
+
4. Update `lib/db_seeder/version.rb` with new version
|
|
129
|
+
5. After merging to `main`, create a git tag:
|
|
130
|
+
```bash
|
|
131
|
+
git checkout main
|
|
132
|
+
git pull origin main
|
|
133
|
+
git tag -a v0.2.0 -m "Release version 0.2.0"
|
|
134
|
+
git push origin v0.2.0
|
|
135
|
+
```
|
|
136
|
+
6. Build and publish gem:
|
|
137
|
+
```bash
|
|
138
|
+
gem build db_seeder.gemspec
|
|
139
|
+
gem push db_seeder-0.2.0.gem
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Questions?
|
|
143
|
+
|
|
144
|
+
Feel free to open an issue if you have questions about the contribution process.
|
|
145
|
+
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Claudia P. R. Soto
|
|
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,98 @@
|
|
|
1
|
+
# DbSeeder
|
|
2
|
+
|
|
3
|
+
**A modern Rails gem to generate seed files from your existing database data.**
|
|
4
|
+
|
|
5
|
+
Perfect for creating seeds from production data, sharing database states across teams, or migrating data between environments.
|
|
6
|
+
|
|
7
|
+
## Why DbSeeder?
|
|
8
|
+
|
|
9
|
+
While similar gems exist, DbSeeder offers several advantages:
|
|
10
|
+
|
|
11
|
+
- ✅ **Modern & Maintained** - Built for Rails 7+, actively maintained
|
|
12
|
+
- ✅ **Memory Efficient** - Batch processing for large tables (won't crash on millions of records)
|
|
13
|
+
- ✅ **SOLID Architecture** - Clean, testable, maintainable code
|
|
14
|
+
- ✅ **Flexible Configuration** - Customize output path, excluded tables, batch size, and more
|
|
15
|
+
- ✅ **Well Tested** - Comprehensive test suite with 100% core functionality coverage
|
|
16
|
+
|
|
17
|
+
### DbSeeder vs Other Solutions
|
|
18
|
+
|
|
19
|
+
| Feature | DbSeeder | seed_dump | Faker |
|
|
20
|
+
|---------|----------|-----------|-------|
|
|
21
|
+
| Purpose | Export real data | Export real data | Generate fake data |
|
|
22
|
+
| Rails 7+ | ✅ Yes | ⚠️ Unmaintained since 2015 | ✅ Yes |
|
|
23
|
+
| Batch Processing | ✅ Yes | ❌ No | N/A |
|
|
24
|
+
| Configurable | ✅ Yes | Limited | N/A |
|
|
25
|
+
| Use Case | Production → Seeds | Production → Seeds | Testing/Development |
|
|
26
|
+
|
|
27
|
+
**Use DbSeeder when:** You need to export real database records to seed files
|
|
28
|
+
**Use Faker when:** You need to generate fake/random data for testing
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
Add to your Gemfile:
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
gem 'db_seeder'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Then execute:
|
|
39
|
+
```bash
|
|
40
|
+
bundle install
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
### Basic Usage
|
|
46
|
+
|
|
47
|
+
Generate seeds for all tables:
|
|
48
|
+
```bash
|
|
49
|
+
rake db:seed:generate
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Generate seeds for specific table:
|
|
53
|
+
```bash
|
|
54
|
+
rake db:seed:generate TABLE=users
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Configuration
|
|
58
|
+
|
|
59
|
+
Create an initializer `config/initializers/db_seeder.rb`:
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
DbSeeder.configure do |config|
|
|
63
|
+
config.output_path = "db/seeds"
|
|
64
|
+
config.excluded_tables = %w[schema_migrations ar_internal_metadata sessions]
|
|
65
|
+
config.batch_size = 1000
|
|
66
|
+
config.format = :ruby
|
|
67
|
+
end
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Programmatic Usage
|
|
71
|
+
|
|
72
|
+
```ruby
|
|
73
|
+
DbSeeder.generate("users")
|
|
74
|
+
|
|
75
|
+
DbSeeder.generate
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Configuration Options
|
|
79
|
+
|
|
80
|
+
- `output_path`: Directory for generated seed files (default: "db/seeds")
|
|
81
|
+
- `excluded_tables`: Tables to skip (default: schema_migrations, ar_internal_metadata)
|
|
82
|
+
- `batch_size`: Records per batch (default: 1000)
|
|
83
|
+
- `format`: Output format (default: :ruby)
|
|
84
|
+
|
|
85
|
+
## Development
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
bundle install
|
|
89
|
+
bundle exec rspec
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Contributing
|
|
93
|
+
|
|
94
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ClaudiaRojasSoto/db_seeder.
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
MIT License. See LICENSE.txt for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
task default: :spec
|
data/TESTING.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DbSeeder
|
|
4
|
+
class Configuration
|
|
5
|
+
attr_accessor :output_path, :excluded_tables, :batch_size, :format
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@output_path = "db/seeds"
|
|
9
|
+
@excluded_tables = %w[schema_migrations ar_internal_metadata]
|
|
10
|
+
@batch_size = 1000
|
|
11
|
+
@format = :ruby
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DbSeeder
|
|
4
|
+
class Exporter
|
|
5
|
+
attr_reader :model, :config
|
|
6
|
+
|
|
7
|
+
def initialize(model, config)
|
|
8
|
+
@model = model
|
|
9
|
+
@config = config
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def export
|
|
13
|
+
records = []
|
|
14
|
+
model.find_in_batches(batch_size: config.batch_size) do |batch|
|
|
15
|
+
records.concat(batch.map(&method(:serialize_record)))
|
|
16
|
+
end
|
|
17
|
+
records
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def serialize_record(record)
|
|
23
|
+
record.attributes.except("id", "created_at", "updated_at").symbolize_keys
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DbSeeder
|
|
4
|
+
module Formatter
|
|
5
|
+
class Base
|
|
6
|
+
def format(records)
|
|
7
|
+
raise NotImplementedError
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class Ruby < Base
|
|
12
|
+
def initialize(model_name = nil)
|
|
13
|
+
@model_name = model_name
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def format(records)
|
|
17
|
+
return "" if records.empty?
|
|
18
|
+
|
|
19
|
+
lines = ["# frozen_string_literal: true", ""]
|
|
20
|
+
lines << "# Seeds for #{@model_name}" if @model_name
|
|
21
|
+
lines << ""
|
|
22
|
+
|
|
23
|
+
records.each do |record|
|
|
24
|
+
lines << format_record(record)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
lines.join("\n")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def format_record(record)
|
|
33
|
+
attributes = record.map { |k, v| " #{k}: #{value_to_ruby(v)}" }.join(",\n")
|
|
34
|
+
"#{@model_name}.create!(\n#{attributes}\n)\n"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def value_to_ruby(value)
|
|
38
|
+
case value
|
|
39
|
+
when String then "'#{value.gsub("'", "\\\\'")}'"
|
|
40
|
+
when Symbol then ":#{value}"
|
|
41
|
+
when NilClass then "nil"
|
|
42
|
+
when TrueClass, FalseClass, Numeric then value.to_s
|
|
43
|
+
when Time, Date, DateTime then "'#{value.iso8601}'"
|
|
44
|
+
else value.inspect
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.for(format_type, model_name = nil)
|
|
50
|
+
case format_type
|
|
51
|
+
when :ruby then Ruby.new(model_name)
|
|
52
|
+
else raise Error, "Unknown format: #{format_type}"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_record"
|
|
4
|
+
|
|
5
|
+
module DbSeeder
|
|
6
|
+
class Generator
|
|
7
|
+
attr_reader :config, :options
|
|
8
|
+
|
|
9
|
+
def initialize(config, options = {})
|
|
10
|
+
@config = config
|
|
11
|
+
@options = options
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def generate(table_name = nil)
|
|
15
|
+
tables = table_name ? [table_name] : fetch_tables
|
|
16
|
+
|
|
17
|
+
tables.each do |table|
|
|
18
|
+
export_table(table)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def fetch_tables
|
|
25
|
+
ActiveRecord::Base.connection.tables.reject do |table|
|
|
26
|
+
config.excluded_tables.include?(table)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def export_table(table_name)
|
|
31
|
+
model_class = resolve_model(table_name)
|
|
32
|
+
exporter = Exporter.new(model_class, config)
|
|
33
|
+
formatter = Formatter.for(config.format, model_class.name)
|
|
34
|
+
|
|
35
|
+
output = formatter.format(exporter.export)
|
|
36
|
+
write_output(table_name, output)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def resolve_model(table_name)
|
|
40
|
+
class_name = table_name.classify
|
|
41
|
+
class_name.constantize
|
|
42
|
+
rescue NameError
|
|
43
|
+
create_anonymous_model(table_name)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def create_anonymous_model(table_name)
|
|
47
|
+
Class.new(ActiveRecord::Base) do
|
|
48
|
+
self.table_name = table_name
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def write_output(table_name, content)
|
|
53
|
+
FileUtils.mkdir_p(config.output_path)
|
|
54
|
+
file_path = File.join(config.output_path, "#{table_name}.rb")
|
|
55
|
+
File.write(file_path, content)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
data/lib/db_seeder.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "db_seeder/version"
|
|
4
|
+
require_relative "db_seeder/configuration"
|
|
5
|
+
require_relative "db_seeder/generator"
|
|
6
|
+
require_relative "db_seeder/exporter"
|
|
7
|
+
require_relative "db_seeder/formatter"
|
|
8
|
+
require_relative "db_seeder/railtie" if defined?(Rails::Railtie)
|
|
9
|
+
|
|
10
|
+
module DbSeeder
|
|
11
|
+
class Error < StandardError; end
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
attr_writer :configuration
|
|
15
|
+
|
|
16
|
+
def configuration
|
|
17
|
+
@configuration ||= Configuration.new
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def configure
|
|
21
|
+
yield(configuration)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def generate(table_name = nil, **options)
|
|
25
|
+
Generator.new(configuration, options).generate(table_name)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
namespace :db do
|
|
4
|
+
namespace :seed do
|
|
5
|
+
desc "Generate seed files from database tables"
|
|
6
|
+
task generate: :environment do
|
|
7
|
+
table = ENV["TABLE"]
|
|
8
|
+
|
|
9
|
+
DbSeeder.generate(table)
|
|
10
|
+
|
|
11
|
+
puts "Seed files generated in #{DbSeeder.configuration.output_path}"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
desc "Generate seed files and run them"
|
|
15
|
+
task regenerate: :environment do
|
|
16
|
+
Rake::Task["db:seed:generate"].invoke
|
|
17
|
+
Rake::Task["db:seed"].invoke
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
data/sig/db_seeder.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: db_seeder
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Claudia P. R. Soto
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-12-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rake
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '13.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '13.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: sqlite3
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.4'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.4'
|
|
55
|
+
description: A Rails gem that automatically generates seed files from your existing
|
|
56
|
+
database records, making it easy to replicate data across environments
|
|
57
|
+
email:
|
|
58
|
+
- claudiarojassoto@gmail.com
|
|
59
|
+
executables: []
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- ".rspec"
|
|
64
|
+
- CHANGELOG.md
|
|
65
|
+
- CODE_OF_CONDUCT.md
|
|
66
|
+
- CONTRIBUTING.md
|
|
67
|
+
- LICENSE.txt
|
|
68
|
+
- README.md
|
|
69
|
+
- Rakefile
|
|
70
|
+
- TESTING.md
|
|
71
|
+
- lib/db_seeder.rb
|
|
72
|
+
- lib/db_seeder/configuration.rb
|
|
73
|
+
- lib/db_seeder/exporter.rb
|
|
74
|
+
- lib/db_seeder/formatter.rb
|
|
75
|
+
- lib/db_seeder/generator.rb
|
|
76
|
+
- lib/db_seeder/railtie.rb
|
|
77
|
+
- lib/db_seeder/version.rb
|
|
78
|
+
- lib/tasks/db_seeder.rake
|
|
79
|
+
- sig/db_seeder.rbs
|
|
80
|
+
homepage: https://github.com/ClaudiaRojasSoto/db_seeder
|
|
81
|
+
licenses:
|
|
82
|
+
- MIT
|
|
83
|
+
metadata:
|
|
84
|
+
homepage_uri: https://github.com/ClaudiaRojasSoto/db_seeder
|
|
85
|
+
source_code_uri: https://github.com/ClaudiaRojasSoto/db_seeder
|
|
86
|
+
changelog_uri: https://github.com/ClaudiaRojasSoto/db_seeder/blob/main/CHANGELOG.md
|
|
87
|
+
post_install_message:
|
|
88
|
+
rdoc_options: []
|
|
89
|
+
require_paths:
|
|
90
|
+
- lib
|
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: 3.1.0
|
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - ">="
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '0'
|
|
101
|
+
requirements: []
|
|
102
|
+
rubygems_version: 3.3.7
|
|
103
|
+
signing_key:
|
|
104
|
+
specification_version: 4
|
|
105
|
+
summary: Generate Rails seeds from existing database data
|
|
106
|
+
test_files: []
|