Branchwell

AI Resource Pro / website / MIGRATION_INSTRUCTIONS.md

Show the source

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:

  1. Adds the is_archived column with a default value of false to the user_roadmaps table
  2. 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
  3. Creates a partial unique index on user_id where is_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:

  1. Connect to the production database through Supabase dashboard or direct connection
  2. Execute the SQL commands from the migration script
  3. 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:

  1. Verify a user can create a roadmap
  2. Verify a user can't have two active roadmaps at the same time
  3. Verify a user can archive an existing roadmap and create a new one
  4. 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.