Skip to content

Commit fa9d2a0

Browse files
author
Sergey Podgornyy
committed
Add drop table if exists to the schema
1 parent eb6ce74 commit fa9d2a0

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

schema.go

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ func (s *Schema) DropTable(name string, soft bool, option string) {
3131
s.pool = append(s.pool, dropTableCommand{name, soft, option})
3232
}
3333

34+
// DropTableIfExists removes table if exists from schema
35+
// Warning ⚠️ BC incompatible
36+
//
37+
// Example:
38+
// var s migrator.Schema
39+
// s.DropTableIfExists("test")
40+
func (s *Schema) DropTableIfExists(name string) {
41+
s.pool = append(s.pool, dropTableCommand{name, true, ""})
42+
}
43+
3444
// RenameTable executes command to rename table
3545
// Warning ⚠️ BC incompatible
3646
//

schema_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ func TestSchemaDropTable(t *testing.T) {
3131
assert.Equal(dropTableCommand{"test", false, ""}, s.pool[0])
3232
}
3333

34+
func TestSchemaDropTableIfExists(t *testing.T) {
35+
assert := assert.New(t)
36+
37+
s := Schema{}
38+
assert.Len(s.pool, 0)
39+
40+
s.DropTableIfExists("test")
41+
42+
assert.Len(s.pool, 1)
43+
assert.Equal(dropTableCommand{"test", true, ""}, s.pool[0])
44+
}
45+
3446
func TestSchemaRenameTable(t *testing.T) {
3547
assert := assert.New(t)
3648

0 commit comments

Comments
 (0)