Skip to content

Commit

Permalink
Add drop table if exists to the schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Podgornyy committed Jun 28, 2020
1 parent eb6ce74 commit fa9d2a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func (s *Schema) DropTable(name string, soft bool, option string) {
s.pool = append(s.pool, dropTableCommand{name, soft, option})
}

// DropTableIfExists removes table if exists from schema
// Warning ⚠️ BC incompatible
//
// Example:
// var s migrator.Schema
// s.DropTableIfExists("test")
func (s *Schema) DropTableIfExists(name string) {
s.pool = append(s.pool, dropTableCommand{name, true, ""})
}

// RenameTable executes command to rename table
// Warning ⚠️ BC incompatible
//
Expand Down
12 changes: 12 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ func TestSchemaDropTable(t *testing.T) {
assert.Equal(dropTableCommand{"test", false, ""}, s.pool[0])
}

func TestSchemaDropTableIfExists(t *testing.T) {
assert := assert.New(t)

s := Schema{}
assert.Len(s.pool, 0)

s.DropTableIfExists("test")

assert.Len(s.pool, 1)
assert.Equal(dropTableCommand{"test", true, ""}, s.pool[0])
}

func TestSchemaRenameTable(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit fa9d2a0

Please sign in to comment.