-- Safe Migration: Only add missing columns and indexes -- Works with existing schema, won't break on re-run -- Add missing columns to posts table if they don't exist DO $$ BEGIN -- Add location column if missing (for geo posts) IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'posts' AND column_name = 'location') THEN ALTER TABLE posts ADD COLUMN location GEOGRAPHY(POINT, 4326); END IF; -- Add target_city if missing IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'posts' AND column_name = 'target_city') THEN ALTER TABLE posts ADD COLUMN target_city VARCHAR(255); END IF; -- Add target_state if missing IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'posts' AND column_name = 'target_state') THEN ALTER TABLE posts ADD COLUMN target_state VARCHAR(50); END IF; -- Add generation_data if missing IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'posts' AND column_name = 'generation_data') THEN ALTER TABLE posts ADD COLUMN generation_data JSONB DEFAULT '{}'; END IF; END $$; -- Create indexes only if they don't exist CREATE INDEX IF NOT EXISTS idx_posts_location ON posts USING GIST (location); CREATE INDEX IF NOT EXISTS idx_posts_target_city ON posts (target_city); -- Success message DO $$ BEGIN RAISE NOTICE '✅ Safe migration completed - existing data preserved'; END $$;