Skip to content

Commit

Permalink
fix: don't append CASCADE by default in drop table/column queries
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Nov 30, 2021
1 parent 47a60c1 commit 26457ea
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion db.go
Expand Up @@ -125,7 +125,7 @@ func (db *DB) NewDropColumn() *DropColumnQuery {

func (db *DB) ResetModel(ctx context.Context, models ...interface{}) error {
for _, model := range models {
if _, err := db.NewDropTable().Model(model).IfExists().Exec(ctx); err != nil {
if _, err := db.NewDropTable().Model(model).IfExists().Cascade().Exec(ctx); err != nil {
return err
}
if _, err := db.NewCreateTable().Model(model).Exec(ctx); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-34
@@ -1 +1 @@
DROP TABLE "models" CASCADE
DROP TABLE "models"
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-42
@@ -1 +1 @@
DROP INDEX CONCURRENTLY IF EXISTS title_idx CASCADE
DROP INDEX CONCURRENTLY IF EXISTS title_idx
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-45
@@ -1 +1 @@
TRUNCATE TABLE "models" RESTART IDENTITY CASCADE
TRUNCATE TABLE "models" RESTART IDENTITY
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-34
@@ -1 +1 @@
DROP TABLE "models" CASCADE
DROP TABLE "models"
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-42
@@ -1 +1 @@
DROP INDEX CONCURRENTLY IF EXISTS title_idx CASCADE
DROP INDEX CONCURRENTLY IF EXISTS title_idx
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-45
@@ -1 +1 @@
TRUNCATE TABLE "models" RESTART IDENTITY CASCADE
TRUNCATE TABLE "models" RESTART IDENTITY
6 changes: 4 additions & 2 deletions query_base.go
Expand Up @@ -951,17 +951,19 @@ func (q setQuery) appendSet(fmter schema.Formatter, b []byte) (_ []byte, err err
//------------------------------------------------------------------------------

type cascadeQuery struct {
cascade bool
restrict bool
}

func (q cascadeQuery) appendCascade(fmter schema.Formatter, b []byte) []byte {
if !fmter.HasFeature(feature.TableCascade) {
return b
}
if q.cascade {
b = append(b, " CASCADE"...)
}
if q.restrict {
b = append(b, " RESTRICT"...)
} else {
b = append(b, " CASCADE"...)
}
return b
}
5 changes: 5 additions & 0 deletions query_index_drop.go
Expand Up @@ -50,6 +50,11 @@ func (q *DropIndexQuery) IfExists() *DropIndexQuery {
return q
}

func (q *DropIndexQuery) Cascade() *DropIndexQuery {
q.cascade = true
return q
}

func (q *DropIndexQuery) Restrict() *DropIndexQuery {
q.restrict = true
return q
Expand Down
5 changes: 5 additions & 0 deletions query_table_drop.go
Expand Up @@ -61,6 +61,11 @@ func (q *DropTableQuery) IfExists() *DropTableQuery {
return q
}

func (q *DropTableQuery) Cascade() *DropTableQuery {
q.cascade = true
return q
}

func (q *DropTableQuery) Restrict() *DropTableQuery {
q.restrict = true
return q
Expand Down
5 changes: 5 additions & 0 deletions query_table_truncate.go
Expand Up @@ -57,6 +57,11 @@ func (q *TruncateTableQuery) ContinueIdentity() *TruncateTableQuery {
return q
}

func (q *TruncateTableQuery) Cascade() *TruncateTableQuery {
q.cascade = true
return q
}

func (q *TruncateTableQuery) Restrict() *TruncateTableQuery {
q.restrict = true
return q
Expand Down

0 comments on commit 26457ea

Please sign in to comment.