database-migrator

Command-line management tools for managing database schema, inspired by Rails Active Record, works like rake db:migrate
.
Read Active Record Migrations in Rails Guide to know the principle of database migration. The difference have to be aware is that database-migrator
takes SQL files directly instead of model definitions in Rails.
Installation
Install from Source Code
$ git clone git@github.com:pengsrc/database-migrator.git
$ cd database-migrator
$ glide install
...
[INFO] Replacing existing vendor dependencies
$ make install
...
Installing into /data/go/bin/database-migrator...
Done
Download Precompiled Binary
- Go to releases tab and download the binary for your operating system.
- Unarchive the downloaded file, and put the executable file
database-migrator
into a directory that in the $PATH
environment variable, for example /usr/local/bin
.
- Run
database-migrator --help
to get started.
Usage
The database-migrator
contains some sub commands, each of them support --help
flag to print out all supported parameters.
$ database-migrator --help
Database schema migration tool
Usage:
database-migrator [flags]
database-migrator [command]
Available Commands:
down Revert one migration
help Help about any command
status Show database schema status
sync Sync database schema
up Run one migration
Flags:
-h, --help help for database-migrator
Use "database-migrator [command] --help" for more information about a command.
$ database-migrator sync --help
Sync database schema
Usage:
database-migrator sync [flags]
Flags:
-d, --database string Specify database name
--dialect string Specify database dialect (default "MySQL")
--help Show help
-h, --host string Specify database host (default "127.0.0.1")
-m, --migrations string Specify migration files
-P, --password string Specify password
-p, --port int Specify database port (default 3306)
-u, --user string Specify user (default "root")
Example
Migration Files
Create a directories to keep migration files, and create a migration file.
$ mkdir migrations
$ database-migrator new -m migrations -n create_objects
New migration created at "migrations/20180425065739_create_objects.sql".
$ cat migrations/20180425065739_create_objects.sql
-- +migrate Up
-- SQL in section 'Up' is executed when this migration is applied.
CREATE TABLE `examples` (
`id` bigint(11) UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
AUTO_INCREMENT = 10000
DEFAULT CHARSET = utf8;
-- +migrate Down
-- SQL section 'Down' is executed when this migration is rolled back.
DROP TABLE `examples`;
The Up
and Down
section are pretty much similar to the up
and down
method in Rails model, see Using the up/down Methods to get a better understanding.
Run
Check current status.
$ database-migrator status -h 127.0.0.1 -p 3306 -u root -P root -d test -m migrations
Applied At Migration
================================================================================
Pending - 20180425065739_create_objects
Sync status.
$ database-migrator sync -h 127.0.0.1 -p 3306 -u root -P root -d test -m migrations
Migrated to the latest database schema.
- 20180425065739_create_objects
$ database-migrator status -h 127.0.0.1 -p 3306 -u root -P root -d test -m migrations
Applied At Migration
================================================================================
2018-04-25T07:04:27Z - 20180425065739_create_objects
Apply/Revert one by one.
$ database-migrator down -h 127.0.0.1 -p 3306 -u root -P root -d test -m migrations
Revert migration complete, 20180425065739_create_objects.
$ database-migrator up -h 127.0.0.1 -p 3306 -u root -P root -d test -m migrations
Run migration complete, 20180425065739_create_objects.
Notices
- The
database-migrator
creates a table named schema_migrations
to track schema history.
- Currently, only MySQL direct is supported.
LICENSE
The Apache License (Version 2.0, January 2004).