A command line interface for database management.
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.github
bin
lib
spec
.codeclimate.yml
.gitignore
.rubocop.yml
.rubocop_todo.yml
.ruby-version
.todo.reek
.travis.yml
CHANGES.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Gemfile
Guardfile
LICENSE.md
README.md
Rakefile
db.gemspec

README.md

DB

Gem Version Code Climate GPA Code Climate Coverage Gemnasium Status Travis CI Status Patreon

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

Features

  1. Faster execution when creating/dropping databases than the default Ruby on Rails rake tasks.
  2. Adds support for dumping and restoring of a database.
  3. Adds configurable settings for common database operations.
  4. Adds support for easier remigration/rebuilding of a database with many migration fragments.

Requirements

  1. Ruby 2.x.x.
  2. A strong understanding of database management, migrations, dumps/restores, etc.

Setup

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:

~/.dbrc

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

Usage

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).

Remigration

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:

  1. Run: db --remigrate --setup
  2. Remove excess migrations by manually applying changes to existing "create" migrations.
  3. Run: db --remigrate --generator
  4. Edit the "remigrate_generator" and reorder the migrations (if necessary, otherwise move to the next step).
  5. Run: db --remigrate --execute
  6. Run: 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

Tests

To test, run:

bundle exec rake

Versioning

Read Semantic Versioning for details. Briefly, it means:

  • Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
  • Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
  • Major (X.y.z) - Incremented for any backwards incompatible public API changes.

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.

Contributions

Read CONTRIBUTING for details.

License

Copyright (c) 2011 Alchemists. Read LICENSE for details.

History

Read CHANGES for details. Built with Gemsmith.

Credits

Developed by Brooke Kuhlmann at Alchemists.