Skip to content

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

feat: handle revision type in begin migration (#17087)

p0ny3ebe707f0e11
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.go
index 7c5dfd3443..4244a4c472 100644
--- a/backend/api/v1/release_service_check.go
+++ b/backend/api/v1/release_service_check.go
@@ -153,6 +153,7 @@ func (s *ReleaseService) CheckRelease(ctx context.Context, req *connect.Request[
revisionMap[revision.Version] = revision
}
+ // TODO(p0ny): handle declarative type
for _, file := range request.Release.Files {
if stopChecking {
break
diff --git a/backend/common/util.go b/backend/common/util.go
index 6d77c973ac..80ce899518 100644
--- a/backend/common/util.go
+++ b/backend/common/util.go
@@ -228,3 +228,7 @@ func IsNil(val any) bool {
return false
}
+
+func NewP[T any](x T) *T {
+ return &x
+}
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index 43b9b39265..0e947b9b75 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -412,18 +412,46 @@ func beginMigration(ctx context.Context, stores *store.Store, mc *migrateContext
// what they are doing
// TODO(p0ny): handle SDL case
if mc.version != "" {
- list, err := stores.ListRevisions(ctx, &store.FindRevisionMessage{
- InstanceID: &mc.database.InstanceID,
- DatabaseName: &mc.database.DatabaseName,
- Version: &mc.version,
- })
- if err != nil {
- return false, errors.Wrapf(err, "failed to list revisions")
- }
- if len(list) > 0 {
- // This version has been executed.
- // skip execution.
- return true, nil
+ if mc.task.Type == storepb.Task_DATABASE_SCHEMA_UPDATE_SDL {
+ list, err := stores.ListRevisions(ctx, &store.FindRevisionMessage{
+ InstanceID: &mc.database.InstanceID,
+ DatabaseName: &mc.database.DatabaseName,
+ Limit: common.NewP(1),
+ Type: common.NewP(storepb.RevisionPayload_DECLARATIVE),
+ })
+ if err != nil {
+ return false, errors.Wrapf(err, "failed to list revisions")
+ }
+ if len(list) > 0 {
+ // If the version is higher than the current version, return error
+ latestRevision := list[0]
+ latestVersion, err := model.NewVersion(latestRevision.Version)
+ if err != nil {
+ return false, errors.Wrapf(err, "failed to parse latest revision version %q", latestRevision.Version)
+ }
+ currentVersion, err := model.NewVersion(mc.version)
+ if err != nil {
+ return false, errors.Wrapf(err, "failed to parse current version %q", mc.version)
+ }
+ if currentVersion.LessThan(latestVersion) {
+ return false, errors.Errorf("cannot apply SDL migration with version %s because a newer version %s already exists", mc.version, latestRevision.Version)
+ }
+ }
+ } else {
+ list, err := stores.ListRevisions(ctx, &store.FindRevisionMessage{
+ InstanceID: &mc.database.InstanceID,
+ DatabaseName: &mc.database.DatabaseName,
+ Version: &mc.version,
+ Type: common.NewP(storepb.RevisionPayload_VERSIONED),
+ })
+ if err != nil {
+ return false, errors.Wrapf(err, "failed to list revisions")
+ }
+ if len(list) > 0 {
+ // This version has been executed.
+ // skip execution.
+ return true, nil
+ }
}
}
diff --git a/backend/store/revision.go b/backend/store/revision.go
index ba47671ba3..3658a1bd7b 100644
--- a/backend/store/revision.go
+++ b/backend/store/revision.go
@@ -31,6 +31,7 @@ type FindRevisionMessage struct {
UID *int64
InstanceID *string
DatabaseName *string
+ Type *storepb.RevisionPayload_Type
Version *string
Versions *[]string
@@ -41,6 +42,8 @@ type FindRevisionMessage struct {
ShowDeleted bool
}
+// ListRevisions lists revisions.
+// The results are ordered by version desc.
func (s *Store) ListRevisions(ctx context.Context, find *FindRevisionMessage) ([]*RevisionMessage, error) {
where, args := []string{"TRUE"}, []any{}
@@ -56,6 +59,10 @@ func (s *Store) ListRevisions(ctx context.Context, find *FindRevisionMessage) ([
where = append(where, fmt.Sprintf("db_name = $%d", len(args)+1))
args = append(args, *v)
}
+ if v := find.Type; v != nil {
+ where = append(where, fmt.Sprintf("payload->>'type' = $%d", len(args)+1))
+ args = append(args, v.String())
+ }
if v := find.Version; v != nil {
where = append(where, fmt.Sprintf("version = $%d", len(args)+1))
args = append(args, *v)