Skip to content

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

feat: support pg index opclass (#17736)

rebelice6e9b14a7ce5f
diff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.go
index b9e01a2564..6025968b6a 100644
--- a/backend/plugin/schema/pg/get_database_definition.go
+++ b/backend/plugin/schema/pg/get_database_definition.go
@@ -1719,6 +1719,18 @@ func writeIndexKeyList(out io.Writer, index *storepb.IndexMetadata) error {
return err
}
+ // Add opclass if it's not the default
+ if i < len(index.OpclassNames) && i < len(index.OpclassDefaults) {
+ if !index.OpclassDefaults[i] && index.OpclassNames[i] != "" {
+ if _, err := io.WriteString(out, " "); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, index.OpclassNames[i]); err != nil {
+ return err
+ }
+ }
+ }
+
// Add DESC if this column is marked as descending
if i < len(index.Descending) && index.Descending[i] {
if _, err := io.WriteString(out, " DESC"); err != nil {
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 57faa8b48e..9a5b00e658 100644
--- a/backend/plugin/schema/pg/get_database_definition_sdl_test.go
+++ b/backend/plugin/schema/pg/get_database_definition_sdl_test.go
@@ -841,6 +841,80 @@ CREATE TABLE "public"."products" (
"name" VARCHAR(255) NOT NULL
);
+`,
+ },
+ {
+ name: "Table with indexes using custom opclass",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "documents",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "title",
+ Type: "TEXT",
+ Nullable: false,
+ },
+ {
+ Name: "content",
+ Type: "TEXT",
+ Nullable: false,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "documents_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ {
+ Name: "idx_documents_title_pattern",
+ Expressions: []string{"title"},
+ Type: "btree",
+ OpclassNames: []string{"text_pattern_ops"},
+ OpclassDefaults: []bool{false},
+ },
+ {
+ Name: "idx_documents_title_content",
+ Expressions: []string{"title", "content"},
+ Type: "btree",
+ OpclassNames: []string{"text_pattern_ops", "text_pattern_ops"},
+ OpclassDefaults: []bool{false, false},
+ },
+ {
+ Name: "idx_documents_default_opclass",
+ Expressions: []string{"title"},
+ Type: "btree",
+ OpclassNames: []string{"text_ops"},
+ OpclassDefaults: []bool{true}, // Default opclass should not be printed
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."documents" (
+ "id" SERIAL NOT NULL,
+ "title" TEXT NOT NULL,
+ "content" TEXT NOT NULL,
+ 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_content" ON ONLY "public"."documents" (title text_pattern_ops, content text_pattern_ops);
+
+CREATE INDEX "idx_documents_default_opclass" ON ONLY "public"."documents" (title);
+
`,
},
}