-- Migration: 005_property_schema_version
-- Description: Track property schema version for automated migrations

BEGIN;

-- Add property schema version to revisions
-- This tracks which version of the property schema was used when the revision was created
ALTER TABLE revisions ADD COLUMN IF NOT EXISTS property_schema_version INTEGER DEFAULT 1;

-- Create index for finding revisions that need migration
CREATE INDEX IF NOT EXISTS idx_revisions_property_schema_version ON revisions(property_schema_version);

-- Create property migration history table
-- Tracks when property schema migrations have been run
CREATE TABLE IF NOT EXISTS property_migrations (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    schema_name TEXT NOT NULL,
    from_version INTEGER NOT NULL,
    to_version INTEGER NOT NULL,
    items_affected INTEGER NOT NULL DEFAULT 0,
    started_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    completed_at TIMESTAMPTZ,
    status TEXT NOT NULL DEFAULT 'pending',  -- pending, running, completed, failed
    error_message TEXT
);

CREATE INDEX IF NOT EXISTS idx_property_migrations_schema ON property_migrations(schema_name);
CREATE INDEX IF NOT EXISTS idx_property_migrations_status ON property_migrations(status);

COMMIT;
