Skip to content

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

fix(review): read the PostgreSQL archive schema from the synced metadata

rebeliced3bc4f99e521
diff --git a/backend/runner/reviewrun/rule_executor.go b/backend/runner/reviewrun/rule_executor.go
index 4aabaad1d3..ee352067b4 100644
--- a/backend/runner/reviewrun/rule_executor.go
+++ b/backend/runner/reviewrun/rule_executor.go
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
+ metadatapb "github.com/bytebase/omni/metadata"
"github.com/bytebase/omni/review"
"github.com/pkg/errors"
@@ -172,14 +173,9 @@ func (e *RuleExecutor) resolveReviewTarget(ctx context.Context, project *store.P
}
schema := dbSchema.GetProto()
- backupDatabaseName := common.BackupDatabaseNameOfEngine(engine)
- backupDatabase, err := e.store.GetDatabase(ctx, &store.FindDatabaseMessage{
- Workspace: instance.Workspace,
- InstanceID: &instance.ResourceID,
- DatabaseName: &backupDatabaseName,
- })
+ backupDatabaseExists, err := e.backupDatabaseExists(ctx, instance, schema)
if err != nil {
- return nil, errors.Wrapf(err, "failed to look up backup database %q", backupDatabaseName)
+ return nil, err
}
return &reviewTarget{
@@ -187,13 +183,42 @@ func (e *RuleExecutor) resolveReviewTarget(ctx context.Context, project *store.P
Engine: engine,
Input: review.Target{
Schema: schema,
- BackupDatabaseExists: backupDatabase != nil,
+ BackupDatabaseExists: backupDatabaseExists,
SessionUser: sessionUser(project, instance, schema.GetOwner()),
LowerCaseTableNames: int(instance.Metadata.GetMysqlLowerCaseTableNames()),
},
}, nil
}
+// backupDatabaseExists reports whether the engine's backup location is in
+// place, as the schema syncer decides it: PostgreSQL keeps the archive as a
+// schema inside the database, the other engines with prior backup as a
+// database on the instance.
+func (e *RuleExecutor) backupDatabaseExists(ctx context.Context, instance *store.InstanceMessage, schema *metadatapb.DatabaseSchemaMetadata) (bool, error) {
+ engine := instance.Metadata.GetEngine()
+ if !common.EngineSupportPriorBackup(engine) {
+ return false, nil
+ }
+ name := common.BackupDatabaseNameOfEngine(engine)
+ if engine == storepb.Engine_POSTGRES {
+ for _, s := range schema.GetSchemas() {
+ if s.GetName() == name {
+ return true, nil
+ }
+ }
+ return false, nil
+ }
+ backupDatabase, err := e.store.GetDatabase(ctx, &store.FindDatabaseMessage{
+ Workspace: instance.Workspace,
+ InstanceID: &instance.ResourceID,
+ DatabaseName: &name,
+ })
+ if err != nil {
+ return false, errors.Wrapf(err, "failed to look up backup database %q", name)
+ }
+ return backupDatabase != nil, nil
+}
+
// sessionUser is the role the change runs as: the database owner when the
// project's PostgreSQL tenant mode switches to it, else the admin data
// source's login user. Empty opts out of the engine's ownership checks.
diff --git a/backend/runner/reviewrun/rule_review_test.go b/backend/runner/reviewrun/rule_review_test.go
index 254ca764d4..ae7e56339f 100644
--- a/backend/runner/reviewrun/rule_review_test.go
+++ b/backend/runner/reviewrun/rule_review_test.go
@@ -1,9 +1,11 @@
package reviewrun
import (
+ "context"
"errors"
"testing"
+ metadatapb "github.com/bytebase/omni/metadata"
"github.com/bytebase/omni/review"
"github.com/stretchr/testify/require"
@@ -145,6 +147,28 @@ func TestReviewResultComments(t *testing.T) {
require.Equal(t, &storepb.Position{Line: 3}, line.Payload.StatementAnchor.EndPosition)
}
+// TestBackupDatabaseExistsPostgres reads the PostgreSQL archive schema from
+// the synced metadata; no store lookup is involved, so the executor has none.
+func TestBackupDatabaseExistsPostgres(t *testing.T) {
+ t.Parallel()
+ e := &RuleExecutor{}
+ pg := &store.InstanceMessage{Metadata: &storepb.Instance{Engine: storepb.Engine_POSTGRES}}
+ withArchive := &metadatapb.DatabaseSchemaMetadata{Schemas: []*metadatapb.SchemaMetadata{{Name: "public"}, {Name: "bbdataarchive"}}}
+ withoutArchive := &metadatapb.DatabaseSchemaMetadata{Schemas: []*metadatapb.SchemaMetadata{{Name: "public"}}}
+
+ exists, err := e.backupDatabaseExists(context.Background(), pg, withArchive)
+ require.NoError(t, err)
+ require.True(t, exists)
+ exists, err = e.backupDatabaseExists(context.Background(), pg, withoutArchive)
+ require.NoError(t, err)
+ require.False(t, exists)
+
+ // An engine without prior backup never has a backup location.
+ exists, err = e.backupDatabaseExists(context.Background(), &store.InstanceMessage{Metadata: &storepb.Instance{Engine: storepb.Engine_SNOWFLAKE}}, withArchive)
+ require.NoError(t, err)
+ require.False(t, exists)
+}
+
func TestSessionUser(t *testing.T) {
t.Parallel()
pg := &store.InstanceMessage{Metadata: &storepb.Instance{