Skip to content

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

feat: add `useDBSchemaStore` module (#3896)

boojackbd485fe80483
diff --git a/api/db_schema.go b/api/db_schema.go
index f63fad4db6..5eaa7a6a4c 100644
--- a/api/db_schema.go
+++ b/api/db_schema.go
@@ -2,7 +2,7 @@ package api
// DBSchema is the API message for database schema.
type DBSchema struct {
- ID int `jsonapi:"primary,dbSchema"`
+ ID int
// Standard fields
@@ -10,8 +10,8 @@ type DBSchema struct {
DatabaseID int
// Domain specific fields
- Metadata string `jsonapi:"attr,metadata"`
- RawDump string `jsonapi:"attr,rawDump"`
+ Metadata string
+ RawDump string
}
// DBSchemaUpsert is the API message for creating a database schema.
diff --git a/frontend/src/store/modules/dbSchema.ts b/frontend/src/store/modules/dbSchema.ts
new file mode 100644
index 0000000000..692c8c2880
--- /dev/null
+++ b/frontend/src/store/modules/dbSchema.ts
@@ -0,0 +1,26 @@
+import axios from "axios";
+import { defineStore } from "pinia";
+import { DatabaseId, DBSchemaState } from "@/types";
+import { DatabaseMetadata } from "@/types/proto/database";
+
+export const useDBSchemaStore = defineStore("dbSchema", {
+ state: (): DBSchemaState => ({
+ databaseMetadataById: new Map(),
+ }),
+ actions: {
+ async getOrFetchDatabaseMetadata(
+ databaseId: DatabaseId
+ ): Promise<DatabaseMetadata> {
+ if (this.databaseMetadataById.has(databaseId)) {
+ return this.databaseMetadataById.get(databaseId) as DatabaseMetadata;
+ }
+
+ const res = await axios.get<DatabaseMetadata>(
+ `/api/database/${databaseId}/schema?metadata=true`
+ );
+ const databaseMetadata = DatabaseMetadata.fromJSON(res.data);
+ this.databaseMetadataById.set(databaseId, databaseMetadata);
+ return databaseMetadata;
+ },
+ },
+});
diff --git a/frontend/src/store/modules/index.ts b/frontend/src/store/modules/index.ts
index ce110fe0ac..011ac01b54 100644
--- a/frontend/src/store/modules/index.ts
+++ b/frontend/src/store/modules/index.ts
@@ -44,3 +44,4 @@ export * from "./dbExtension";
export * from "./sqlReview";
export * from "./onboardingGuide";
export * from "./schemaEditor";
+export * from "./dbSchema";
diff --git a/frontend/src/types/proto/database.ts b/frontend/src/types/proto/database.ts
index 0d8fbf0177..d7480379a1 100644
--- a/frontend/src/types/proto/database.ts
+++ b/frontend/src/types/proto/database.ts
@@ -78,7 +78,7 @@ export interface ColumnMetadata {
/** The nullable is the nullable of a column. */
nullable: boolean;
/** The type is the type of a column. */
- Type: string;
+ type: string;
/** The character_set is the character_set of a column. */
characterSet: string;
/** The collation is the collation of a column. */
@@ -509,7 +509,7 @@ function createBaseColumnMetadata(): ColumnMetadata {
hasDefault: false,
default: "",
nullable: false,
- Type: "",
+ type: "",
characterSet: "",
collation: "",
comment: "",
@@ -533,8 +533,8 @@ export const ColumnMetadata = {
if (message.nullable === true) {
writer.uint32(40).bool(message.nullable);
}
- if (message.Type !== "") {
- writer.uint32(50).string(message.Type);
+ if (message.type !== "") {
+ writer.uint32(50).string(message.type);
}
if (message.characterSet !== "") {
writer.uint32(58).string(message.characterSet);
@@ -571,7 +571,7 @@ export const ColumnMetadata = {
message.nullable = reader.bool();
break;
case 6:
- message.Type = reader.string();
+ message.type = reader.string();
break;
case 7:
message.characterSet = reader.string();
@@ -597,7 +597,7 @@ export const ColumnMetadata = {
hasDefault: isSet(object.hasDefault) ? Boolean(object.hasDefault) : false,
default: isSet(object.default) ? String(object.default) : "",
nullable: isSet(object.nullable) ? Boolean(object.nullable) : false,
- Type: isSet(object.Type) ? String(object.Type) : "",
+ type: isSet(object.type) ? String(object.type) : "",
characterSet: isSet(object.characterSet) ? String(object.characterSet) : "",
collation: isSet(object.collation) ? String(object.collation) : "",
comment: isSet(object.comment) ? String(object.comment) : "",
@@ -611,7 +611,7 @@ export const ColumnMetadata = {
message.hasDefault !== undefined && (obj.hasDefault = message.hasDefault);
message.default !== undefined && (obj.default = message.default);
message.nullable !== undefined && (obj.nullable = message.nullable);
- message.Type !== undefined && (obj.Type = message.Type);
+ message.type !== undefined && (obj.type = message.type);
message.characterSet !== undefined && (obj.characterSet = message.characterSet);
message.collation !== undefined && (obj.collation = message.collation);
message.comment !== undefined && (obj.comment = message.comment);
@@ -625,7 +625,7 @@ export const ColumnMetadata = {
message.hasDefault = object.hasDefault ?? false;
message.default = object.default ?? "";
message.nullable = object.nullable ?? false;
- message.Type = object.Type ?? "";
+ message.type = object.type ?? "";
message.characterSet = object.characterSet ?? "";
message.collation = object.collation ?? "";
message.comment = object.comment ?? "";
diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.ts
index 5b876bf1c9..337dd50c4f 100644
--- a/frontend/src/types/store.ts
+++ b/frontend/src/types/store.ts
@@ -1,3 +1,4 @@
+import { RemovableRef } from "@vueuse/core";
import {
AuthProvider,
DeploymentConfig,
@@ -51,7 +52,7 @@ import { ConnectionAtom } from "./sqlEditor";
import { ReleaseInfo } from "./actuator";
import type { DebugLog } from "@/types/debug";
import type { AuditLog } from "@/types/auditLog";
-import { RemovableRef } from "@vueuse/core";
+import { DatabaseMetadata } from "./proto/database";
export interface ActuatorState {
serverInfo?: ServerInfo;
@@ -156,6 +157,10 @@ export interface DatabaseState {
databaseListByProjectId: Map<ProjectId, Database[]>;
}
+export interface DBSchemaState {
+ databaseMetadataById: Map<DatabaseId, DatabaseMetadata>;
+}
+
export interface TableState {
tableListByDatabaseId: Map<DatabaseId, Table[]>;
}
diff --git a/proto/generate_ts.sh b/proto/generate_ts.sh
index a294a30ca2..89b121fd9c 100644
--- a/proto/generate_ts.sh
+++ b/proto/generate_ts.sh
@@ -2,4 +2,9 @@
cd "$(dirname "$0")/../"
-protoc --plugin=./frontend/node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=./frontend/src/types/proto --ts_proto_opt=esModuleInterop=true -I=proto/store database.proto
+protoc \
+ --plugin=./frontend/node_modules/.bin/protoc-gen-ts_proto \
+ --ts_proto_out=./frontend/src/types/proto \
+ --ts_proto_opt=esModuleInterop=true,env=browser,useOptionals=messages,initializeFieldsAsUndefined=true \
+ -I=proto/store \
+ database.proto
diff --git a/proto/generated-go/store/database.pb.go b/proto/generated-go/store/database.pb.go
index 1800e6f8ab..f416d7dba9 100644
--- a/proto/generated-go/store/database.pb.go
+++ b/proto/generated-go/store/database.pb.go
@@ -340,7 +340,7 @@ type ColumnMetadata struct {
// The nullable is the nullable of a column.
Nullable bool `protobuf:"varint,5,opt,name=nullable,proto3" json:"nullable,omitempty"`
// The type is the type of a column.
- Type string `protobuf:"bytes,6,opt,name=Type,proto3" json:"Type,omitempty"`
+ Type string `protobuf:"bytes,6,opt,name=type,proto3" json:"type,omitempty"`
// The character_set is the character_set of a column.
CharacterSet string `protobuf:"bytes,7,opt,name=character_set,json=characterSet,proto3" json:"character_set,omitempty"`
// The collation is the collation of a column.
@@ -873,8 +873,8 @@ var file_database_proto_rawDesc = []byte{
0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x64,
0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x6e, 0x75, 0x6c, 0x6c, 0x61, 0x62,
0x6c, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x6e, 0x75, 0x6c, 0x6c, 0x61, 0x62,
- 0x6c, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09,
- 0x52, 0x04, 0x54, 0x79, 0x70, 0x65, 0x12, 0x23, 0x0a, 0x0d, 0x63, 0x68, 0x61, 0x72, 0x61, 0x63,
+ 0x6c, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09,
+ 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x23, 0x0a, 0x0d, 0x63, 0x68, 0x61, 0x72, 0x61, 0x63,
0x74, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x74, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x63,
0x68, 0x61, 0x72, 0x61, 0x63, 0x74, 0x65, 0x72, 0x53, 0x65, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x63,
0x6f, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09,
diff --git a/proto/store/database.proto b/proto/store/database.proto
index 2894d98b2b..8e1ced94a2 100644
--- a/proto/store/database.proto
+++ b/proto/store/database.proto
@@ -95,7 +95,7 @@ message ColumnMetadata {
bool nullable = 5;
// The type is the type of a column.
- string Type = 6;
+ string type = 6;
// The character_set is the character_set of a column.
string character_set = 7;
diff --git a/server/database.go b/server/database.go
index f2cb299ff9..2b92148e87 100644
--- a/server/database.go
+++ b/server/database.go
@@ -413,7 +413,6 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
return nil
}
- queryDump := c.QueryParam("metadata") == ""
dbSchema, err := s.store.GetDBSchema(ctx, id)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get dbSchema for database ID %v", id)).SetInternal(err)
@@ -433,7 +432,8 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
dbSchema = newDBSchema
}
- if queryDump {
+ isQueryRawDump := c.QueryParam("metadata") == ""
+ if isQueryRawDump {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextPlainCharsetUTF8)
if _, err := c.Response().Write([]byte(dbSchema.RawDump)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to write schema response for database %v", id)).SetInternal(err)
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 094238b583..2dfe47ddf6 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -1892,7 +1892,7 @@ CREATE TABLE public.book (
);
`,
- wantDatabaseMetadata: `{"name": "latestSchema", "schemas": [{"name": "public", "tables": [{"name": "book", "columns": [{"Type": "integer", "name": "id", "nullable": true, "position": 1, "hasDefault": true}, {"Type": "text", "name": "name", "nullable": true, "position": 2, "hasDefault": true}], "dataSize": "8192"}]}], "collation": "en_US.UTF-8", "characterSet": "UTF8"}`,
+ wantDatabaseMetadata: `{"name": "latestSchema", "schemas": [{"name": "public", "tables": [{"name": "book", "columns": [{"name": "id", "type": "integer", "nullable": true, "position": 1, "hasDefault": true}, {"name": "name", "type": "text", "nullable": true, "position": 2, "hasDefault": true}], "dataSize": "8192"}]}], "collation": "en_US.UTF-8", "characterSet": "UTF8"}`,
},
}
a := require.New(t)