Skip to content

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

fix(pg): preserve WHERE clause and fix ON ONLY in index export (#19310)

Vincent Huangdef581083d7b
diff --git a/CLAUDE.md b/CLAUDE.md
index 4f0b7d0f90..02792e78aa 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -176,21 +176,3 @@ Always follow these guidelines to avoid common linting errors:
- The database JSONB columns store JSON marshalled by `protojson.Marshal` in Go code. `protojson.Marshal` produces camelCased keys rather than the snake_case keys defined in the proto files. e.g. `task_run` becomes `taskRun`
- When modifying multiple files, run file modification tasks in parallel whenever possible, instead of processing them sequentially
-
-## Schema Dump Format
-
-When modifying schema dump output, increment the engine's `DumpFormatVersion` in `backend/plugin/schema/{engine}/dump_format_version.go`.
-
-**Files that affect dump format:**
-- `backend/plugin/schema/{engine}/get_database_definition.go`
-- `backend/plugin/schema/{engine}/sdl_*.go`
-- `backend/plugin/db/{engine}/dump.go`
-
-**When to bump:**
-- Adding/removing SQL statements in output
-- Changing formatting or ordering
-- Changing how objects are serialized
-
-**Do not bump for:**
-- Refactoring that doesn't change output
-- Changes to parsing or validation code
diff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.go
index 229b2b8a7e..74f658389d 100644
--- a/backend/plugin/schema/pg/get_database_definition.go
+++ b/backend/plugin/schema/pg/get_database_definition.go
@@ -1003,7 +1003,7 @@ func writeMaterializedView(out io.Writer, schema string, view *storepb.Materiali
}
for _, index := range view.Indexes {
- if err := writeIndex(out, schema, view.Name, index); err != nil {
+ if err := writeIndex(out, schema, view.Name, index, false); err != nil {
return err
}
}
@@ -1662,7 +1662,7 @@ func writeTable(out io.Writer, schema string, table *storepb.TableMetadata, sequ
// Construct Index.
for _, index := range table.Indexes {
if !index.Primary && !index.IsConstraint {
- if err := writeIndex(out, schema, table.Name, index); err != nil {
+ if err := writeIndex(out, schema, table.Name, index, len(table.Partitions) > 0); err != nil {
return err
}
}
@@ -1833,7 +1833,7 @@ func writeAttachIndex(out io.Writer, schema string, index *storepb.IndexMetadata
func writePartitionIndex(out io.Writer, schema string, partition *storepb.TablePartitionMetadata) error {
for _, index := range partition.Indexes {
if !index.IsConstraint && !index.Primary {
- if err := writeIndex(out, schema, partition.Name, index); err != nil {
+ if err := writeIndex(out, schema, partition.Name, index, len(partition.Subpartitions) > 0); err != nil {
return err
}
}
@@ -1847,8 +1847,8 @@ func writePartitionIndex(out io.Writer, schema string, partition *storepb.TableP
return nil
}
-func writeIndex(out io.Writer, schema string, table string, index *storepb.IndexMetadata) error {
- return writeIndexInternal(out, schema, table, index, true, true)
+func writeIndex(out io.Writer, schema string, table string, index *storepb.IndexMetadata, useOnlyClause bool) error {
+ return writeIndexInternal(out, schema, table, index, useOnlyClause, true)
}
func writeIndexKeyList(out io.Writer, index *storepb.IndexMetadata) error {
@@ -2696,7 +2696,7 @@ func getSDLFormat(metadata *storepb.DatabaseSchemaMetadata) (string, error) {
continue
}
- if err := writeIndexSDL(&buf, schema.Name, materializedView.Name, index); err != nil {
+ if err := writeIndexSDL(&buf, schema.Name, materializedView.Name, index, false); err != nil {
return "", err
}
if _, err := buf.WriteString(";\n\n"); err != nil {
@@ -3153,7 +3153,7 @@ func writeIndexesSDL(out io.Writer, schemaName string, table *storepb.TableMetad
continue
}
- if err := writeIndexSDL(out, schemaName, table.Name, index); err != nil {
+ if err := writeIndexSDL(out, schemaName, table.Name, index, len(table.Partitions) > 0); err != nil {
return err
}
@@ -3164,8 +3164,8 @@ func writeIndexesSDL(out io.Writer, schemaName string, table *storepb.TableMetad
return nil
}
-func writeIndexSDL(out io.Writer, schemaName string, tableName string, index *storepb.IndexMetadata) error {
- return writeIndexInternal(out, schemaName, tableName, index, true, false)
+func writeIndexSDL(out io.Writer, schemaName string, tableName string, index *storepb.IndexMetadata, useOnlyClause bool) error {
+ return writeIndexInternal(out, schemaName, tableName, index, useOnlyClause, false)
}
// writeIndexInternal is the core index writing function with options for different modes
@@ -3183,6 +3183,9 @@ func writeIndexInternal(out io.Writer, schema string, table string, index *store
return err
}
+ // TODO: useOnlyClause assumes partition-structured metadata. If legacy
+ // PostgreSQL table inheritance support is ever needed, revisit with
+ // definition-driven ONLY extraction.
if useOnlyClause {
if _, err := io.WriteString(out, `" ON ONLY "`); err != nil {
return err
@@ -3220,6 +3223,18 @@ func writeIndexInternal(out io.Writer, schema string, table string, index *store
return err
}
+ // Write WHERE clause for partial indexes.
+ // The WHERE clause is stored in the full index definition (from pg_get_indexdef).
+ if index.Definition != "" {
+ comparer := &PostgreSQLIndexComparer{}
+ whereClause := comparer.ExtractWhereClauseFromIndexDef(index.Definition)
+ if whereClause != "" {
+ if _, err := fmt.Fprintf(out, " WHERE %s", whereClause); err != nil {
+ return err
+ }
+ }
+ }
+
if includeTerminatorAndComment {
if _, err := io.WriteString(out, ";\n\n"); err != nil {
return err
@@ -3718,7 +3733,7 @@ func GetMultiFileDatabaseDefinition(ctx schema.GetDefinitionContext, metadata *s
}
buf.WriteString("\n")
- if err := writeIndexSDL(&buf, schemaName, materializedView.Name, index); err != nil {
+ if err := writeIndexSDL(&buf, schemaName, materializedView.Name, index, false); err != nil {
return nil, errors.Wrapf(err, "failed to generate index SDL for %s.%s", schemaName, index.Name)
}
buf.WriteString(";\n")
diff --git a/backend/plugin/schema/pg/get_database_definition_sdl_test.go b/backend/plugin/schema/pg/get_database_definition_sdl_test.go
index 9fe929cae5..41d6aadc29 100644
--- a/backend/plugin/schema/pg/get_database_definition_sdl_test.go
+++ b/backend/plugin/schema/pg/get_database_definition_sdl_test.go
@@ -337,11 +337,11 @@ CREATE TABLE "public"."products" (
CONSTRAINT "products_pkey" PRIMARY KEY (id)
);
-CREATE INDEX "idx_products_name" ON ONLY "public"."products" (name);
+CREATE INDEX "idx_products_name" ON "public"."products" (name);
-CREATE INDEX "idx_products_category_price" ON ONLY "public"."products" (category_id, price DESC);
+CREATE INDEX "idx_products_category_price" ON "public"."products" (category_id, price DESC);
-CREATE UNIQUE INDEX "idx_products_name_unique" ON ONLY "public"."products" (name);
+CREATE UNIQUE INDEX "idx_products_name_unique" ON "public"."products" (name);
`,
},
@@ -911,11 +911,11 @@ CREATE TABLE "public"."products" (
CONSTRAINT "documents_pkey" PRIMARY KEY (id)
);
-CREATE INDEX "idx_documents_title_pattern" ON ONLY "public"."documents" (title text_pattern_ops);
+CREATE INDEX "idx_documents_title_pattern" ON "public"."documents" (title text_pattern_ops);
-CREATE INDEX "idx_documents_title_content" ON ONLY "public"."documents" (title text_pattern_ops, content text_pattern_ops);
+CREATE INDEX "idx_documents_title_content" ON "public"."documents" (title text_pattern_ops, content text_pattern_ops);
-CREATE INDEX "idx_documents_default_opclass" ON ONLY "public"."documents" (title);
+CREATE INDEX "idx_documents_default_opclass" ON "public"."documents" (title);
`,
},
diff --git a/backend/plugin/schema/pg/materialized_view_index_test.go b/backend/plugin/schema/pg/materialized_view_index_test.go
index a230117604..dcd7a35c5b 100644
--- a/backend/plugin/schema/pg/materialized_view_index_test.go
+++ b/backend/plugin/schema/pg/materialized_view_index_test.go
@@ -58,8 +58,9 @@ func TestMaterializedViewIndexInSDLOutput(t *testing.T) {
require.Contains(t, sdl, "CREATE INDEX \"idx_product_name\"")
require.Contains(t, sdl, "CREATE UNIQUE INDEX \"idx_product_id_unique\"")
- // Verify index is on materialized view (using ON ONLY for SDL)
- require.Contains(t, sdl, "ON ONLY \"public\".\"product_summary_mv\"")
+ // Verify index is on materialized view (no ON ONLY for non-partitioned)
+ require.Contains(t, sdl, "ON \"public\".\"product_summary_mv\"")
+ require.NotContains(t, sdl, "ON ONLY")
// Verify comments
require.Contains(t, sdl, "COMMENT ON MATERIALIZED VIEW")
@@ -113,7 +114,8 @@ func TestMaterializedViewIndexInMultiFileSDL(t *testing.T) {
require.Contains(t, mvFile.Content, "CREATE MATERIALIZED VIEW")
require.Contains(t, mvFile.Content, "user_stats_mv")
require.Contains(t, mvFile.Content, "CREATE INDEX \"idx_user_stats_user_id\"")
- require.Contains(t, mvFile.Content, "ON ONLY \"public\".\"user_stats_mv\"")
+ require.Contains(t, mvFile.Content, "ON \"public\".\"user_stats_mv\"")
+ require.NotContains(t, mvFile.Content, "ON ONLY")
}
// TestMaterializedViewIndexDependencyOrdering tests that materialized view indexes have correct dependency ordering in migrations
diff --git a/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go b/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
index ad1b96f595..89ead04f61 100644
--- a/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
+++ b/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
@@ -79,11 +79,11 @@ ALTER SEQUENCE "public"."audit_id_seq" OWNED BY "public"."audit"."id";
ALTER TABLE ONLY "public"."audit" ADD CONSTRAINT "audit_pkey" PRIMARY KEY (id);
-CREATE INDEX "idx_audit_changed_at" ON ONLY "public"."audit" (changed_at);
+CREATE INDEX "idx_audit_changed_at" ON "public"."audit" (changed_at);
-CREATE INDEX "idx_audit_operation" ON ONLY "public"."audit" (operation);
+CREATE INDEX "idx_audit_operation" ON "public"."audit" (operation);
-CREATE INDEX "idx_audit_username" ON ONLY "public"."audit" (user_name);
+CREATE INDEX "idx_audit_username" ON "public"."audit" (user_name);
CREATE TABLE "public"."department" (
"dept_no" text NOT NULL,
@@ -134,7 +134,7 @@ ALTER SEQUENCE "public"."employee_emp_no_seq" OWNED BY "public"."employee"."emp_
ALTER TABLE ONLY "public"."employee" ADD CONSTRAINT "employee_pkey" PRIMARY KEY (emp_no);
-CREATE INDEX "idx_employee_hire_date" ON ONLY "public"."employee" (hire_date);
+CREATE INDEX "idx_employee_hire_date" ON "public"."employee" (hire_date);
CREATE OR REPLACE FUNCTION public.log_dml_operations()
RETURNS trigger
@@ -168,7 +168,7 @@ CREATE TABLE "public"."salary" (
ALTER TABLE ONLY "public"."salary" ADD CONSTRAINT "salary_pkey" PRIMARY KEY (emp_no, from_date);
-CREATE INDEX "idx_salary_amount" ON ONLY "public"."salary" (amount);
+CREATE INDEX "idx_salary_amount" ON "public"."salary" (amount);
CREATE TABLE "public"."title" (
"emp_no" integer NOT NULL,
diff --git a/backend/plugin/schema/pg/standalone_index_test.go b/backend/plugin/schema/pg/standalone_index_test.go
index 66e5f3d932..2ff24b1e4e 100644
--- a/backend/plugin/schema/pg/standalone_index_test.go
+++ b/backend/plugin/schema/pg/standalone_index_test.go
@@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/schema"
)
@@ -513,3 +514,121 @@ CREATE INDEX "idx_employees_name" ON "company"."employees" (name);
})
}
}
+
+// TestPartialIndexWhereClauseInSDLOutput tests that partial index WHERE clauses
+// are preserved in SDL output (regression test for WHERE clause being dropped).
+func TestPartialIndexWhereClauseInSDLOutput(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "channels",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Position: 1},
+ {Name: "slug", Type: "character varying(255)", Position: 2},
+ {Name: "social_region", Type: "character varying(50)", Position: 3},
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "udx_slug_social_region_on_channels",
+ Expressions: []string{"slug", "social_region"},
+ Type: "btree",
+ Unique: true,
+ Definition: "CREATE UNIQUE INDEX udx_slug_social_region_on_channels ON public.channels USING btree (slug, social_region) WHERE ((slug)::text <> ''::text);",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ sdl, err := getSDLFormat(metadata)
+ require.NoError(t, err)
+
+ // WHERE clause must be present
+ require.Contains(t, sdl, "WHERE")
+ require.Contains(t, sdl, "(slug)::text")
+
+ // Non-partitioned table: must NOT have ON ONLY
+ require.NotContains(t, sdl, "ON ONLY")
+ require.Contains(t, sdl, "ON \"public\".\"channels\"")
+}
+
+// TestRegularIndexNoOnOnly tests that regular (non-partitioned) table indexes
+// do not include ON ONLY in SDL output.
+func TestRegularIndexNoOnOnly(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Position: 1},
+ {Name: "email", Type: "character varying(255)", Position: 2},
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "idx_users_email",
+ Expressions: []string{"email"},
+ Type: "btree",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ sdl, err := getSDLFormat(metadata)
+ require.NoError(t, err)
+
+ require.Contains(t, sdl, `CREATE INDEX "idx_users_email"`)
+ require.Contains(t, sdl, `ON "public"."users"`)
+ require.NotContains(t, sdl, "ON ONLY")
+}
+
+// TestPartitionedTableIndexUsesOnOnly tests that partitioned parent table indexes
+// use ON ONLY in SDL output to prevent recursive child index creation.
+func TestPartitionedTableIndexUsesOnOnly(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Position: 1},
+ {Name: "order_date", Type: "date", Position: 2},
+ {Name: "customer_id", Type: "integer", Position: 3},
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "idx_orders_customer_id",
+ Expressions: []string{"customer_id"},
+ Type: "btree",
+ },
+ },
+ Partitions: []*storepb.TablePartitionMetadata{
+ {
+ Name: "orders_2024",
+ Type: storepb.TablePartitionMetadata_RANGE,
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ sdl, err := getSDLFormat(metadata)
+ require.NoError(t, err)
+
+ require.Contains(t, sdl, `CREATE INDEX "idx_orders_customer_id"`)
+ require.Contains(t, sdl, `ON ONLY "public"."orders"`)
+}