fix: make migration 032 idempotent and fix UUID type in credentials bootstrap

- Migration 032: add NOT EXISTS check to avoid duplicate key violation
- CredentialsBootstrap: use NULLIF for updated_by UUID column, pass empty string instead of system_bootstrap
This commit is contained in:
GoHorse Deploy 2026-02-07 17:22:15 +00:00
parent fee98a651b
commit 1c29e469a7
2 changed files with 6 additions and 3 deletions

View file

@ -33,7 +33,7 @@ func NewCredentialsService(db *sql.DB) *CredentialsService {
func (s *CredentialsService) SaveCredentials(ctx context.Context, serviceName, encryptedPayload, updatedBy string) error { func (s *CredentialsService) SaveCredentials(ctx context.Context, serviceName, encryptedPayload, updatedBy string) error {
query := ` query := `
INSERT INTO external_services_credentials (service_name, encrypted_payload, updated_by, updated_at) INSERT INTO external_services_credentials (service_name, encrypted_payload, updated_by, updated_at)
VALUES ($1, $2, $3, NOW()) VALUES ($1, $2, NULLIF($3, '')::uuid, NOW())
ON CONFLICT (service_name) ON CONFLICT (service_name)
DO UPDATE SET DO UPDATE SET
encrypted_payload = EXCLUDED.encrypted_payload, encrypted_payload = EXCLUDED.encrypted_payload,
@ -334,7 +334,7 @@ func (s *CredentialsService) BootstrapCredentials(ctx context.Context) error {
fmt.Printf("[CredentialsBootstrap] Failed to encrypt %s: %v\n", service, err) fmt.Printf("[CredentialsBootstrap] Failed to encrypt %s: %v\n", service, err)
continue continue
} }
if err := s.SaveCredentials(ctx, service, encrypted, "system_bootstrap"); err != nil { if err := s.SaveCredentials(ctx, service, encrypted, ""); err != nil {
fmt.Printf("[CredentialsBootstrap] Failed to save %s: %v\n", service, err) fmt.Printf("[CredentialsBootstrap] Failed to save %s: %v\n", service, err)
} else { } else {
fmt.Printf("[CredentialsBootstrap] Successfully migrated %s\n", service) fmt.Printf("[CredentialsBootstrap] Successfully migrated %s\n", service)

View file

@ -1,9 +1,11 @@
-- Migration: Update Super Admin to 'lol' and force password reset -- Migration: Update Super Admin to 'lol' and force password reset
-- Description: Updates the superadmin identifier, email, name, and sets status to enforce password change. -- Description: Updates the superadmin identifier, email, name, and sets status to enforce password change.
-- Made idempotent: only runs if target identifier doesn't already exist.
-- Increase status column length to support 'force_change_password' (21 chars) -- Increase status column length to support 'force_change_password' (21 chars)
ALTER TABLE users ALTER COLUMN status TYPE VARCHAR(50); ALTER TABLE users ALTER COLUMN status TYPE VARCHAR(50);
-- Only update if 'lol' identifier doesn't already exist
UPDATE users UPDATE users
SET SET
identifier = 'lol', identifier = 'lol',
@ -12,4 +14,5 @@ SET
name = 'Dr. Horse Expert', name = 'Dr. Horse Expert',
status = 'force_change_password', status = 'force_change_password',
updated_at = CURRENT_TIMESTAMP updated_at = CURRENT_TIMESTAMP
WHERE identifier = 'superadmin' OR email = 'admin@gohorsejobs.com'; WHERE (identifier = 'superadmin' OR email = 'admin@gohorsejobs.com')
AND NOT EXISTS (SELECT 1 FROM users WHERE identifier = 'lol');