func (p *SQLiteSuite) Test_SQLite_RenameIndex() { r := p.Require() ddl := `DROP INDEX IF EXISTS "old_ix"; CREATE UNIQUE INDEX "new_ix" ON "users" (id, created_at);` schema.schema["users"] = &fizz.Table{ Name: "users", Columns: []fizz.Column{ fizz.ID_COL, fizz.CREATED_COL, fizz.UPDATED_COL, }, Indexes: []fizz.Index{ { Name: "old_ix", Columns: []string{"id", "created_at"}, Unique: true, }, }, } res, _ := fizz.AString(`rename_index("users", "old_ix", "new_ix")`, sqt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_AddIndex_CustomName() { r := p.Require() ddl := `CREATE INDEX email_index ON users (email);` res, _ := fizz.AString(`add_index("users", "email", {"name": "email_index"})`, myt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_DropIndex() { r := p.Require() ddl := `DROP INDEX email_idx ON users;` res, _ := fizz.AString(`drop_index("users", "email_idx")`, myt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_AddIndex_Unique() { r := p.Require() ddl := `CREATE UNIQUE INDEX users_email_idx ON users (email);` res, _ := fizz.AString(`add_index("users", "email", {"unique": true})`, myt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_CreateTable() { r := p.Require() ddl := `CREATE TABLE users ( id integer NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, first_name VARCHAR (255) NOT NULL, last_name VARCHAR (255) NOT NULL, email VARCHAR (20) NOT NULL, permissions text, age integer DEFAULT 40 ) ENGINE=InnoDB;` res, _ := fizz.AString(` create_table("users", func(t) { t.Column("first_name", "string", {}) t.Column("last_name", "string", {}) t.Column("email", "string", {"size":20}) t.Column("permissions", "text", {"null": true}) t.Column("age", "integer", {"null": true, "default": 40}) }) `, myt) r.Equal(ddl, res) }
func (p *PostgreSQLSuite) Test_Postgres_DropIndex() { r := p.Require() ddl := `DROP INDEX "my_idx";` res, _ := fizz.AString(`drop_index("users", "my_idx")`, pgt) r.Equal(ddl, res) }
func (p *SQLiteSuite) Test_SQLite_DropIndex() { r := p.Require() ddl := `DROP INDEX IF EXISTS "my_idx";` res, _ := fizz.AString(`drop_index("my_table", "my_idx")`, sqt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_AddIndex_MultiColumn() { r := p.Require() ddl := `CREATE INDEX users_id_email_idx ON users (id, email);` res, _ := fizz.AString(`add_index("users", ["id", "email"], {})`, myt) r.Equal(ddl, res) }
func (p *SQLiteSuite) Test_SQLite_AddIndex_MultiColumn() { r := p.Require() ddl := `CREATE INDEX "table_name_col1_col2_col3_idx" ON "table_name" (col1, col2, col3);` res, _ := fizz.AString(`add_index("table_name", ["col1", "col2", "col3"], {})`, sqt) r.Equal(ddl, res) }
func (p *SQLiteSuite) Test_SQLite_AddIndex_CustomName() { r := p.Require() ddl := `CREATE INDEX "custom_name" ON "table_name" (column_name);` res, _ := fizz.AString(`add_index("table_name", "column_name", {"name": "custom_name"})`, sqt) r.Equal(ddl, res) }
func (p *SQLiteSuite) Test_SQLite_AddIndex_Unique() { r := p.Require() ddl := `CREATE UNIQUE INDEX "table_name_column_name_idx" ON "table_name" (column_name);` res, _ := fizz.AString(`add_index("table_name", "column_name", {"unique": true})`, sqt) r.Equal(ddl, res) }
func (p *PostgreSQLSuite) Test_Postgres_AddIndex() { r := p.Require() ddl := `CREATE INDEX "table_name_column_name_idx" ON "table_name" (column_name);` res, _ := fizz.AString(`add_index("table_name", "column_name", {})`, pgt) r.Equal(ddl, res) }
func (p *PostgreSQLSuite) Test_Postgres_RenameColumn() { r := p.Require() ddl := `ALTER TABLE "table_name" RENAME COLUMN "old_column" TO "new_column";` res, _ := fizz.AString(`rename_column("table_name", "old_column", "new_column")`, pgt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_RenameColumn() { r := p.Require() ddl := `ALTER TABLE users CHANGE email email_address varchar(50) NOT NULL DEFAULT '*****@*****.**';` res, _ := fizz.AString(`rename_column("users", "email", "email_address")`, myt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_DropColumn() { r := p.Require() ddl := `ALTER TABLE users DROP COLUMN mycolumn;` res, _ := fizz.AString(`drop_column("users", "mycolumn")`, myt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_RenameIndex() { r := p.Require() ddl := `ALTER TABLE users RENAME INDEX email_idx TO email_address_ix;` res, _ := fizz.AString(`rename_index("users", "email_idx", "email_address_ix")`, myt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_DropTable() { r := p.Require() ddl := `DROP TABLE "users";` res, _ := fizz.AString(`drop_table("users")`, myt) r.Equal(ddl, res) }
func (p *SQLiteSuite) Test_SQLite_DropTable() { r := p.Require() ddl := `DROP TABLE "users";` res, _ := fizz.AString(`drop_table("users")`, sqt) r.Equal(ddl, res) }
func (p *PostgreSQLSuite) Test_Postgres_DropColumn() { r := p.Require() ddl := `ALTER TABLE "table_name" DROP COLUMN "column_name";` res, _ := fizz.AString(`drop_column("table_name", "column_name")`, pgt) r.Equal(ddl, res) }
func (p *PostgreSQLSuite) Test_Postgres_DropTable() { r := p.Require() ddl := `DROP TABLE "users";` res, _ := fizz.AString(`drop_table("users")`, pgt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_RenameTable() { r := p.Require() ddl := `ALTER TABLE "users" RENAME TO "people";` res, _ := fizz.AString(`rename_table("users", "people")`, myt) r.Equal(ddl, res) }
func (p *PostgreSQLSuite) Test_Postgres_AddColumn() { r := p.Require() ddl := `ALTER TABLE "mytable" ADD COLUMN "mycolumn" VARCHAR (50) NOT NULL DEFAULT 'foo';` res, _ := fizz.AString(`add_column("mytable", "mycolumn", "string", {"default": "foo", "size": 50})`, pgt) r.Equal(ddl, res) }
func (p *MySQLSuite) Test_MySQL_AddColumn() { r := p.Require() ddl := `ALTER TABLE users ADD COLUMN mycolumn VARCHAR (50) NOT NULL DEFAULT 'foo';` res, _ := fizz.AString(`add_column("users", "mycolumn", "string", {"default": "foo", "size": 50})`, myt) r.Equal(ddl, res) }
func (p *PostgreSQLSuite) Test_Postgres_RenameIndex() { r := p.Require() ddl := `ALTER INDEX "old_ix" RENAME TO "new_ix";` res, _ := fizz.AString(`rename_index("table", "old_ix", "new_ix")`, pgt) r.Equal(ddl, res) }
func (p *SQLiteSuite) Test_SQLite_RenameTable() { r := p.Require() ddl := `ALTER TABLE "users" RENAME TO "people";` schema.schema["users"] = &fizz.Table{} res, _ := fizz.AString(`rename_table("users", "people")`, sqt) r.Equal(ddl, res) }
func (p *SQLiteSuite) Test_SQLite_AddColumn() { r := p.Require() ddl := `ALTER TABLE "users" ADD COLUMN "mycolumn" TEXT NOT NULL DEFAULT 'foo';` schema.schema["users"] = &fizz.Table{} res, _ := fizz.AString(`add_column("users", "mycolumn", "string", {"default": "foo", "size": 50})`, sqt) r.Equal(ddl, res) }
func (p *SQLiteSuite) Test_SQLite_DropColumn() { r := p.Require() ddl := `ALTER TABLE "users" RENAME TO "_users_tmp"; CREATE TABLE "users" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT, "updated_at" DATETIME NOT NULL ); INSERT INTO "users" (id, updated_at) SELECT id, updated_at FROM "_users_tmp"; DROP TABLE "_users_tmp";` schema.schema["users"] = &fizz.Table{ Name: "users", Columns: []fizz.Column{ fizz.ID_COL, fizz.CREATED_COL, fizz.UPDATED_COL, }, } res, _ := fizz.AString(`drop_column("users", "created_at")`, sqt) r.Equal(ddl, res) }
func (p *PostgreSQLSuite) Test_Postgres_CreateTable() { r := p.Require() ddl := `CREATE TABLE "users" ( "id" SERIAL PRIMARY KEY, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, "first_name" VARCHAR (255) NOT NULL, "last_name" VARCHAR (255) NOT NULL, "email" VARCHAR (20) NOT NULL, "permissions" jsonb, "age" integer DEFAULT '40' );` res, _ := fizz.AString(` create_table("users", func(t) { t.Column("first_name", "string", {}) t.Column("last_name", "string", {}) t.Column("email", "string", {"size":20}) t.Column("permissions", "jsonb", {"null": true}) t.Column("age", "integer", {"null": true, "default": 40}) }) `, pgt) r.Equal(ddl, res) }
func (p *SQLiteSuite) Test_SQLite_CreateTable() { r := p.Require() ddl := `CREATE TABLE "users" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT, "created_at" DATETIME NOT NULL, "updated_at" DATETIME NOT NULL, "first_name" TEXT NOT NULL, "last_name" TEXT NOT NULL, "email" TEXT NOT NULL, "permissions" text, "age" integer DEFAULT '40' );` res, _ := fizz.AString(` create_table("users", func(t) { t.Column("first_name", "string", {}) t.Column("last_name", "string", {}) t.Column("email", "string", {"size":20}) t.Column("permissions", "text", {"null": true}) t.Column("age", "integer", {"null": true, "default": 40}) }) `, sqt) r.Equal(ddl, res) }
func (m migrationFile) Content(c *Connection) (string, error) { b, err := ioutil.ReadFile(m.Path) if err != nil { return "", nil } content := string(b) ext := path.Ext(m.FileName) t := template.Must(template.New("sql").Parse(content)) var bb bytes.Buffer err = t.Execute(&bb, c.Dialect.Details()) if err != nil { return "", errors.Wrapf(err, "could not execute migration template %s", m.Path) } content = bb.String() if ext == ".fizz" { content, err = fizz.AString(content, c.Dialect.FizzTranslator()) if err != nil { return "", errors.Wrapf(err, "could not fizz the migration %s", m.Path) } } return content, nil }