RbDbSetup 🚀
Note
This is a personal scaffolding personal setup created to automate the database infrastructure for my own Ruby projects. It is tailored for my specific workflow but is open for public use!
Features
- Class-Based Seeders: Organize your seeding logic into clean, reusable classes.
-
Central Orchestration: Uses a
DatabaseSeeder.rbentry point, just like Laravel'sDatabaseSeeder. - Hardened Migrations: Automated migration scripts with fail-safe checks and naming consistency.
-
Convenient Generators:
-
rb-db-setup: Scaffolds the entire infrastructure. -
ruby make_migration.rb <name>: Generates sanitized migration templates. -
ruby make_seeder.rb <Name>: Generates class-based seeder templates.
-
- Atomic Seeding: Seeding runs within database transactions to ensure data integrity.
- Cross-Platform: Works perfectly on Linux, macOS, and Windows.
Installation
Standard Installation
If you've installed it as a RubyGem:
gem install rb-db-setupAlternative: Clone & Run (Local)
If you have cloned the repository, you can run the tool directly from the project root:
ruby bin/rb-db-setupAlternative: Manual Setup (Copy & Run)
If you just want the logic without installing a gem or cloning the whole repo, you can simply grab the setup_db_tools.rb script:
- Copy setup_db_tools.rb to your project.
- Run it:
ruby setup_db_tools.rbNote: You can delete the script after it generates the infrastructure.
GitHub One-Liner (Fastest)
Run the latest version directly from GitHub without downloading anything:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/LanceDanielG/rb-db-scaffold-setup/main/setup_db_tools.rb)"Global Local Setup (Command Anywhere)
If you want the rb-db-setup command to work in any folder on your machine without publishing a gem, use the included installer scripts.
Linux / macOS
Run the installer to copy the script to ~/.local/bin and update your PATH:
bash install.sh && source ~/.bashrcWindows (PowerShell)
Run the PowerShell installer to set up a permanent command and update your User PATH:
powershell -ExecutionPolicy Bypass -File install.ps1Once installed, you can simply type rb-db-setup in any terminal window.
Usage
In any new Ruby project folder, simply run:
rb-db-setupThis will automatically create the following structure:
.
├── db/
│ ├── migrations/ # Your SQL/Sequel migrations
│ └── seeds/ # Class-based seeder files
│ └── DatabaseSeeder.rb
├── migrate.rb # Hardened migration runner
├── seed.rb # Laravel-style seeder runner
├── make_migration.rb # Migration generator
└── make_seeder.rb # Seeder generator
Running Migrations
ruby migrate.rbRunning Seeders
To run all seeders (via DatabaseSeeder.rb):
ruby seed.rbTo run a specific seeder class:
ruby seed.rb AdminSeederConfiguration
The generated scripts expect a DATABASE_URL environment variable. They automatically handle .env files if the dotenv gem is present in your project's Gemfile.
Dependencies
The generated infrastructure requires:
sequel-
pg(or your preferred database driver) -
dotenv(optional but recommended)
Automated Publishing with GitHub Actions 🤖
I've included a GitHub Action to automate the publishing of this gem to RubyGems.org.
How to set it up:
- Get your API Key: Go to RubyGems.org Settings and generate a new API key with "Push rubygem" permissions.
-
Add to GitHub Secrets:
- Go to your repository on GitHub.
- Click Settings > Secrets and variables > Actions.
- Create a New repository secret named
RUBYGEMS_API_KEYand paste your key.
-
Trigger a Release:
- Every time you push a tag starting with
v(e.g.,git tag v0.1.0 && git push origin v0.1.0), GitHub will automatically build and publish the gem for you!
- Every time you push a tag starting with
License
MIT License. See LICENSE for more information.