dialect/sql: add change-column for mysql (#304)

Old versions of MySQL (<8) do not support the 'RENAME' caluse
This commit is contained in:
Ariel Mashraki
2020-01-21 05:04:01 -05:00
committed by GitHub
parent 4c89190e8a
commit fd8c07717c
2 changed files with 12 additions and 0 deletions

View File

@@ -252,6 +252,13 @@ func (t *TableAlter) DropColumn(c *ColumnBuilder) *TableAlter {
return t
}
// ChangeColumn appends the `CHANGE COLUMN` clause to the given `ALTER TABLE` statement.
func (t *TableAlter) ChangeColumn(name string, c *ColumnBuilder) *TableAlter {
prefix := fmt.Sprintf("CHANGE COLUMN %s", t.Quote(name))
t.Queries = append(t.Queries, &Wrapper{prefix + " %s", c})
return t
}
// RenameIndex appends the `RENAME INDEX` clause to the given `ALTER TABLE` statement.
func (t *TableAlter) RenameIndex(curr, new string) *TableAlter {
t.Queries = append(t.Queries, Raw(fmt.Sprintf("RENAME INDEX %s TO %s", t.Quote(curr), t.Quote(new))))

View File

@@ -184,6 +184,11 @@ func TestBuilder(t *testing.T) {
ModifyColumn(Column("name").Attr("DROP NOT NULL")),
wantQuery: `ALTER TABLE "users" ALTER COLUMN "age" TYPE int, ALTER COLUMN "age" SET NOT NULL, ALTER COLUMN "name" DROP NOT NULL`,
},
{
input: AlterTable("users").
ChangeColumn("old_age", Column("age").Type("int")),
wantQuery: "ALTER TABLE `users` CHANGE COLUMN `old_age` `age` int",
},
{
input: Dialect(dialect.Postgres).AlterTable("users").
AddColumn(Column("boring").Type("varchar")).