Skip to content

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

fix(sync): guard against nil database in schema syncer [BYT-9309] (#20071)

h3n4l8ab84d8166ef
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 0965c9450e..c3b0a7fd8d 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -636,6 +636,17 @@ func queryRetry(
if err != nil {
return nil, nil, duration, err
}
+ if d == nil {
+ // The referenced database is not tracked by Bytebase (e.g. it was
+ // dropped, excluded by sync filters, or never discovered yet).
+ // Skip the sync attempt and leave the span's NotFoundError in place
+ // so the masking policy below can reject the query cleanly instead
+ // of panicking on a nil *DatabaseMessage.
+ slog.Debug("skip metadata sync: database not tracked",
+ slog.String("instance", instance.ResourceID),
+ slog.String("database", accessDatabaseName))
+ continue
+ }
if err := schemaSyncer.SyncDatabaseSchema(ctx, d); err != nil {
return nil, nil, duration, errors.Wrapf(err, "failed to sync database schema for database %q", accessDatabaseName)
}
diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.go
index 039ae46173..163bd56057 100644
--- a/backend/runner/schemasync/syncer.go
+++ b/backend/runner/schemasync/syncer.go
@@ -371,6 +371,9 @@ func (s *Syncer) SyncInstance(ctx context.Context, instance *store.InstanceMessa
// doSyncDatabaseSchema is the core implementation that syncs the schema for a database and optionally creates a sync history record.
func (s *Syncer) doSyncDatabaseSchema(ctx context.Context, database *store.DatabaseMessage, createSyncHistory bool) (syncHistoryResourceID string, retErr error) {
+ if database == nil {
+ return "", errors.New("cannot sync nil database")
+ }
instance, err := s.store.GetInstanceByResourceID(ctx, database.InstanceID)
if err != nil {
return "", errors.Wrapf(err, "failed to get instance %q", database.InstanceID)
diff --git a/backend/runner/schemasync/syncer_test.go b/backend/runner/schemasync/syncer_test.go
new file mode 100644
index 0000000000..b35ccd1b37
--- /dev/null
+++ b/backend/runner/schemasync/syncer_test.go
@@ -0,0 +1,29 @@
+package schemasync
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+// TestSyncDatabaseSchemaNilDatabase pins the guard that prevents a nil
+// *store.DatabaseMessage from panicking the syncer. Callers may receive
+// (nil, nil) from store.GetDatabase when the referenced database is not
+// tracked by Bytebase; the syncer must return a descriptive error instead
+// of dereferencing the nil pointer. Regression test for BYT-9309.
+func TestSyncDatabaseSchemaNilDatabase(t *testing.T) {
+ s := &Syncer{}
+
+ _, err := s.doSyncDatabaseSchema(context.Background(), nil, false)
+ require.Error(t, err)
+ require.Contains(t, err.Error(), "nil database")
+
+ err = s.SyncDatabaseSchema(context.Background(), nil)
+ require.Error(t, err)
+ require.Contains(t, err.Error(), "nil database")
+
+ _, err = s.SyncDatabaseSchemaToHistory(context.Background(), nil)
+ require.Error(t, err)
+ require.Contains(t, err.Error(), "nil database")
+}