···11+-- Migration: Add change_type and original_path to draft_content
22+-- This allows tracking different types of changes (edit, new_file, new_folder, rename)
33+-- This migration is idempotent and handles cases where columns might already exist
44+55+-- Create a temporary table with the new schema
66+CREATE TABLE IF NOT EXISTS draft_content_new (
77+ id INTEGER PRIMARY KEY AUTOINCREMENT,
88+ user_id INTEGER NOT NULL,
99+ repo_full_name TEXT NOT NULL,
1010+ file_path TEXT NOT NULL,
1111+ content TEXT NOT NULL,
1212+ change_type TEXT DEFAULT 'edit',
1313+ original_path TEXT,
1414+ last_saved_at DATETIME DEFAULT CURRENT_TIMESTAMP,
1515+ UNIQUE(user_id, repo_full_name, file_path),
1616+ FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
1717+);
1818+1919+-- Copy data from old table if it exists
2020+INSERT OR IGNORE INTO draft_content_new (id, user_id, repo_full_name, file_path, content, change_type, original_path, last_saved_at)
2121+SELECT id, user_id, repo_full_name, file_path, content,
2222+ COALESCE(change_type, 'edit') as change_type,
2323+ original_path,
2424+ last_saved_at
2525+FROM draft_content;
2626+2727+-- Drop old table
2828+DROP TABLE IF EXISTS draft_content;
2929+3030+-- Rename new table to old name
3131+ALTER TABLE draft_content_new RENAME TO draft_content;
3232+3333+-- Create index for efficient querying by change_type
3434+CREATE INDEX IF NOT EXISTS idx_draft_content_change_type ON draft_content(user_id, repo_full_name, change_type);
3535+3636+-- Create original index if it doesn't exist
3737+CREATE INDEX IF NOT EXISTS idx_draft_content_user_repo_file ON draft_content(user_id, repo_full_name, file_path);