dialect/sql: add drop-fk command to builder

This commit is contained in:
Ariel Mashraki
2019-12-31 21:26:02 +02:00
parent b6cfa43af6
commit ab732c7654
2 changed files with 11 additions and 0 deletions

View File

@@ -264,6 +264,12 @@ func (t *TableAlter) DropConstraint(ident string) *TableAlter {
return t
}
// DropForeignKey appends the `DROP FOREIGN KEY` clause to the given `ALTER TABLE` statement.
func (t *TableAlter) DropForeignKey(ident string) *TableAlter {
t.Queries = append(t.Queries, Raw(fmt.Sprintf("DROP FOREIGN KEY %s", t.Quote(ident))))
return t
}
// Query returns query representation of the `ALTER TABLE` statement.
//
// ALTER TABLE name

View File

@@ -133,6 +133,11 @@ func TestBuilder(t *testing.T) {
AddColumn(Column("name").Type("varchar(255)")),
wantQuery: "ALTER TABLE `users` ADD COLUMN `age` int, ADD COLUMN `name` varchar(255)",
},
{
input: AlterTable("users").
DropForeignKey("users_parent_id"),
wantQuery: "ALTER TABLE `users` DROP FOREIGN KEY `users_parent_id`",
},
{
input: Dialect(dialect.Postgres).AlterTable("users").
AddColumn(Column("age").Type("int")).