Generate difference sql of two mysql schema
Go Makefile
Latest commit 6ee98d3 Feb 18, 2017 @soh335 soh335 committed on GitHub Merge pull request #25 from ken39arg/hotfix/diff-nochange-foreign-key
Hotfix/diff nochange foreign key
Permalink
Failed to load latest commit information.
cmd/schemalex Getting ready for a binary release Jan 9, 2017
diff
format Change how to identify an index Feb 17, 2017
internal lint Jan 14, 2017
model fix change symbol name Feb 17, 2017
.travis.yml tweak Jan 5, 2017
Changes update Changes Feb 15, 2017
LICENSE add LICENSE Jun 17, 2015
Makefile minor tweak Jan 9, 2017
README.md Getting ready for a binary release Jan 9, 2017
errors.go Hide the ParseError structure behind an interface Jan 7, 2017
example_test.go Fix paths Jan 6, 2017
glide.lock change lexer/parser Jan 6, 2017
glide.yaml
lexer.go lint Jan 9, 2017
lexer_test.go lint Jan 9, 2017
parser.go Change how to identify an index Feb 17, 2017
parser_test.go add test. when create table with option like comment, then option val… Feb 15, 2017
schemalex.go bump version Feb 15, 2017
token.go

README.md

schemalex

Generate difference sql of two mysql schema

Build Status

GoDoc

SYNOPSIS (COMMAND LINE)

Suppose you have an existing SQL schema like the following:

CREATE TABLE hoge (
    id INTEGER NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (id)
);

And you want "upgrade" your schema to the following:

CREATE TABLE hoge (
    id INTEGER NOT NULL AUTO_INCREMENT,
    c VARCHAR (20) NOT NULL DEFAULT "hoge",
    PRIMARY KEY (id)
);

CREATE TABLE fuga (
    id INTEGER NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (id)
);

Using schemalex you can generate a set of commands to perform the migration:

schemalex old.sql new.sql

SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE `fuga` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
);

ALTER TABLE `hoge` ADD COLUMN `c` VARCHAR (20) NOT NULL DEFAULT "hoge";

SET FOREIGN_KEY_CHECKS = 1;

COMMIT;

SYNOPSIS (Using the library)

Below is the equivalent of the previous SYNOPSIS.

package schemalex_test

import (
    "os"

    "github.com/schemalex/schemalex/diff"
)

func Example() {
    const sql1 = `CREATE TABLE hoge (
    id INTEGER NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (id)
);`
    const sql2 = `CREATE TABLE hoge (
    id INTEGER NOT NULL AUTO_INCREMENT,
    c VARCHAR (20) NOT NULL DEFAULT "hoge",
    PRIMARY KEY (id)
);

CREATE TABLE fuga (
    id INTEGER NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (id)
);`

    diff.Strings(os.Stdout, sql1, sql2, diff.WithTransaction(true))

    // OUTPUT:
    // BEGIN;
    //
    // SET FOREIGN_KEY_CHECKS = 0;
    //
    // CREATE TABLE `fuga` (
    // `id` INTEGER NOT NULL AUTO_INCREMENT,
    // PRIMARY KEY (`id`)
    // );
    //
    // ALTER TABLE `hoge` ADD COLUMN `c` VARCHAR (20) NOT NULL DEFAULT "hoge";
    //
    // SET FOREIGN_KEY_CHECKS = 1;
    //
    // COMMIT;
}

SEE ALSO

LICENSE

MIT