From f57f22e853fa196e6e0048979cedba8219fa41d1 Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Thu, 16 Aug 2018 08:10:11 +0200 Subject: [PATCH] Escape MySQL identifiers (#16) Related to https://github.com/gobuffalo/pop/issues/42 --- translators/cockroach.go | 8 +- translators/mysql.go | 78 ++++++++++------- translators/mysql_test.go | 171 +++++++++++++++++++++----------------- translators/postgres.go | 10 +-- translators/sqlite.go | 6 +- 5 files changed, 158 insertions(+), 115 deletions(-) diff --git a/translators/cockroach.go b/translators/cockroach.go index b1d2b52c..0912aa7d 100644 --- a/translators/cockroach.go +++ b/translators/cockroach.go @@ -89,7 +89,7 @@ func (p *Cockroach) RenameTable(t []fizz.Table) (string, error) { func (p *Cockroach) ChangeColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } c := t.Columns[0] @@ -130,7 +130,7 @@ func (p *Cockroach) ChangeColumn(t fizz.Table) (string, error) { func (p *Cockroach) AddColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } c := t.Columns[0] s := fmt.Sprintf("ALTER TABLE \"%s\" ADD COLUMN %s;COMMIT TRANSACTION;BEGIN TRANSACTION;", t.Name, p.buildAddColumn(c)) @@ -155,7 +155,7 @@ func (p *Cockroach) AddColumn(t fizz.Table) (string, error) { func (p *Cockroach) DropColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } c := t.Columns[0] p.Schema.DeleteColumn(t.Name, c.Name) @@ -164,7 +164,7 @@ func (p *Cockroach) DropColumn(t fizz.Table) (string, error) { func (p *Cockroach) RenameColumn(t fizz.Table) (string, error) { if len(t.Columns) < 2 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } oc := t.Columns[0] diff --git a/translators/mysql.go b/translators/mysql.go index 63d1c0e7..cbdf7ded 100644 --- a/translators/mysql.go +++ b/translators/mysql.go @@ -10,10 +10,12 @@ import ( "github.com/gobuffalo/fizz" ) +// MySQL is a MySQL-specific translator. type MySQL struct { Schema SchemaQuery } +// NewMySQL constructs a new MySQL translator. func NewMySQL(url, name string) *MySQL { schema := &mysqlSchema{Schema{URL: url, Name: name, schema: map[string]*fizz.Table{}}} schema.Builder = schema @@ -22,13 +24,14 @@ func NewMySQL(url, name string) *MySQL { } } +// CreateTable translates a fizz Table to its MySQL SQL definition. func (p *MySQL) CreateTable(t fizz.Table) (string, error) { sql := []string{} cols := []string{} for _, c := range t.Columns { cols = append(cols, p.buildColumn(c)) if c.Primary { - cols = append(cols, fmt.Sprintf("PRIMARY KEY(%s)", c.Name)) + cols = append(cols, fmt.Sprintf("PRIMARY KEY(`%s`)", c.Name)) } } @@ -36,7 +39,7 @@ func (p *MySQL) CreateTable(t fizz.Table) (string, error) { cols = append(cols, p.buildForeignKey(t, fk, true)) } - s := fmt.Sprintf("CREATE TABLE %s (\n%s\n) ENGINE=InnoDB;", t.Name, strings.Join(cols, ",\n")) + s := fmt.Sprintf("CREATE TABLE %s (\n%s\n) ENGINE=InnoDB;", p.escapeIdentifier(t.Name), strings.Join(cols, ",\n")) sql = append(sql, s) @@ -55,58 +58,58 @@ func (p *MySQL) CreateTable(t fizz.Table) (string, error) { } func (p *MySQL) DropTable(t fizz.Table) (string, error) { - return fmt.Sprintf("DROP TABLE %s;", t.Name), nil + return fmt.Sprintf("DROP TABLE %s;", p.escapeIdentifier(t.Name)), nil } func (p *MySQL) RenameTable(t []fizz.Table) (string, error) { if len(t) < 2 { - return "", errors.New("Not enough table names supplied!") + return "", errors.New("not enough table names supplied") } - return fmt.Sprintf("ALTER TABLE %s RENAME TO %s;", t[0].Name, t[1].Name), nil + return fmt.Sprintf("ALTER TABLE %s RENAME TO %s;", p.escapeIdentifier(t[0].Name), p.escapeIdentifier(t[1].Name)), nil } func (p *MySQL) ChangeColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } c := t.Columns[0] - s := fmt.Sprintf("ALTER TABLE %s MODIFY %s;", t.Name, p.buildColumn(c)) + s := fmt.Sprintf("ALTER TABLE %s MODIFY %s;", p.escapeIdentifier(t.Name), p.buildColumn(c)) return s, nil } func (p *MySQL) AddColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } if _, ok := t.Columns[0].Options["first"]; ok { c := t.Columns[0] - s := fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s FIRST;", t.Name, p.buildColumn(c)) + s := fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s FIRST;", p.escapeIdentifier(t.Name), p.buildColumn(c)) return s, nil } if val, ok := t.Columns[0].Options["after"]; ok { c := t.Columns[0] - s := fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s AFTER %s;", t.Name, p.buildColumn(c), val) + s := fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s AFTER `%s`;", p.escapeIdentifier(t.Name), p.buildColumn(c), val) return s, nil } c := t.Columns[0] - s := fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s;", t.Name, p.buildColumn(c)) + s := fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s;", p.escapeIdentifier(t.Name), p.buildColumn(c)) return s, nil } func (p *MySQL) DropColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } c := t.Columns[0] - return fmt.Sprintf("ALTER TABLE %s DROP COLUMN %s;", t.Name, c.Name), nil + return fmt.Sprintf("ALTER TABLE %s DROP COLUMN `%s`;", p.escapeIdentifier(t.Name), c.Name), nil } func (p *MySQL) RenameColumn(t fizz.Table) (string, error) { if len(t.Columns) < 2 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } oc := t.Columns[0] nc := t.Columns[1] @@ -123,16 +126,20 @@ func (p *MySQL) RenameColumn(t fizz.Table) (string, error) { } col := p.buildColumn(c) col = strings.Replace(col, oc.Name, fmt.Sprintf("%s %s", oc.Name, nc.Name), -1) - s := fmt.Sprintf("ALTER TABLE %s CHANGE %s;", t.Name, col) + s := fmt.Sprintf("ALTER TABLE %s CHANGE %s;", p.escapeIdentifier(t.Name), col) return s, nil } func (p *MySQL) AddIndex(t fizz.Table) (string, error) { if len(t.Indexes) == 0 { - return "", errors.New("Not enough indexes supplied!") + return "", errors.New("not enough indexes supplied") } i := t.Indexes[0] - s := fmt.Sprintf("CREATE INDEX %s ON %s (%s);", i.Name, t.Name, strings.Join(i.Columns, ", ")) + cols := []string{} + for _, c := range i.Columns { + cols = append(cols, fmt.Sprintf("`%s`", c)) + } + s := fmt.Sprintf("CREATE INDEX `%s` ON %s (%s);", i.Name, p.escapeIdentifier(t.Name), strings.Join(cols, ", ")) if i.Unique { s = strings.Replace(s, "CREATE", "CREATE UNIQUE", 1) } @@ -141,10 +148,10 @@ func (p *MySQL) AddIndex(t fizz.Table) (string, error) { func (p *MySQL) DropIndex(t fizz.Table) (string, error) { if len(t.Indexes) == 0 { - return "", errors.New("Not enough indexes supplied!") + return "", errors.New("not enough indexes supplied") } i := t.Indexes[0] - return fmt.Sprintf("DROP INDEX %s ON %s;", i.Name, t.Name), nil + return fmt.Sprintf("DROP INDEX `%s` ON %s;", i.Name, p.escapeIdentifier(t.Name)), nil } func (p *MySQL) RenameIndex(t fizz.Table) (string, error) { @@ -158,16 +165,16 @@ func (p *MySQL) RenameIndex(t fizz.Table) (string, error) { } ix := t.Indexes if len(ix) < 2 { - return "", errors.New("Not enough indexes supplied!") + return "", errors.New("not enough indexes supplied") } oi := ix[0] ni := ix[1] - return fmt.Sprintf("ALTER TABLE %s RENAME INDEX %s TO %s;", t.Name, oi.Name, ni.Name), nil + return fmt.Sprintf("ALTER TABLE %s RENAME INDEX `%s` TO `%s`;", p.escapeIdentifier(t.Name), oi.Name, ni.Name), nil } func (p *MySQL) AddForeignKey(t fizz.Table) (string, error) { if len(t.ForeignKeys) == 0 { - return "", errors.New("Not enough foreign keys supplied!") + return "", errors.New("not enough foreign keys supplied") } return p.buildForeignKey(t, t.ForeignKeys[0], false), nil @@ -175,7 +182,7 @@ func (p *MySQL) AddForeignKey(t fizz.Table) (string, error) { func (p *MySQL) DropForeignKey(t fizz.Table) (string, error) { if len(t.ForeignKeys) == 0 { - return "", errors.New("Not enough foreign keys supplied!") + return "", errors.New("not enough foreign keys supplied") } fk := t.ForeignKeys[0] @@ -185,12 +192,12 @@ func (p *MySQL) DropForeignKey(t fizz.Table) (string, error) { ifExists = "IF EXISTS" } - s := fmt.Sprintf("ALTER TABLE %s DROP FOREIGN KEY %s %s;", t.Name, ifExists, fk.Name) + s := fmt.Sprintf("ALTER TABLE %s DROP FOREIGN KEY %s `%s`;", p.escapeIdentifier(t.Name), ifExists, fk.Name) return s, nil } func (p *MySQL) buildColumn(c fizz.Column) string { - s := fmt.Sprintf("%s %s", c.Name, p.colType(c)) + s := fmt.Sprintf("`%s` %s", c.Name, p.colType(c)) if c.Options["null"] == nil || c.Primary { s = fmt.Sprintf("%s NOT NULL", s) } @@ -231,8 +238,12 @@ func (p *MySQL) colType(c fizz.Column) string { } func (p *MySQL) buildForeignKey(t fizz.Table, fk fizz.ForeignKey, onCreate bool) string { - refs := fmt.Sprintf("%s (%s)", fk.References.Table, strings.Join(fk.References.Columns, ", ")) - s := fmt.Sprintf("FOREIGN KEY (%s) REFERENCES %s", fk.Column, refs) + rcols := []string{} + for _, c := range fk.References.Columns { + rcols = append(rcols, fmt.Sprintf("`%s`", c)) + } + refs := fmt.Sprintf("%s (%s)", p.escapeIdentifier(fk.References.Table), strings.Join(rcols, ", ")) + s := fmt.Sprintf("FOREIGN KEY (`%s`) REFERENCES %s", fk.Column, refs) if onUpdate, ok := fk.Options["on_update"]; ok { s += fmt.Sprintf(" ON UPDATE %s", onUpdate) @@ -243,8 +254,19 @@ func (p *MySQL) buildForeignKey(t fizz.Table, fk fizz.ForeignKey, onCreate bool) } if !onCreate { - s = fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT %s %s;", t.Name, fk.Name, s) + s = fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT `%s` %s;", p.escapeIdentifier(t.Name), fk.Name, s) } return s } + +func (p *MySQL) escapeIdentifier(s string) string { + if !strings.ContainsRune(s, '.') { + return fmt.Sprintf("`%s`", s) + } + parts := strings.Split(s, ".") + for _, p := range parts { + p = fmt.Sprintf("`%s`", p) + } + return strings.Join(parts, ".") +} diff --git a/translators/mysql_test.go b/translators/mysql_test.go index a66cf018..14d856ed 100644 --- a/translators/mysql_test.go +++ b/translators/mysql_test.go @@ -3,6 +3,8 @@ package translators_test import ( "fmt" + // Load MySQL Go driver + _ "github.com/go-sql-driver/mysql" "github.com/gobuffalo/envy" "github.com/gobuffalo/fizz" "github.com/gobuffalo/fizz/translators" @@ -19,10 +21,10 @@ func init() { func (p *MySQLSuite) Test_MySQL_SchemaMigration() { r := p.Require() - ddl := `CREATE TABLE schema_migrations ( -version VARCHAR (255) NOT NULL + ddl := `CREATE TABLE ` + "`schema_migrations`" + ` ( +` + "`version`" + ` VARCHAR (255) NOT NULL ) ENGINE=InnoDB; -CREATE UNIQUE INDEX version_idx ON schema_migrations (version);` +CREATE UNIQUE INDEX ` + "`version_idx`" + ` ON ` + "`schema_migrations`" + ` (` + "`version`" + `);` res, err := myt.CreateTable(fizz.Table{ Name: "schema_migrations", @@ -39,20 +41,20 @@ CREATE UNIQUE INDEX version_idx ON schema_migrations (version);` func (p *MySQLSuite) Test_MySQL_CreateTable() { r := p.Require() - ddl := `CREATE TABLE users ( -id integer NOT NULL AUTO_INCREMENT, -PRIMARY KEY(id), -first_name VARCHAR (255) NOT NULL, -last_name VARCHAR (255) NOT NULL, -email VARCHAR (20) NOT NULL, -permissions text, -age integer DEFAULT 40, -raw BLOB NOT NULL, -created_at DATETIME NOT NULL, -updated_at DATETIME NOT NULL + ddl := `CREATE TABLE ` + "`users`" + ` ( +` + "`id`" + ` integer NOT NULL AUTO_INCREMENT, +PRIMARY KEY(` + "`id`" + `), +` + "`first_name`" + ` VARCHAR (255) NOT NULL, +` + "`last_name`" + ` VARCHAR (255) NOT NULL, +` + "`email`" + ` VARCHAR (20) NOT NULL, +` + "`permissions`" + ` text, +` + "`age`" + ` integer DEFAULT 40, +` + "`raw`" + ` BLOB NOT NULL, +` + "`created_at`" + ` DATETIME NOT NULL, +` + "`updated_at`" + ` DATETIME NOT NULL ) ENGINE=InnoDB;` - res, _ := fizz.AString(` + res, err := fizz.AString(` create_table("users") { t.Column("first_name", "string", {}) t.Column("last_name", "string", {}) @@ -62,25 +64,26 @@ updated_at DATETIME NOT NULL t.Column("raw", "blob", {}) } `, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_CreateTable_UUID() { r := p.Require() - ddl := `CREATE TABLE users ( -first_name VARCHAR (255) NOT NULL, -last_name VARCHAR (255) NOT NULL, -email VARCHAR (20) NOT NULL, -permissions text, -age integer DEFAULT 40, -company_id char(36) NOT NULL DEFAULT 'test', -uuid char(36) NOT NULL, -PRIMARY KEY(uuid), -created_at DATETIME NOT NULL, -updated_at DATETIME NOT NULL + ddl := `CREATE TABLE ` + "`users`" + ` ( +` + "`first_name`" + ` VARCHAR (255) NOT NULL, +` + "`last_name`" + ` VARCHAR (255) NOT NULL, +` + "`email`" + ` VARCHAR (20) NOT NULL, +` + "`permissions`" + ` text, +` + "`age`" + ` integer DEFAULT 40, +` + "`company_id`" + ` char(36) NOT NULL DEFAULT 'test', +` + "`uuid`" + ` char(36) NOT NULL, +PRIMARY KEY(` + "`uuid`" + `), +` + "`created_at`" + ` DATETIME NOT NULL, +` + "`updated_at`" + ` DATETIME NOT NULL ) ENGINE=InnoDB;` - res, _ := fizz.AString(` + res, err := fizz.AString(` create_table("users") { t.Column("first_name", "string", {}) t.Column("last_name", "string", {}) @@ -91,30 +94,31 @@ updated_at DATETIME NOT NULL t.Column("uuid", "uuid", {"primary": true}) } `, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_CreateTables_WithForeignKeys() { r := p.Require() - ddl := `CREATE TABLE users ( -id INT NOT NULL AUTO_INCREMENT, -PRIMARY KEY(id), -email VARCHAR (20) NOT NULL, -created_at DATETIME NOT NULL, -updated_at DATETIME NOT NULL + ddl := `CREATE TABLE ` + "`users`" + ` ( +` + "`id`" + ` INT NOT NULL AUTO_INCREMENT, +PRIMARY KEY(` + "`id`" + `), +` + "`email`" + ` VARCHAR (20) NOT NULL, +` + "`created_at`" + ` DATETIME NOT NULL, +` + "`updated_at`" + ` DATETIME NOT NULL ) ENGINE=InnoDB; -CREATE TABLE profiles ( -id INT NOT NULL AUTO_INCREMENT, -PRIMARY KEY(id), -user_id INT NOT NULL, -first_name VARCHAR (255) NOT NULL, -last_name VARCHAR (255) NOT NULL, -created_at DATETIME NOT NULL, -updated_at DATETIME NOT NULL, -FOREIGN KEY (user_id) REFERENCES users (id) +CREATE TABLE ` + "`profiles`" + ` ( +` + "`id`" + ` INT NOT NULL AUTO_INCREMENT, +PRIMARY KEY(` + "`id`" + `), +` + "`user_id`" + ` INT NOT NULL, +` + "`first_name`" + ` VARCHAR (255) NOT NULL, +` + "`last_name`" + ` VARCHAR (255) NOT NULL, +` + "`created_at`" + ` DATETIME NOT NULL, +` + "`updated_at`" + ` DATETIME NOT NULL, +FOREIGN KEY (` + "`user_id`" + `) REFERENCES ` + "`users`" + ` (` + "`id`" + `) ) ENGINE=InnoDB;` - res, _ := fizz.AString(` + res, err := fizz.AString(` create_table("users") { t.Column("id", "INT", {"primary": true}) t.Column("email", "string", {"size":20}) @@ -127,24 +131,27 @@ FOREIGN KEY (user_id) REFERENCES users (id) t.ForeignKey("user_id", {"users": ["id"]}, {}) } `, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_DropTable() { r := p.Require() - ddl := `DROP TABLE users;` + ddl := `DROP TABLE ` + "`users`" + `;` - res, _ := fizz.AString(`drop_table("users")`, myt) + res, err := fizz.AString(`drop_table("users")`, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_RenameTable() { r := p.Require() - ddl := `ALTER TABLE users RENAME TO people;` + ddl := `ALTER TABLE ` + "`users`" + ` RENAME TO ` + "`people`" + `;` - res, _ := fizz.AString(`rename_table("users", "people")`, myt) + res, err := fizz.AString(`rename_table("users", "people")`, myt) + r.NoError(err) r.Equal(ddl, res) } @@ -157,118 +164,132 @@ func (p *MySQLSuite) Test_MySQL_RenameTable_NotEnoughValues() { func (p *MySQLSuite) Test_MySQL_ChangeColumn() { r := p.Require() - ddl := `ALTER TABLE users MODIFY mycolumn VARCHAR (50) NOT NULL DEFAULT 'foo';` + ddl := `ALTER TABLE ` + "`users`" + ` MODIFY ` + "`mycolumn`" + ` VARCHAR (50) NOT NULL DEFAULT 'foo';` - res, _ := fizz.AString(`change_column("users", "mycolumn", "string", {"default": "foo", "size": 50})`, myt) + res, err := fizz.AString(`change_column("users", "mycolumn", "string", {"default": "foo", "size": 50})`, myt) + r.NoError(err) 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';` + 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) + res, err := fizz.AString(`add_column("users", "mycolumn", "string", {"default": "foo", "size": 50})`, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_AddColumnAfter() { r := p.Require() - ddl := `ALTER TABLE users ADD COLUMN mycolumn VARCHAR (50) NOT NULL DEFAULT 'foo' AFTER created_at;` + ddl := `ALTER TABLE ` + "`users`" + ` ADD COLUMN ` + "`mycolumn`" + ` VARCHAR (50) NOT NULL DEFAULT 'foo' AFTER ` + "`created_at`" + `;` - res, _ := fizz.AString(`add_column("users", "mycolumn", "string", {"default": "foo", "size": 50, "after":"created_at"})`, myt) + res, err := fizz.AString(`add_column("users", "mycolumn", "string", {"default": "foo", "size": 50, "after":"created_at"})`, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_AddColumnFirst() { r := p.Require() - ddl := `ALTER TABLE users ADD COLUMN mycolumn VARCHAR (50) NOT NULL DEFAULT 'foo' FIRST;` + ddl := `ALTER TABLE ` + "`users`" + ` ADD COLUMN ` + "`mycolumn`" + ` VARCHAR (50) NOT NULL DEFAULT 'foo' FIRST;` - res, _ := fizz.AString(`add_column("users", "mycolumn", "string", {"default": "foo", "size": 50, "first":true})`, myt) + res, err := fizz.AString(`add_column("users", "mycolumn", "string", {"default": "foo", "size": 50, "first":true})`, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_DropColumn() { r := p.Require() - ddl := `ALTER TABLE users DROP COLUMN mycolumn;` + ddl := `ALTER TABLE ` + "`users`" + ` DROP COLUMN ` + "`mycolumn`" + `;` - res, _ := fizz.AString(`drop_column("users", "mycolumn")`, myt) + res, err := fizz.AString(`drop_column("users", "mycolumn")`, myt) + r.NoError(err) 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 'foo@example.com';` + ddl := `ALTER TABLE ` + "`users`" + ` CHANGE ` + "`email`" + ` ` + "`email_address`" + ` varchar(50) NOT NULL DEFAULT 'foo@example.com';` - res, _ := fizz.AString(`rename_column("users", "email", "email_address")`, myt) + res, err := fizz.AString(`rename_column("users", "email", "email_address")`, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_AddIndex() { r := p.Require() - ddl := `CREATE INDEX users_email_idx ON users (email);` + ddl := `CREATE INDEX ` + "`users_email_idx`" + ` ON ` + "`users`" + ` (` + "`email`" + `);` - res, _ := fizz.AString(`add_index("users", "email", {})`, myt) + res, err := fizz.AString(`add_index("users", "email", {})`, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_AddIndex_Unique() { r := p.Require() - ddl := `CREATE UNIQUE INDEX users_email_idx ON users (email);` + ddl := `CREATE UNIQUE INDEX ` + "`users_email_idx`" + ` ON ` + "`users`" + ` (` + "`email`" + `);` - res, _ := fizz.AString(`add_index("users", "email", {"unique": true})`, myt) + res, err := fizz.AString(`add_index("users", "email", {"unique": true})`, myt) + r.NoError(err) 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);` + ddl := `CREATE INDEX ` + "`users_id_email_idx`" + ` ON ` + "`users`" + ` (` + "`id`" + `, ` + "`email`" + `);` - res, _ := fizz.AString(`add_index("users", ["id", "email"], {})`, myt) + res, err := fizz.AString(`add_index("users", ["id", "email"], {})`, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_AddIndex_CustomName() { r := p.Require() - ddl := `CREATE INDEX email_index ON users (email);` + ddl := `CREATE INDEX ` + "`email_index`" + ` ON ` + "`users`" + ` (` + "`email`" + `);` - res, _ := fizz.AString(`add_index("users", "email", {"name": "email_index"})`, myt) + res, err := fizz.AString(`add_index("users", "email", {"name": "email_index"})`, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_DropIndex() { r := p.Require() - ddl := `DROP INDEX email_idx ON users;` + ddl := `DROP INDEX ` + "`email_idx`" + ` ON ` + "`users`" + `;` - res, _ := fizz.AString(`drop_index("users", "email_idx")`, myt) + res, err := fizz.AString(`drop_index("users", "email_idx")`, myt) + r.NoError(err) 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;` + ddl := `ALTER TABLE ` + "`users`" + ` RENAME INDEX ` + "`email_idx`" + ` TO ` + "`email_address_idx`" + `;` - res, _ := fizz.AString(`rename_index("users", "email_idx", "email_address_ix")`, myt) + res, err := fizz.AString(`rename_index("users", "email_idx", "email_address_ix")`, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_AddForeignKey() { r := p.Require() - ddl := `ALTER TABLE profiles ADD CONSTRAINT profiles_users_id_fk FOREIGN KEY (user_id) REFERENCES users (id);` + ddl := `ALTER TABLE ` + "`profiles`" + ` ADD CONSTRAINT ` + "`profiles_users_id_fk`" + ` FOREIGN KEY (` + "`user_id`" + `) REFERENCES ` + "`users`" + ` (` + "`id`" + `);` - res, _ := fizz.AString(`add_foreign_key("profiles", "user_id", {"users": ["id"]}, {})`, myt) + res, err := fizz.AString(`add_foreign_key("profiles", "user_id", {"users": ["id"]}, {})`, myt) + r.NoError(err) r.Equal(ddl, res) } func (p *MySQLSuite) Test_MySQL_DropForeignKey() { r := p.Require() - ddl := `ALTER TABLE profiles DROP FOREIGN KEY profiles_users_id_fk;` + ddl := `ALTER TABLE ` + "`profiles`" + ` DROP FOREIGN KEY ` + "`profiles_users_id_fk`" + `;` - res, _ := fizz.AString(`drop_foreign_key("profiles", "profiles_users_id_fk", {})`, myt) + res, err := fizz.AString(`drop_foreign_key("profiles", "profiles_users_id_fk", {})`, myt) + r.NoError(err) r.Equal(ddl, res) } diff --git a/translators/postgres.go b/translators/postgres.go index 4835b4f2..822298a9 100644 --- a/translators/postgres.go +++ b/translators/postgres.go @@ -65,14 +65,14 @@ func (p *Postgres) DropTable(t fizz.Table) (string, error) { func (p *Postgres) RenameTable(t []fizz.Table) (string, error) { if len(t) < 2 { - return "", errors.New("Not enough table names supplied!") + return "", errors.New("not enough table names supplied") } return fmt.Sprintf("ALTER TABLE \"%s\" RENAME TO \"%s\";", t[0].Name, t[1].Name), nil } func (p *Postgres) ChangeColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } c := t.Columns[0] s := fmt.Sprintf("ALTER TABLE \"%s\" ALTER COLUMN %s;", t.Name, p.buildChangeColumn(c)) @@ -81,7 +81,7 @@ func (p *Postgres) ChangeColumn(t fizz.Table) (string, error) { func (p *Postgres) AddColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } c := t.Columns[0] s := fmt.Sprintf("ALTER TABLE \"%s\" ADD COLUMN %s;", t.Name, p.buildAddColumn(c)) @@ -90,7 +90,7 @@ func (p *Postgres) AddColumn(t fizz.Table) (string, error) { func (p *Postgres) DropColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } c := t.Columns[0] return fmt.Sprintf("ALTER TABLE \"%s\" DROP COLUMN \"%s\";", t.Name, c.Name), nil @@ -98,7 +98,7 @@ func (p *Postgres) DropColumn(t fizz.Table) (string, error) { func (p *Postgres) RenameColumn(t fizz.Table) (string, error) { if len(t.Columns) < 2 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } oc := t.Columns[0] nc := t.Columns[1] diff --git a/translators/sqlite.go b/translators/sqlite.go index a2610405..421b56d8 100644 --- a/translators/sqlite.go +++ b/translators/sqlite.go @@ -123,7 +123,7 @@ func (p *SQLite) ChangeColumn(t fizz.Table) (string, error) { func (p *SQLite) AddColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } c := t.Columns[0] @@ -140,7 +140,7 @@ func (p *SQLite) AddColumn(t fizz.Table) (string, error) { func (p *SQLite) DropColumn(t fizz.Table) (string, error) { if len(t.Columns) < 1 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } tableInfo, err := p.Schema.TableInfo(t.Name) @@ -196,7 +196,7 @@ func (p *SQLite) DropColumn(t fizz.Table) (string, error) { func (p *SQLite) RenameColumn(t fizz.Table) (string, error) { if len(t.Columns) < 2 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } tableInfo, err := p.Schema.TableInfo(t.Name)