Skip to content

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

refactor: pass in schema to store so that we can sync the whole schema together later (#1878)

Danny Xu0e6a8b695c84
diff --git a/server/sql.go b/server/sql.go
index d30f6a46b1..8383e48ba6 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -683,34 +683,11 @@ func syncTableSchema(ctx context.Context, store *store.Store, database *api.Data
}
func syncViewSchema(ctx context.Context, store *store.Store, database *api.Database, schema *db.Schema) error {
- var creates []*api.ViewCreate
- for _, view := range schema.ViewList {
- creates = append(creates, &api.ViewCreate{
- CreatorID: api.SystemBotID,
- CreatedTs: view.CreatedTs,
- UpdatedTs: view.UpdatedTs,
- DatabaseID: database.ID,
- Name: view.Name,
- Definition: view.Definition,
- Comment: view.Comment,
- })
- }
- return store.SetViewList(ctx, creates, database.ID, api.SystemBotID)
+ return store.SetViewList(ctx, schema, database.ID, api.SystemBotID)
}
func syncDBExtensionSchema(ctx context.Context, store *store.Store, database *api.Database, schema *db.Schema) error {
- var creates []*api.DBExtensionCreate
- for _, dbExtension := range schema.ExtensionList {
- creates = append(creates, &api.DBExtensionCreate{
- CreatorID: api.SystemBotID,
- DatabaseID: database.ID,
- Name: dbExtension.Name,
- Version: dbExtension.Version,
- Schema: dbExtension.Schema,
- Description: dbExtension.Description,
- })
- }
- return store.SetDBExtensionList(ctx, creates, database.ID, api.SystemBotID)
+ return store.SetDBExtensionList(ctx, schema, database.ID, api.SystemBotID)
}
func getLatestSchemaVersion(ctx context.Context, driver db.Driver, databaseName string) (string, error) {
diff --git a/store/extension.go b/store/extension.go
index 08839c9e2c..83b1500e36 100644
--- a/store/extension.go
+++ b/store/extension.go
@@ -8,6 +8,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/db"
)
// dbExtensionRaw is the store model for an DBExtension.
@@ -77,7 +78,18 @@ type extensionKey struct {
}
// SetDBExtensionList sets the extensions for a database.
-func (s *Store) SetDBExtensionList(ctx context.Context, dbExtensionCreateList []*api.DBExtensionCreate, databaseID int, updaterID int) error {
+func (s *Store) SetDBExtensionList(ctx context.Context, schema *db.Schema, databaseID int, updaterID int) error {
+ var newDBExtensionList []*api.DBExtensionCreate
+ for _, dbExtension := range schema.ExtensionList {
+ newDBExtensionList = append(newDBExtensionList, &api.DBExtensionCreate{
+ CreatorID: api.SystemBotID,
+ DatabaseID: databaseID,
+ Name: dbExtension.Name,
+ Version: dbExtension.Version,
+ Schema: dbExtension.Schema,
+ Description: dbExtension.Description,
+ })
+ }
oldDBExtensionRawList, err := s.findDBExtensionRaw(ctx, &api.DBExtensionFind{
DatabaseID: &databaseID,
})
@@ -91,7 +103,7 @@ func (s *Store) SetDBExtensionList(ctx context.Context, dbExtensionCreateList []
}
defer tx.PTx.Rollback()
- deletes, creates := generateDBExtensionActions(oldDBExtensionRawList, dbExtensionCreateList)
+ deletes, creates := generateDBExtensionActions(oldDBExtensionRawList, newDBExtensionList)
for _, d := range deletes {
if err := deleteDBExtensionImpl(ctx, tx.PTx, d); err != nil {
return err
diff --git a/store/view.go b/store/view.go
index 5b5bb3451b..265aef992f 100644
--- a/store/view.go
+++ b/store/view.go
@@ -8,6 +8,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/db"
)
// viewRaw is the store model for an View.
@@ -70,7 +71,20 @@ func (s *Store) FindView(ctx context.Context, find *api.ViewFind) ([]*api.View,
}
// SetViewList sets the views for a database.
-func (s *Store) SetViewList(ctx context.Context, viewCreateList []*api.ViewCreate, databaseID int, updaterID int) error {
+func (s *Store) SetViewList(ctx context.Context, schema *db.Schema, databaseID int, updaterID int) error {
+ var viewCreateList []*api.ViewCreate
+ for _, view := range schema.ViewList {
+ viewCreateList = append(viewCreateList, &api.ViewCreate{
+ CreatorID: api.SystemBotID,
+ CreatedTs: view.CreatedTs,
+ UpdatedTs: view.UpdatedTs,
+ DatabaseID: databaseID,
+ Name: view.Name,
+ Definition: view.Definition,
+ Comment: view.Comment,
+ })
+ }
+
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return FormatError(err)