Skip to content

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

chore: instance change history drop instance_id not null constraint (#5060)

p0ny8532be909f3f
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.go
index bfd371eb9d..38adc24394 100644
--- a/backend/plugin/db/driver.go
+++ b/backend/plugin/db/driver.go
@@ -143,7 +143,7 @@ type MigrationInfo struct {
ReleaseVersion string
Version string
Namespace string
- InstanceID int
+ InstanceID *int
Database string
DatabaseID *int
Environment string
@@ -350,7 +350,7 @@ type MigrationHistoryFind struct {
// Fields below should be set if fetching from metaDB instance_change_history table.
DatabaseID *int
- InstanceID int
+ InstanceID *int
}
// ConnectionConfig is the configuration for connections.
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index ed3bfd58ea..66c7de4be2 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -77,7 +77,7 @@ func preMigration(ctx context.Context, stores *store.Store, profile config.Profi
}
mi := &db.MigrationInfo{
- InstanceID: instance.UID,
+ InstanceID: &instance.UID,
DatabaseID: &database.UID,
CreatorID: api.SystemBotID,
ReleaseVersion: profile.Version,
@@ -93,7 +93,7 @@ func preMigration(ctx context.Context, stores *store.Store, profile config.Profi
log.Error("failed to find containing issue", zap.Error(err))
}
if issue != nil {
- // Concate issue title and task name as the migration description so that user can see
+ // Concat issue title and task name as the migration description so that user can see
// more context of the migration.
mi.Description = fmt.Sprintf("%s - %s", issue.Title, task.Name)
mi.IssueID = strconv.Itoa(issue.UID)
diff --git a/backend/server/instance.go b/backend/server/instance.go
index c3628be157..61e0c6c0ce 100644
--- a/backend/server/instance.go
+++ b/backend/server/instance.go
@@ -379,7 +379,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
}
var entry *db.MigrationHistory
- find := &db.MigrationHistoryFind{ID: &historyID, InstanceID: instanceID}
+ find := &db.MigrationHistoryFind{ID: &historyID, InstanceID: &instanceID}
if instance.Engine == db.Redis || instance.Engine == db.Oracle {
list, err := s.store.FindInstanceChangeHistoryList(ctx, find)
if err != nil {
@@ -468,7 +468,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
var migrationHistoryList []*db.MigrationHistory
find := &db.MigrationHistoryFind{
- InstanceID: instance.UID,
+ InstanceID: &instance.UID,
}
if instance.Engine == db.Redis || instance.Engine == db.Oracle {
if databaseStr := c.QueryParams().Get("database"); databaseStr != "" {
diff --git a/backend/server/server.go b/backend/server/server.go
index f3b0cb0e60..d2260b1085 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -1084,7 +1084,7 @@ func (s *Server) backfillInstanceChangeHistory(ctx context.Context) {
var errList error
for _, instance := range instanceList {
- err := func() error {
+ err := func(instance *store.InstanceMessage) error {
if instance.Engine == db.Redis || instance.Engine == db.Oracle || instance.Engine == db.Spanner || instance.Engine == db.MongoDB {
return nil
}
@@ -1108,7 +1108,7 @@ func (s *Server) backfillInstanceChangeHistory(ctx context.Context) {
defer driver.Close(ctx)
history, err := driver.FindMigrationHistoryList(ctx, &db.MigrationHistoryFind{
- InstanceID: instance.UID,
+ InstanceID: &instance.UID,
})
if err != nil {
return err
@@ -1152,7 +1152,7 @@ func (s *Server) backfillInstanceChangeHistory(ctx context.Context) {
CreatedTs: h.CreatedTs,
UpdaterID: updaterID,
UpdatedTs: h.UpdatedTs,
- InstanceID: instance.UID,
+ InstanceID: &instance.UID,
DatabaseID: databaseID,
IssueID: issueID,
ReleaseVersion: h.ReleaseVersion,
@@ -1176,7 +1176,7 @@ func (s *Server) backfillInstanceChangeHistory(ctx context.Context) {
return err
}
return nil
- }()
+ }(instance)
if err != nil {
errList = multierr.Append(errList, err)
}
diff --git a/backend/store/instance_change_history.go b/backend/store/instance_change_history.go
index d8a7227a22..6fdc9b7d4c 100644
--- a/backend/store/instance_change_history.go
+++ b/backend/store/instance_change_history.go
@@ -14,11 +14,12 @@ import (
// InstanceChangeHistoryMessage records the change history of an instance.
// it deprecates the old MigrationHistory.
type InstanceChangeHistoryMessage struct {
- CreatorID int
- CreatedTs int64
- UpdaterID int
- UpdatedTs int64
- InstanceID int
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+ // nil means bytebase meta instance.
+ InstanceID *int
DatabaseID *int
IssueID *int
ReleaseVersion string
@@ -42,7 +43,7 @@ type InstanceChangeHistoryMessage struct {
// FindInstanceChangeHistoryMessage is for listing a list of instance change history.
type FindInstanceChangeHistoryMessage struct {
ID *int64
- InstanceID int
+ InstanceID *int
DatabaseID *int
Source *db.MigrationSource
Version *string
@@ -433,7 +434,7 @@ func (s *Store) updateInstanceChangeHistory(ctx context.Context, update *UpdateI
return tx.Commit()
}
-func (*Store) getLargestInstanceChangeHistorySequenceImpl(ctx context.Context, tx *Tx, instanceID int, databaseID *int, baseline bool) (int64, error) {
+func (*Store) getLargestInstanceChangeHistorySequenceImpl(ctx context.Context, tx *Tx, instanceID *int, databaseID *int, baseline bool) (int64, error) {
query := `
SELECT
MAX(sequence)
@@ -458,7 +459,7 @@ func (*Store) getLargestInstanceChangeHistorySequenceImpl(ctx context.Context, t
}
// GetLargestInstanceChangeHistorySequence will get the largest sequence number.
-func (s *Store) GetLargestInstanceChangeHistorySequence(ctx context.Context, instanceID int, databaseID *int, baseline bool) (int64, error) {
+func (s *Store) GetLargestInstanceChangeHistorySequence(ctx context.Context, instanceID *int, databaseID *int, baseline bool) (int64, error) {
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return -1, err
@@ -478,7 +479,7 @@ func (s *Store) GetLargestInstanceChangeHistorySequence(ctx context.Context, ins
}
// GetLargestInstanceChangeHistoryVersionSinceBaseline will get the largest version since last baseline or branch.
-func (s *Store) GetLargestInstanceChangeHistoryVersionSinceBaseline(ctx context.Context, instanceID int, databaseID *int) (*string, error) {
+func (s *Store) GetLargestInstanceChangeHistoryVersionSinceBaseline(ctx context.Context, instanceID *int, databaseID *int) (*string, error) {
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
diff --git a/backend/store/migration/dev/LATEST.sql b/backend/store/migration/dev/LATEST.sql
index 3a429428fa..747c62ed86 100644
--- a/backend/store/migration/dev/LATEST.sql
+++ b/backend/store/migration/dev/LATEST.sql
@@ -710,7 +710,8 @@ CREATE TABLE instance_change_history (
created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
- instance_id INTEGER NOT NULL REFERENCES instance (id),
+ -- NULL means the migrations for Bytebase's own metadata database.
+ instance_id INTEGER REFERENCES instance (id),
-- NULL means an instance-level change.
database_id INTEGER REFERENCES db (id),
-- issue_id is nullable because this field is backfilled and may not be present.
diff --git a/backend/store/migration/prod/1.14/0000##add_instance_change_history.sql b/backend/store/migration/prod/1.14/0000##add_instance_change_history.sql
index 1af39fd6e3..37f8f58a6b 100644
--- a/backend/store/migration/prod/1.14/0000##add_instance_change_history.sql
+++ b/backend/store/migration/prod/1.14/0000##add_instance_change_history.sql
@@ -6,7 +6,8 @@ CREATE TABLE instance_change_history (
created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
- instance_id INTEGER NOT NULL REFERENCES instance (id),
+ -- NULL means the migrations for Bytebase's own metadata database.
+ instance_id INTEGER REFERENCES instance (id),
-- NULL means an instance-level change.
database_id INTEGER REFERENCES db (id),
-- issue_id is nullable because this field is backfilled and may not be present.
diff --git a/backend/store/migration/prod/LATEST.sql b/backend/store/migration/prod/LATEST.sql
index 2dabcc14c5..13326bca28 100644
--- a/backend/store/migration/prod/LATEST.sql
+++ b/backend/store/migration/prod/LATEST.sql
@@ -709,7 +709,8 @@ CREATE TABLE instance_change_history (
created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
- instance_id INTEGER NOT NULL REFERENCES instance (id),
+ -- NULL means the migrations for Bytebase's own metadata database.
+ instance_id INTEGER REFERENCES instance (id),
-- NULL means an instance-level change.
database_id INTEGER REFERENCES db (id),
-- issue_id is nullable because this field is backfilled and may not be present.
diff --git a/backend/utils/utils.go b/backend/utils/utils.go
index 2f6c836c7a..202bbdf7c7 100644
--- a/backend/utils/utils.go
+++ b/backend/utils/utils.go
@@ -29,7 +29,7 @@ func GetLatestSchemaVersion(ctx context.Context, store *store.Store, driver db.D
// TODO(d): support semantic versioning.
limit := 1
find := &db.MigrationHistoryFind{
- InstanceID: instanceID,
+ InstanceID: &instanceID,
Database: &databaseName,
DatabaseID: &databaseID,
Limit: &limit,