-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the drop table operation (#45)
Add support for **drop table** migrations. * On starting the migration the table is not dropped; but is not present in the new version of the schema. * The table is dropped on completion of the migration. * Rollback is a no-op.
- Loading branch information
1 parent
7d48a00
commit 450e6db
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "07_drop_table", | ||
"operations": [ | ||
{ | ||
"drop_table": { | ||
"name": "products" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/lib/pq" | ||
|
||
"pg-roll/pkg/schema" | ||
) | ||
|
||
type OpDropTable struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
var _ Operation = (*OpDropTable)(nil) | ||
|
||
func (o *OpDropTable) Start(ctx context.Context, conn *sql.DB, schemaName, stateSchema string, s *schema.Schema) error { | ||
s.RemoveTable(o.Name) | ||
return nil | ||
} | ||
|
||
func (o *OpDropTable) Complete(ctx context.Context, conn *sql.DB) error { | ||
_, err := conn.ExecContext(ctx, fmt.Sprintf("DROP TABLE IF EXISTS %s", pq.QuoteIdentifier(o.Name))) | ||
|
||
return err | ||
} | ||
|
||
func (o *OpDropTable) Rollback(ctx context.Context, conn *sql.DB) error { | ||
return nil | ||
} | ||
|
||
func (o *OpDropTable) Validate(ctx context.Context, s *schema.Schema) error { | ||
table := s.GetTable(o.Name) | ||
|
||
if table == nil { | ||
return TableDoesNotExistError{Name: o.Name} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package migrations_test | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
|
||
"pg-roll/pkg/migrations" | ||
) | ||
|
||
func TestDropTable(t *testing.T) { | ||
t.Parallel() | ||
|
||
ExecuteTests(t, TestCases{TestCase{ | ||
name: "drop table", | ||
migrations: []migrations.Migration{ | ||
{ | ||
Name: "01_create_table", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateTable{ | ||
Name: "users", | ||
Columns: []migrations.Column{ | ||
{ | ||
Name: "id", | ||
Type: "serial", | ||
PrimaryKey: true, | ||
}, | ||
{ | ||
Name: "name", | ||
Type: "varchar(255)", | ||
Unique: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "02_drop_table", | ||
Operations: migrations.Operations{ | ||
&migrations.OpDropTable{ | ||
Name: "users", | ||
}, | ||
}, | ||
}, | ||
}, | ||
afterStart: func(t *testing.T, db *sql.DB) { | ||
// The view for the deleted table does not exist in the new version schema. | ||
ViewMustNotExist(t, db, "public", "02_drop_table", "users") | ||
|
||
// But the underlying table has not been deleted. | ||
TableMustExist(t, db, "public", "users") | ||
}, | ||
afterRollback: func(t *testing.T, db *sql.DB) { | ||
// Rollback is a no-op. | ||
}, | ||
afterComplete: func(t *testing.T, db *sql.DB) { | ||
// The underlying table has been deleted. | ||
TableMustNotExist(t, db, "public", "users") | ||
}, | ||
}}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters