A command line interface for database management with customizable options for common tasks (i.e. create, drop, dump, restore, etc.) Also supports the remigration process which makes dealing with cleaning up tiny migration changes/updates that accumulate over time in your Ruby on Rails db/migrate folder.
Table of Contents
- Code of Conduct
- Faster execution when creating/dropping databases than the default Ruby on Rails rake tasks.
- Adds support for dumping and restoring of a database.
- Adds configurable settings for common database operations.
- Adds support for easier remigration/rebuilding of a database with many migration fragments.
- Ruby 2.4.x.
- A strong understanding of database management, migrations, dumps/restores, etc.
For a secure install, type the following from the command line (recommended):
gem cert --add <(curl --location --silent https://www.alchemists.io/gem-public.pem) gem install db --trust-policy MediumSecurity
NOTE: A HighSecurity trust policy would be best but MediumSecurity enables signed gem verification while allowing the installation of unsigned dependencies since they are beyond the scope of this gem.
For an insecure install, type the following (not recommended):
gem install db
You can change the default settings for this gem by creating the following file:
The contents of the file should look like this (where the default values can be changed to your liking):
--- :current_database: pg :databases: :pg: :options: :create: -w :drop: -w :dump: -Fc -w :restore: -O -w :archive_file: "db/archive.dump" :rails: :enabled: true :env: development
From the command line, type: db
db -C, [--create=OPTIONS] # Create new database. db -D, [--drop=OPTIONS] # Drop current database. db -F, [--fresh] # Create fresh database from scratch (i.e. drop, create, migrate, and seed). db -M, [--remigrate=OPTIONS] # Rebuild current database from new migrations. db -c, [--config] # Manage gem configuration. db -d, [--dump=OPTIONS] # Dump current database to archive file. db -h, [--help=COMMAND] # Show this message or get help for a command. db -i, [--import] # Import archive data into current database (i.e. drop, create, restore, and migrate). db -m, [--migrate] # Execute migrations for current database. db -r, [--restore=OPTIONS] # Restore current database from archive file. db -v, [--version] # Show gem version.
For configuration options, type: db --help --config:
-e, [--edit], [--no-edit] # Edit gem configuration. -i, [--info], [--no-info] # Print gem configuration.
For remigration options, type: db --help --remigrate:
-s, [--setup], [--no-setup] # Prepare existing migrations for remigration process. -g, [--generator], [--no-generator] # Create the remigration generator based on new migrations (as created during setup). -e, [--execute], [--no-execute] # Execute the remigration process. -c, [--clean], [--no-clean] # Clean excess remigration files created during the setup and generator steps. -r, [--restore], [--no-restore] # Revert database migrations to original state (i.e. reverses setup).
For those with Ruby on Rails projects, you might find this a welcomed tool for cleaning and maintaining your migrations. The problem with database migrations is that they tend to get cluttered over time. Instead of nice create migrations you eventually get drop, rename, add, etc. migrations mixed with your create migrations (think of this as migration fragmentation). This can also make your table schemas messy to read depending on how many changes you have applied to a table over time. Instead of being stuck with this clutter, the remigration process allows you to clean up previous migration fragments and consolidate them into create migrations instead so that you have a directory of create migration files with a better looking database schema to boot.
To remigrate a database:
db --remigrate --setup
- Remove excess migrations by manually applying changes to existing "create" migrations.
db --remigrate --generator
- Edit the "remigrate_generator" and reorder the migrations (if necessary, otherwise move to the next step).
db --remigrate --execute
db --remigrate --clean
Remigration is a multi-step process due to the fact that you need to have breakpoints where you can customize and tweak your migrations as necessary. You'll want to perform all six steps each time you undergo remigration. Should you get cold feet and decide against remigration, run the following command to restore to original state:
db --remigrate --restore
To test, run:
bundle exec rake
Read Semantic Versioning for details. Briefly, it means:
- Major (X.y.z) - Incremented for any backwards incompatible public API changes.
- Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
- Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
Code of Conduct
Please note that this project is released with a CODE OF CONDUCT. By participating in this project you agree to abide by its terms.
Read CONTRIBUTING for details.