• Stars
    star
    453
  • Rank 96,573 (Top 2 %)
  • Language
    Go
  • License
    The Unlicense
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple and language-independent SQL schema migration tool

rambler

build Coverage Status

A simple and language-independent SQL schema migration tool

Installation

You can download the latest release on the release page of the project.

Go users can also simply compile it from source and install it as a go executable using the following command :

go install github.com/elwinar/rambler

Releases are compiled using the wonderful XGo. Don't hesitate to check it out, it really kicks some serious ass.

Usage

Migrations

In rambler, migrations are kept in the simplest form possible: a migration is a list of sections (up and down), each section being an SQL statement. Example:

-- rambler up

CREATE TABLE foo (
	id INTEGER UNSIGNED AUTO_INCREMENT,
	bar VARCHAR(60),
	PRIMARY KEY (id)
);

-- rambler down

DROP TABLE foo;

Sections are delimited by SQL comments suffixed by the rambler marker (white-spaces sensitive). While applying a migration, rambler will execute each up section in order, and while reversing it it will execute each down section in reverse order.

Migrations are executed in alphabetical order, thus a versioning scheme of the form version_description.sql is highly recommended, version being an integer value, and description an underscored string. Examples:

  • 201409272258_Added_table_foo.sql
  • 01_First_migration.sql

The migrations applied to the database are stored in a table named migration (can be changed with the table configuration option).

Configuration

Rambler configuration is lightweight: just dump the credentials of your database and the path to your migrations' directory into a JSON file, and you're done. Here is an example or JSON configuration file with the default values of rambler:

{
	"database": "",
	"directory": ".",
	"driver": "mysql",
	"host": "localhost",
	"password": "",
	"port": 3306,
	"protocol": "tcp",
	"schema": "public",
	"sslmode": "disable"
	"table": "migrations",
	"user": "root",
}

When running, rambler will try to find a configuration file in the working directory and use its values to connect to the managed database.

HJSON

Rambler supports HJSON configuration files, which is by the way retrocompatible with JSON.

Environment Variables

Alternatively, Rambler can read configuration from environment variables. The environment variables can override any of the confifuration file values and are prefixed with RAMBLER_.

Env Var Config
RAMBLER_DATABASE database
RAMBLER_DIRECTORY directory
RAMBLER_DRIVER driver
RAMBLER_HOST host
RAMBLER_PASSWORD password
RAMBLER_PORT port
RAMBLER_PROTOCOL protocol
RAMBLER_SCHEMA schema
RAMBLER_SSLMODE sslmode
RAMBLER_TABLE table
RAMBLER_USER user

Drivers

Rambler supports actually 3 drivers:

  • mysql
  • postgresql
  • sqlite

Don't hesitate to get in touch if you want to see another one supported, provided a golang database/sql driver exist for your database vendor.

Note that some configuration options only apply to some drivers.

Applying a migration

To apply a migration, use the apply command.

rambler apply

Rambler will compare the migrations already applied and the available migrations in increasing order to find the next migration to apply, then execute all its up sections' statements in order.

Reversing a migration

To reverse a migration, use the reverse command.

rambler reverse

Rambler will compare the migrations already applied and the available migrations in decreasing order to find the last applied migrations, then execute all its down sections' statements in reverse order.

Options

  • --all, -a repeat the command until there is no more migration to apply/reverse. This flag is exclusive with --migration.
  • --no-save doesn't save the applied/reversed migration. This option is mainly destined to allow faster iteration when writing the migration.
  • --migration bypass the migration automatic discovery and apply the migration whose path is given. This is exclusive with --all.

Errors

To ensure database schema consistency, rambler will complain and stop when encountering a new migration in the middle of the already existing ones or if it can't find a migration already applied.

Environments

An environment is an additional configuration that is given a name, and can be used to create multiple configurations for a single application (for example, to differentiate production, testing, etc).

Environments are defined in the configuration file, under the environments item. Each environment is defined as an attribute of this item, the key being the name and the value being the configuration options.

Environments configuration are derived from the default configuration of rambler (at the configuration file's root), so you only need to override the needed options:

{
    "driver": "mysql",
    "protocol": "tcp",
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "",
    "database": "rambler_default",
    "directory": "migrations",
    "table": "migrations",
    "environments": {
        "development": {
            "database": "rambler_development"
        },
        "testing": {
            "database": "rambler_testing"
        }
    }
}

Here we have three environments defined:

  • default, will use the rambler_default database,
  • development, will use the rambler_development database,
  • testing, will use the rambler_testing database;

Logging

Rambler will log a few important informations for monitoring what is happening on stdout. If you suspect something of being wrong, you can also use the debug mode by adding --debug to your command line.

Dry-run

The --dry-run flag will print statements instead of executing them.

CONTRIBUTORS

Feedback and contributions

Feel free to give feedback, make pull requests or simply open issues if you find a bug or have an idea.

More Repositories

1

rcoredump

Remote indexation tools for coredump
Go
18
star
2

phototrail

Go
2
star
3

votrederniermot

Go
2
star
4

meetup-modern-devops

Slide deck for the modern devops meetup
CSS
1
star
5

base16-mycli

HTML
1
star
6

valiant

A brave mail pigeon
Go
1
star
7

token

JWT tokens that don't suck
Go
1
star
8

smartfocus

PHP classes for the SmartFocus API
PHP
1
star
9

butler

A PHP CLI utilities toolbox
PHP
1
star
10

mawsworn

Shell
1
star
11

the-resistance

Go
1
star
12

kimsufi-crawler

Kimsufi crawler that notifies using desktop native notifications
Go
1
star
13

grimoire

1
star
14

chronicler

Console game tracker for Warmachine/Hordes
Python
1
star
15

wtc

Warmachine/Hordes World Team Championship statistical analysis
Go
1
star
16

boxes

Definitions, configurations & co for various VM-related tools
Shell
1
star
17

minit

Minimal Inheritance Templating engine for Laravel 5
PHP
1
star
18

axial-synergies-analyzer

Automatically exported from code.google.com/p/axial-synergies-analyzer
C++
1
star
19

heirloom

Template inheritance wrapper around golang's html/template library
Go
1
star
20

babel

Laravel 5 wrappers & engines for markup languages
PHP
1
star
21

anvilward

Dwarf spy
Go
1
star
22

b16m

base16-manager type that don't suck
Go
1
star
23

dotfiles

Personnal Dotfiles repository
Shell
1
star
24

fizzbuzz

Go
1
star
25

temporaire

Hack
1
star
26

advent-of-code

http://adventofcode.com/
COBOL
1
star
27

simon

Dumb chat client/server
C
1
star
28

semaphor

A CLI tool to get eve central data
PHP
1
star
29

euler

Personnal Euler Project solutions
1
star
30

adeptus

Command-line character sheet tracking tool for the Dark Heresy system.
Go
1
star
31

base16-fish

Shell
1
star
32

elwinar

Sources of http://elwinar.com
Go
1
star
33

slave-password-generator

A clever password generating tool
C++
1
star
34

rambler-website

presentation website for rambler
JavaScript
1
star