Generate difference sql of two mysql schema
Clone or download
Latest commit 1c8484e Oct 8, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cmd schemadiff command to show diff between schemas Feb 9, 2018
diff Fix various issues on #59 Oct 5, 2018
format Accept sort direction on column names for indexes Oct 6, 2018
internal include ASC/DESC tokens Oct 6, 2018
lint Shuffle stuff around so it's easier to reuse Jan 22, 2018
model Accept sort direction on column names for indexes Oct 6, 2018
scripts Add script to check diffs after make generate is called Jan 25, 2018
.gitignore ignore glide Jan 25, 2018
.travis.yml use go 1.11 on travis Oct 9, 2018
Changes update Changes Oct 9, 2018
LICENSE add LICENSE Jun 17, 2015
Makefile Add schemadiff command into release file Oct 6, 2018
README.md English Oct 10, 2017
errors.go Hide the ParseError structure behind an interface Jan 7, 2017
example_test.go Fixed tests for ADD COLUMN diff query. Feb 9, 2018
glide.lock Add mysql driver to glide Oct 6, 2017
glide.yaml Add mysql driver to glide Oct 6, 2017
interface.go Shuffle stuff around so it's easier to reuse Jan 22, 2018
lexer.go Treat "\r" as a space character Oct 6, 2018
lexer_test.go Remove references to golang.org/x/net/context Sep 20, 2017
parser.go Accept sort direction on column names for indexes Oct 6, 2018
parser_test.go Accept sort direction on column names for indexes Oct 6, 2018
schemalex.go bump version Oct 9, 2018
source.go Move the location of the docs to a publicly available method Oct 9, 2017
source_test.go Avoid 'Error call has possible formatting directive %s'. Dec 8, 2017
tokens_gen.go include ASC/DESC tokens Oct 6, 2018

README.md

schemalex

Generate the difference of two mysql schema

Build Status

GoDoc

SYNOPSIS

This tool can be used to generate the difference, or more precisely, the statements required to migrate from/to, between two MySQL schema.

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;

You can also use URI formatted strings as the sources to compare, which allow you to compare local files against online schema, a version committed to your git repository against another version, etc.

Please see the help command for a list

schemalex -version
schemalex [options...] before after

-v            Print out the version and exit
-o file	      Output the result to the specified file (default: stdout)
-t[=true]     Enable/Disable transaction in the output (default: true)

"before" and "after" may be a file path, or a URI.
Special URI schemes "mysql" and "local-git" are supported on top of
"file". If the special path "-" is used, it is treated as stdin

Examples:

* Compare local files
  schemalex /path/to/file /another/path/to/file
  schemalex file:///path/to/file /another/path/to/file

* Compare local file against online mysql schema
  schemalex /path/to/file "mysql://user:password@tcp(host:port)/dbname?option=value"

* Compare file in local git repository against local file
  schemalex local-git:///path/to/repo?file=foo.sql&commitish=deadbeaf /path/to/file

* Compare schema from stdin against local file
	.... | schemalex - /path/to/file

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