fix: ensure alerts table columns exist
- Add idempotent migration to ensure severity, notification_sent, etc. columns exist - Fixes production issue where migration was marked as run but columns weren't created - Uses IF NOT EXISTS to safely add missing columns without errors - Includes all columns and indexes from ExtendAlertsForChecks migration
This commit is contained in:
parent
ab4948467a
commit
ed4fce49a4
1 changed files with 70 additions and 0 deletions
|
|
@ -0,0 +1,70 @@
|
|||
defmodule Towerops.Repo.Migrations.EnsureAlertsColumnsExist do
|
||||
use Ecto.Migration
|
||||
|
||||
def up do
|
||||
# Ensure columns from ExtendAlertsForChecks migration exist
|
||||
# This fixes cases where the migration was marked as run but columns weren't created
|
||||
execute """
|
||||
ALTER TABLE alerts
|
||||
ADD COLUMN IF NOT EXISTS severity INTEGER DEFAULT 2
|
||||
"""
|
||||
|
||||
execute """
|
||||
ALTER TABLE alerts
|
||||
ADD COLUMN IF NOT EXISTS notification_sent BOOLEAN DEFAULT false
|
||||
"""
|
||||
|
||||
execute """
|
||||
ALTER TABLE alerts
|
||||
ADD COLUMN IF NOT EXISTS notification_sent_at TIMESTAMP
|
||||
"""
|
||||
|
||||
execute """
|
||||
ALTER TABLE alerts
|
||||
ADD COLUMN IF NOT EXISTS output TEXT
|
||||
"""
|
||||
|
||||
execute """
|
||||
ALTER TABLE alerts
|
||||
ADD COLUMN IF NOT EXISTS check_id UUID
|
||||
"""
|
||||
|
||||
# Create indexes if they don't exist
|
||||
execute """
|
||||
CREATE INDEX IF NOT EXISTS alerts_check_id_index ON alerts (check_id)
|
||||
"""
|
||||
|
||||
execute """
|
||||
CREATE INDEX IF NOT EXISTS alerts_severity_index ON alerts (severity)
|
||||
"""
|
||||
|
||||
execute """
|
||||
CREATE INDEX IF NOT EXISTS alerts_notification_sent_index ON alerts (notification_sent)
|
||||
"""
|
||||
|
||||
# Add foreign key constraint if it doesn't exist
|
||||
execute """
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_constraint WHERE conname = 'alerts_check_id_fkey'
|
||||
) THEN
|
||||
ALTER TABLE alerts
|
||||
ADD CONSTRAINT alerts_check_id_fkey
|
||||
FOREIGN KEY (check_id) REFERENCES checks(id) ON DELETE CASCADE;
|
||||
END IF;
|
||||
END $$;
|
||||
"""
|
||||
|
||||
# Make device_id nullable if it isn't already
|
||||
execute """
|
||||
ALTER TABLE alerts
|
||||
ALTER COLUMN device_id DROP NOT NULL
|
||||
"""
|
||||
end
|
||||
|
||||
def down do
|
||||
# Don't remove columns on rollback - data safety
|
||||
:ok
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue