Skip to content
/ migrate Public

A simple migration helper for Go's database/sql package.

License

Notifications You must be signed in to change notification settings

noonat/migrate

Repository files navigation

migrate

godoc travis report

Package migrate provides helpers for running SQL database migrations. It's designed for migrations that are specified in code and distributed as part of the application binary, and applied as part of the application startup (rather than via external files and an external tool).

Usage

You can read the package documentation for more information, but here is a simple example:

adapter := migrate.NewPostgreSQLAdapter(log.Printf)
db, err := sql.Open("migrate_test", "")
if err != nil {
    log.Panic(err)
}

migrations := []migrate.Migration{
    {
        Comment: "Add user and app tables",
        Up: migrate.ExecQueries([]string{
            `CREATE TABLE users (
                id SERIAL PRIMARY KEY,
                username VARCHAR(50) NOT NULL)`,
            `CREATE TABLE apps (
                id SERIAL PRIMARY KEY,
                title VARCHAR(100) NOT NULL)`,
        }),
        Down: migrate.ExecQueries([]string{
            `DROP TABLE apps`,
            `DROP TABLE users`,
        }),
    },
    {
        Comment: "Add user app join table",
        Up: migrate.ExecQueries([]string{
            `CREATE TABLE user_apps (
                user_id INT NOT NULL REFERENCES users(id),
                app_id INT NOT NULL REFERENCES apps(id),
                UNIQUE (user_id, app_id))`,
        }),
        Down: migrate.ExecQueries([]string{
            `DROP TABLE user_apps`,
        }),
    },
}

err = migrate.Up(context.Background(), db, adapter, migrations)
if err != nil {
    log.Panicf("error running migrations: %s", err)
}

License

MIT

Releases

No releases published

Packages

No packages published

Languages