Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: don't drop table constraint/index if the table is being dropped (#15583)

Danny Xub72b4621eba8
diff --git a/backend/plugin/parser/pg/differ.go b/backend/plugin/parser/pg/differ.go
index 51cf0cfd01..4da2f2f6e0 100644
--- a/backend/plugin/parser/pg/differ.go
+++ b/backend/plugin/parser/pg/differ.go
@@ -2460,7 +2460,13 @@ func (*diffNode) getViewDependency(view *ast.CreateViewStmt, getDatabaseMetadata
func (diff *diffNode) dropConstraint(m schemaMap) {
for _, schemaInfo := range m {
+ if !schemaInfo.existsInNew {
+ continue
+ }
for _, tableInfo := range schemaInfo.tableMap {
+ if !tableInfo.existsInNew {
+ continue
+ }
var constraintInfoList []*constraintInfo
for _, constraintInfo := range tableInfo.constraintMap {
if !constraintInfo.existsInNew {
@@ -2783,11 +2789,21 @@ func (diff *diffNode) dropTriggerStmt(m schemaMap) {
func dropIndex(m schemaMap) *ast.DropIndexStmt {
var indexList []*indexInfo
for _, schema := range m {
+ if !schema.existsInNew {
+ continue
+ }
for _, index := range schema.indexMap {
if index.existsInNew {
// no need to drop
continue
}
+ tbl, ok := schema.tableMap[index.createIndex.Index.Table.Name]
+ if !ok {
+ continue
+ }
+ if !tbl.existsInNew {
+ continue
+ }
indexList = append(indexList, index)
}
}
diff --git a/backend/plugin/parser/pg/test-data/test_differ_data.yaml b/backend/plugin/parser/pg/test-data/test_differ_data.yaml
index 4adceeb693..a02041c8a8 100644
--- a/backend/plugin/parser/pg/test-data/test_differ_data.yaml
+++ b/backend/plugin/parser/pg/test-data/test_differ_data.yaml
@@ -230,9 +230,11 @@
)
diff: ""
- oldSchema: |
+ CREATE TABLE public.t1(a int, b int, c int);
CREATE INDEX idx1 ON public.t1 USING btree (a, b, (c+1));
CREATE INDEX idx2 ON public.t1 USING hash (a);
newSchema: |
+ CREATE TABLE public.t1(a int, b int, c int);
CREATE INDEX idx2 ON public.t1 USING btree (a);
CREATE INDEX idx3 ON public.t1 USING btree (a, b);
diff: |+
@@ -284,8 +286,6 @@
alter table schema1.t1 alter column a set default nextval('schema1.sequence1'::regclass);
newSchema: ""
diff: |+
- DROP INDEX "schema1"."idx_t1_a";
-
ALTER SEQUENCE "schema1"."sequence1"
OWNED BY NONE;
@@ -584,3 +584,21 @@
COMMENT ON COLUMN "public"."t1"."a" IS '';
+- oldSchema: |-
+ CREATE TABLE public.t (
+ id integer NOT NULL
+ );
+ ALTER TABLE ONLY public.t ADD CONSTRAINT t_pkey PRIMARY KEY (id);
+ newSchema: ""
+ diff: |+
+ DROP TABLE "public"."t";
+
+- oldSchema: |-
+ CREATE TABLE public.t (
+ id integer NOT NULL
+ );
+ CREATE INDEX hello ON public.t USING btree (id);
+ newSchema: ""
+ diff: |+
+ DROP TABLE "public"."t";
+
diff --git a/backend/tests/sync_schema_test.go b/backend/tests/sync_schema_test.go
index f11f03011a..627185c17e 100644
--- a/backend/tests/sync_schema_test.go
+++ b/backend/tests/sync_schema_test.go
@@ -22,9 +22,7 @@ func TestSyncSchema(t *testing.T) {
alter sequence schema_a.sequence_s1 owned by schema_a.table_t1.c1;
alter table schema_a.table_t1 alter column c1 set default nextval('schema_a.sequence_s1'::regclass);
`
- expectedDiff = `DROP INDEX "schema_a"."idx_table_t1_c1_c2_c3";
-
-ALTER SEQUENCE "schema_a"."sequence_s1"
+ expectedDiff = `ALTER SEQUENCE "schema_a"."sequence_s1"
OWNED BY NONE;
DROP TABLE "schema_a"."table_t1";