AI Resource Pro / website / MIGRATION_INSTRUCTIONS.md
Migration Instructions: Adding is_archived Column
This document provides instructions for migrating the database to add the is_archived column to the user_roadmaps table and create a partial unique index to ensure users can only have one active roadmap at a time.
Background
We've updated the codebase to enforce a "one active roadmap per user" rule. This requires adding an is_archived column to the user_roadmaps table and creating a partial unique index on the user_id column where is_archived = false.
Migration Script
The migration script is located at scripts/add-is-archived-column.sql. It:
- Adds the
is_archivedcolumn with a default value offalseto theuser_roadmapstable - Automatically cleans up existing duplicate roadmaps by:
- Finding all users who have multiple active roadmaps
- For each user, keeping only their most recent roadmap active
- Archiving all older roadmaps for those users
- Creates a partial unique index on
user_idwhereis_archived = false
Data Impact
The migration will archive older duplicate roadmaps for any users who currently have multiple roadmaps. Users will only see their most recent roadmap after the migration.
Applying the Migration
Development Environment
If using Supabase CLI:
npx supabase db push scripts/add-is-archived-column.sql
If connecting directly with psql:
psql YOUR_SUPABASE_DATABASE_URL -f scripts/add-is-archived-column.sql
Production Environment
For the production environment, follow the appropriate procedure for your deployment:
- Connect to the production database through Supabase dashboard or direct connection
- Execute the SQL commands from the migration script
- Verify the migration was successful with:
-- Check if column exists
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'user_roadmaps' AND column_name = 'is_archived';
-- Check if index exists
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = 'user_roadmaps' AND indexname = 'idx_user_roadmaps_one_active_per_user';
-- Check that each user now has only one active roadmap
SELECT user_id, COUNT(*)
FROM public.user_roadmaps
WHERE is_archived = false
GROUP BY user_id
HAVING COUNT(*) > 1;
The last query should return no rows, confirming that each user has at most one active roadmap.
Testing After Migration
After applying the migration:
- Verify a user can create a roadmap
- Verify a user can't have two active roadmaps at the same time
- Verify a user can archive an existing roadmap and create a new one
- Verify roadmap fetching only returns non-archived roadmaps
Rollback Procedure
If needed, you can roll back this migration with:
-- Drop the index
DROP INDEX IF EXISTS idx_user_roadmaps_one_active_per_user;
-- Remove the column (if safe to do so)
ALTER TABLE public.user_roadmaps DROP COLUMN IF EXISTS is_archived;
Note: Only roll back if there are no dependencies on the new column in the running application code.