Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: better postgres index definition (#9931)
Danny Xu308db0d75283
diff --git a/backend/api/v1/database_converter.go b/backend/api/v1/database_converter.goindex a65b6424dd..d105cd5fb7 100644--- a/backend/api/v1/database_converter.go+++ b/backend/api/v1/database_converter.go@@ -185,6 +185,7 @@ func convertStoreTableMetadata(table *storepb.TableMetadata, view v1pb.DatabaseMPrimary: index.Primary,Visible: index.Visible,Comment: index.Comment,+ Definition: index.Definition,})}for _, foreignKey := range table.GetForeignKeys() {@@ -443,6 +444,7 @@ func convertV1TableMetadata(table *v1pb.TableMetadata) *storepb.TableMetadata {Primary: index.GetPrimary(),Visible: index.GetVisible(),Comment: index.GetComment(),+ Definition: index.GetDefinition(),})}for _, foreignKey := range table.ForeignKeys {diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.goindex 2fc7cc0ee9..7d66f77dda 100644--- a/backend/plugin/db/pg/sync.go+++ b/backend/plugin/db/pg/sync.go@@ -745,6 +745,12 @@ func getIndexes(txn *sql.Tx) (map[db.TableKey][]*storepb.IndexMetadata, error) {if !ok {return nil, errors.Errorf("statement %q is not index statement", statement)}+ deparsed, err := pgrawparser.Deparse(pgrawparser.DeparseContext{}, node)+ if err != nil {+ return nil, err+ }+ // Instead of using indexdef, we use deparsed format so that the definition has quoted identifiers.+ index.Definition = deparsedindex.Type = getIndexMethodType(statement)index.Unique = node.Index.Uniquediff --git a/backend/plugin/db/redshift/sync.go b/backend/plugin/db/redshift/sync.goindex cdfa31cc96..6c5754e167 100644--- a/backend/plugin/db/redshift/sync.go+++ b/backend/plugin/db/redshift/sync.go@@ -699,6 +699,12 @@ func getIndexes(txn *sql.Tx) (map[db.TableKey][]*storepb.IndexMetadata, error) {if !ok {return nil, errors.Errorf("statement %q is not index statement", statement)}+ deparsed, err := pgrawparser.Deparse(pgrawparser.DeparseContext{}, node)+ if err != nil {+ return nil, err+ }+ // Instead of using indexdef, we use deparsed format so that the definition has quoted identifiers.+ index.Definition = deparsedindex.Type = getIndexMethodType(statement)index.Unique = node.Index.Uniquediff --git a/backend/plugin/db/risingwave/sync.go b/backend/plugin/db/risingwave/sync.goindex baf3780e00..42175a9c64 100644--- a/backend/plugin/db/risingwave/sync.go+++ b/backend/plugin/db/risingwave/sync.go@@ -375,6 +375,12 @@ func getIndexes(txn *sql.Tx) (map[db.TableKey][]*storepb.IndexMetadata, error) {if !ok {return nil, errors.Errorf("statement %q is not index statement", statement)}+ deparsed, err := pgrawparser.Deparse(pgrawparser.DeparseContext{}, node)+ if err != nil {+ return nil, err+ }+ // Instead of using indexdef, we use deparsed format so that the definition has quoted identifiers.+ index.Definition = deparsedindex.Type = getIndexMethodType(statement)index.Unique = node.Index.Uniquediff --git a/backend/plugin/schema-engine/pg/structurer.go b/backend/plugin/schema-engine/pg/structurer.goindex 187586548c..629eb2100b 100644--- a/backend/plugin/schema-engine/pg/structurer.go+++ b/backend/plugin/schema-engine/pg/structurer.go@@ -594,11 +594,12 @@ func (f *foreignKeyState) toString(buf *strings.Builder, schemaName, tableName s}type indexState struct {- id int- name string- keys []string- primary bool- unique bool+ id int+ name string+ keys []string+ primary bool+ unique bool+ definition string}func (i *indexState) convertToIndexMetadata() *storepb.IndexMetadata {@@ -607,6 +608,7 @@ func (i *indexState) convertToIndexMetadata() *storepb.IndexMetadata {Expressions: i.keys,Primary: i.primary,Unique: i.unique,+ Definition: i.definition,// Unsupported, for tests only.Visible: true,}@@ -614,11 +616,12 @@ func (i *indexState) convertToIndexMetadata() *storepb.IndexMetadata {func convertToIndexState(id int, index *storepb.IndexMetadata) *indexState {return &indexState{- id: id,- name: index.Name,- keys: index.Expressions,- primary: index.Primary,- unique: index.Unique,+ id: id,+ name: index.Name,+ keys: index.Expressions,+ primary: index.Primary,+ unique: index.Unique,+ definition: index.Definition,}}@@ -647,31 +650,7 @@ func (i *indexState) toString(buf *strings.Builder, schemaName, tableName stringreturn err}} else {- indexType := "INDEX"- if i.unique {- indexType = "UNIQUE INDEX"- }-- if _, err := buf.WriteString(fmt.Sprintf("CREATE %s \"%s\".\"%s\" ON \"%s\".\"%s\" (", indexType, schemaName, i.name, schemaName, tableName)); err != nil {- return err- }- for j, key := range i.keys {- if j > 0 {- if _, err := buf.WriteString(", "); err != nil {- return err- }- }- if _, err := buf.WriteString("\""); err != nil {- return err- }- if _, err := buf.WriteString(key); err != nil {- return err- }- if _, err := buf.WriteString("\""); err != nil {- return err- }- }- if _, err := buf.WriteString(");\n"); err != nil {+ if _, err := buf.WriteString(i.definition); err != nil {return err}}diff --git a/backend/tests/syncer_test.go b/backend/tests/syncer_test.goindex f124b86682..8379a9f4fc 100644--- a/backend/tests/syncer_test.go+++ b/backend/tests/syncer_test.go@@ -143,6 +143,7 @@ func TestSyncerForPostgreSQL(t *testing.T) {Expressions: []string{`A`, `B`, "c"},Type: "btree",Unique: true,+ Definition: `CREATE UNIQUE INDEX "trd_A_B_c_key" ON "schema1"."trd" USING btree (A, B, c);`,},},IndexSize: 8192,diff --git a/frontend/src/types/proto/store/database.ts b/frontend/src/types/proto/store/database.tsindex b8bf1d0223..404e7add04 100644--- a/frontend/src/types/proto/store/database.ts+++ b/frontend/src/types/proto/store/database.ts@@ -405,6 +405,8 @@ export interface IndexMetadata {visible: boolean;/** The comment is the comment of an index. */comment: string;+ /** The definition of an index. */+ definition: string;}/** ExtensionMetadata is the metadata for extensions. */@@ -2370,7 +2372,16 @@ export const FunctionMetadata = {};function createBaseIndexMetadata(): IndexMetadata {- return { name: "", expressions: [], type: "", unique: false, primary: false, visible: false, comment: "" };+ return {+ name: "",+ expressions: [],+ type: "",+ unique: false,+ primary: false,+ visible: false,+ comment: "",+ definition: "",+ };}export const IndexMetadata = {@@ -2396,6 +2407,9 @@ export const IndexMetadata = {if (message.comment !== "") {writer.uint32(58).string(message.comment);}+ if (message.definition !== "") {+ writer.uint32(66).string(message.definition);+ }return writer;},@@ -2455,6 +2469,13 @@ export const IndexMetadata = {message.comment = reader.string();continue;+ case 8:+ if (tag !== 66) {+ break;+ }++ message.definition = reader.string();+ continue;}if ((tag & 7) === 4 || tag === 0) {break;@@ -2475,6 +2496,7 @@ export const IndexMetadata = {primary: isSet(object.primary) ? globalThis.Boolean(object.primary) : false,visible: isSet(object.visible) ? globalThis.Boolean(object.visible) : false,comment: isSet(object.comment) ? globalThis.String(object.comment) : "",+ definition: isSet(object.definition) ? globalThis.String(object.definition) : "",};},@@ -2501,6 +2523,9 @@ export const IndexMetadata = {if (message.comment !== "") {obj.comment = message.comment;}+ if (message.definition !== "") {+ obj.definition = message.definition;+ }return obj;},@@ -2516,6 +2541,7 @@ export const IndexMetadata = {message.primary = object.primary ?? false;message.visible = object.visible ?? false;message.comment = object.comment ?? "";+ message.definition = object.definition ?? "";return message;},};diff --git a/frontend/src/types/proto/v1/database_service.ts b/frontend/src/types/proto/v1/database_service.tsindex 144e934bb6..d87513efda 100644--- a/frontend/src/types/proto/v1/database_service.ts+++ b/frontend/src/types/proto/v1/database_service.ts@@ -791,6 +791,8 @@ export interface IndexMetadata {visible: boolean;/** The comment is the comment of an index. */comment: string;+ /** The definition of an index. */+ definition: string;}/** ExtensionMetadata is the metadata for extensions. */@@ -4989,7 +4991,16 @@ export const StreamMetadata = {};function createBaseIndexMetadata(): IndexMetadata {- return { name: "", expressions: [], type: "", unique: false, primary: false, visible: false, comment: "" };+ return {+ name: "",+ expressions: [],+ type: "",+ unique: false,+ primary: false,+ visible: false,+ comment: "",+ definition: "",+ };}export const IndexMetadata = {@@ -5015,6 +5026,9 @@ export const IndexMetadata = {if (message.comment !== "") {writer.uint32(58).string(message.comment);}+ if (message.definition !== "") {+ writer.uint32(66).string(message.definition);+ }return writer;},@@ -5074,6 +5088,13 @@ export const IndexMetadata = {message.comment = reader.string();continue;+ case 8:+ if (tag !== 66) {+ break;+ }++ message.definition = reader.string();+ continue;}if ((tag & 7) === 4 || tag === 0) {break;@@ -5094,6 +5115,7 @@ export const IndexMetadata = {primary: isSet(object.primary) ? globalThis.Boolean(object.primary) : false,visible: isSet(object.visible) ? globalThis.Boolean(object.visible) : false,comment: isSet(object.comment) ? globalThis.String(object.comment) : "",+ definition: isSet(object.definition) ? globalThis.String(object.definition) : "",};},@@ -5120,6 +5142,9 @@ export const IndexMetadata = {if (message.comment !== "") {obj.comment = message.comment;}+ if (message.definition !== "") {+ obj.definition = message.definition;+ }return obj;},@@ -5135,6 +5160,7 @@ export const IndexMetadata = {message.primary = object.primary ?? false;message.visible = object.visible ?? false;message.comment = object.comment ?? "";+ message.definition = object.definition ?? "";return message;},};diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex 791583ff34..9eb9ce066f 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -786,6 +786,7 @@ IndexMetadata is the metadata for indexes.| primary | [bool](#bool) | | The primary is whether the index is a primary key index. || visible | [bool](#bool) | | The visible is whether the index is visible. || comment | [string](#string) | | The comment is the comment of an index. |+| definition | [string](#string) | | The definition of an index. |diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.mdindex 3854e5dda2..4386a9d0dd 100644--- a/proto/gen/grpc-doc/v1/README.md+++ b/proto/gen/grpc-doc/v1/README.md@@ -2578,6 +2578,7 @@ IndexMetadata is the metadata for indexes.| primary | [bool](#bool) | | The primary is whether the index is a primary key index. || visible | [bool](#bool) | | The visible is whether the index is visible. || comment | [string](#string) | | The comment is the comment of an index. |+| definition | [string](#string) | | The definition of an index. |diff --git a/proto/generated-go/store/database.pb.go b/proto/generated-go/store/database.pb.goindex 5e33a0a281..f776e1e11e 100644--- a/proto/generated-go/store/database.pb.go+++ b/proto/generated-go/store/database.pb.go@@ -1455,6 +1455,8 @@ type IndexMetadata struct {Visible bool `protobuf:"varint,6,opt,name=visible,proto3" json:"visible,omitempty"`// The comment is the comment of an index.Comment string `protobuf:"bytes,7,opt,name=comment,proto3" json:"comment,omitempty"`+ // The definition of an index.+ Definition string `protobuf:"bytes,8,opt,name=definition,proto3" json:"definition,omitempty"`}func (x *IndexMetadata) Reset() {@@ -1538,6 +1540,13 @@ func (x *IndexMetadata) GetComment() string {return ""}+func (x *IndexMetadata) GetDefinition() string {+ if x != nil {+ return x.Definition+ }+ return ""+}+// ExtensionMetadata is the metadata for extensions.type ExtensionMetadata struct {state protoimpl.MessageState@@ -2372,7 +2381,7 @@ var file_store_database_proto_rawDesc = []byte{0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12,0x1e, 0x0a, 0x0a, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20,0x01, 0x28, 0x09, 0x52, 0x0a, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x22,- 0xbf, 0x01, 0x0a, 0x0d, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74,+ 0xdf, 0x01, 0x0a, 0x0d, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74,0x61, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x65, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73,0x69, 0x6f, 0x6e, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0b, 0x65, 0x78, 0x70, 0x72,@@ -2384,7 +2393,9 @@ var file_store_database_proto_rawDesc = []byte{0x07, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07,0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65,0x6e, 0x74, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e,- 0x74, 0x22, 0x7b, 0x0a, 0x11, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x4d, 0x65,+ 0x74, 0x12, 0x1e, 0x0a, 0x0a, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x18,+ 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x6f,+ 0x6e, 0x22, 0x7b, 0x0a, 0x11, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x4d, 0x65,0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01,0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63,0x68, 0x65, 0x6d, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65,diff --git a/proto/generated-go/v1/database_service.pb.go b/proto/generated-go/v1/database_service.pb.goindex 24dbd0bbc8..b5374a847f 100644--- a/proto/generated-go/v1/database_service.pb.go+++ b/proto/generated-go/v1/database_service.pb.go@@ -3140,6 +3140,8 @@ type IndexMetadata struct {Visible bool `protobuf:"varint,6,opt,name=visible,proto3" json:"visible,omitempty"`// The comment is the comment of an index.Comment string `protobuf:"bytes,7,opt,name=comment,proto3" json:"comment,omitempty"`+ // The definition of an index.+ Definition string `protobuf:"bytes,8,opt,name=definition,proto3" json:"definition,omitempty"`}func (x *IndexMetadata) Reset() {@@ -3223,6 +3225,13 @@ func (x *IndexMetadata) GetComment() string {return ""}+func (x *IndexMetadata) GetDefinition() string {+ if x != nil {+ return x.Definition+ }+ return ""+}+// ExtensionMetadata is the metadata for extensions.type ExtensionMetadata struct {state protoimpl.MessageState@@ -5831,7 +5840,7 @@ var file_v1_database_service_proto_rawDesc = []byte{0x4f, 0x44, 0x45, 0x5f, 0x44, 0x45, 0x46, 0x41, 0x55, 0x4c, 0x54, 0x10, 0x01, 0x12, 0x14, 0x0a,0x10, 0x4d, 0x4f, 0x44, 0x45, 0x5f, 0x41, 0x50, 0x50, 0x45, 0x4e, 0x44, 0x5f, 0x4f, 0x4e, 0x4c,0x59, 0x10, 0x02, 0x12, 0x14, 0x0a, 0x10, 0x4d, 0x4f, 0x44, 0x45, 0x5f, 0x49, 0x4e, 0x53, 0x45,- 0x52, 0x54, 0x5f, 0x4f, 0x4e, 0x4c, 0x59, 0x10, 0x03, 0x22, 0xbf, 0x01, 0x0a, 0x0d, 0x49, 0x6e,+ 0x52, 0x54, 0x5f, 0x4f, 0x4e, 0x4c, 0x59, 0x10, 0x03, 0x22, 0xdf, 0x01, 0x0a, 0x0d, 0x49, 0x6e,0x64, 0x65, 0x78, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x6e,0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12,0x20, 0x0a, 0x0b, 0x65, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x02,@@ -5843,7 +5852,9 @@ var file_v1_database_service_proto_rawDesc = []byte{0x70, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x69, 0x73, 0x69, 0x62,0x6c, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c,0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x07, 0x20, 0x01,- 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0x7b, 0x0a, 0x11, 0x45,+ 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x1e, 0x0a, 0x0a, 0x64,+ 0x65, 0x66, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52,+ 0x0a, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x7b, 0x0a, 0x11, 0x45,0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61,0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04,0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x18, 0x02,diff --git a/proto/store/database.proto b/proto/store/database.protoindex 5b963b5cf8..ce0fb7a5de 100644--- a/proto/store/database.proto+++ b/proto/store/database.proto@@ -321,6 +321,9 @@ message IndexMetadata {// The comment is the comment of an index.string comment = 7;++ // The definition of an index.+ string definition = 8;}// ExtensionMetadata is the metadata for extensions.diff --git a/proto/v1/database_service.proto b/proto/v1/database_service.protoindex c9d0f9f60a..f63a37ac62 100644--- a/proto/v1/database_service.proto+++ b/proto/v1/database_service.proto@@ -705,6 +705,9 @@ message IndexMetadata {// The comment is the comment of an index.string comment = 7;++ // The definition of an index.+ string definition = 8;}// ExtensionMetadata is the metadata for extensions.