Skip to content

Commit fda1973

Browse files
Jing-yilinclaude
andcommitted
fix: add missing velocity_24h and ple_delta_24h columns to campaigns table
The model includes Velocity24h and PleDelta24h fields but the database was missing these columns, causing SQLSTATE 42703 errors when querying campaigns. Added idempotent migration to ensure both columns exist with proper defaults (0) when the backend starts. Related to deployment aa9ad5e - columns were added to model but not migrated to dev database. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent aa9ad5e commit fda1973

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

‎backend/internal/db/db.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,31 @@ func Init(cfg *config.Config) error {
9494
return fmt.Errorf("migrate campaigns pk: %w", err)
9595
}
9696

97+
// Ensure velocity_24h and pledge_delta_24h columns exist (added in develop branch)
98+
if err := DB.Exec(`
99+
DO $$
100+
BEGIN
101+
-- Add velocity_24h column if it doesn't exist
102+
IF NOT EXISTS (
103+
SELECT 1 FROM information_schema.columns
104+
WHERE table_name = 'campaigns' AND column_name = 'velocity_24h'
105+
) THEN
106+
ALTER TABLE campaigns ADD COLUMN velocity_24h DOUBLE PRECISION DEFAULT 0;
107+
END IF;
108+
109+
-- Add pledge_delta_24h column if it doesn't exist (named ple_delta_24h in DB due to GORM naming)
110+
IF NOT EXISTS (
111+
SELECT 1 FROM information_schema.columns
112+
WHERE table_name = 'campaigns' AND column_name = 'ple_delta_24h'
113+
) THEN
114+
ALTER TABLE campaigns ADD COLUMN ple_delta_24h DOUBLE PRECISION DEFAULT 0;
115+
END IF;
116+
END
117+
$$;
118+
`).Error; err != nil {
119+
return fmt.Errorf("migrate velocity columns: %w", err)
120+
}
121+
97122
log.Println("Database connected and migrated")
98123
return nil
99124
}

0 commit comments

Comments
 (0)