-- Upgrade falls bereits 001 mit alten Spaltennamen eingespielt wurde.
-- Neuinstallationen nutzen 001 direkt und brauchen 002 nicht.

DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_schema = 'falukant_data' AND table_name = 'relationship_state'
          AND column_name = 'consecutive_underpayment_months'
    ) THEN
        ALTER TABLE falukant_data.relationship_state
            RENAME COLUMN consecutive_underpayment_months TO months_underfunded;
    END IF;
END $$;

ALTER TABLE falukant_data.relationship_state
    ADD COLUMN IF NOT EXISTS last_daily_processed_at timestamptz,
    ADD COLUMN IF NOT EXISTS last_monthly_processed_at timestamptz;
