Skip to content

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

fix: startup crash from nil licenseService and non-idempotent migration (#19727)

zchpeter7a6dfd827811
diff --git a/backend/migrator/migration/3.17/0005##instance_scoped_composite_pk.sql b/backend/migrator/migration/3.17/0005##instance_scoped_composite_pk.sql
index e99b178644..d968f6f045 100644
--- a/backend/migrator/migration/3.17/0005##instance_scoped_composite_pk.sql
+++ b/backend/migrator/migration/3.17/0005##instance_scoped_composite_pk.sql
@@ -1,7 +1,8 @@
-- Step 2e: Switch revision, sync_history, changelog to resource_id PKs.
--- Phase A: Drop FKs
+-- Phase A: Drop FKs (drop both old and new names for idempotency on re-run)
ALTER TABLE changelog DROP CONSTRAINT IF EXISTS changelog_sync_history_id_fkey;
+ALTER TABLE changelog DROP CONSTRAINT IF EXISTS changelog_sync_history_fkey;
-- Phase B: revision — promote resource_id to PK
DROP INDEX IF EXISTS idx_revision_unique_resource_id;
@@ -23,8 +24,11 @@ ALTER TABLE changelog ADD PRIMARY KEY (resource_id);
-- Phase E: changelog — add sync_history text column, backfill, drop old sync_history_id
ALTER TABLE changelog ADD COLUMN IF NOT EXISTS sync_history text;
-UPDATE changelog SET sync_history = sync_history.resource_id
- FROM sync_history WHERE changelog.sync_history IS NULL AND changelog.sync_history_id = sync_history.id;
+DO $$ BEGIN
+ IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'changelog' AND column_name = 'sync_history_id') THEN
+ EXECUTE 'UPDATE changelog SET sync_history = sync_history.resource_id FROM sync_history WHERE changelog.sync_history IS NULL AND changelog.sync_history_id = sync_history.id';
+ END IF;
+END $$;
ALTER TABLE changelog DROP COLUMN IF EXISTS sync_history_id;
-- Phase F: Re-add FK from changelog.sync_history to sync_history.resource_id
diff --git a/backend/server/server.go b/backend/server/server.go
index fb490f4400..02a0f1368b 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -154,6 +154,11 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {
s.store = stores
sheetManager := sheet.NewManager()
+ s.licenseService, err = enterprise.NewLicenseService(profile.Mode, stores, profile.SaaS)
+ if err != nil {
+ return nil, errors.Wrap(err, "failed to create license service")
+ }
+
logSetup := &storepb.WorkspaceProfileSetting{
EnableAuditLogStdout: false,
EnableMetricCollection: true,
@@ -194,11 +199,6 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {
return nil, errors.Wrapf(err, "failed to create message bus")
}
- s.licenseService, err = enterprise.NewLicenseService(profile.Mode, stores, profile.SaaS)
- if err != nil {
- return nil, errors.Wrap(err, "failed to create license service")
- }
-
// The auth secret is a global infrastructure value stored in server_config.
secret, err := s.store.GetAuthSecret(ctx)
if err != nil {