|
| 1 | +/** |
| 2 | + * Local-to-Supabase Migration |
| 3 | + * |
| 4 | + * Migrates existing free-tier local .gitmem/ data to Supabase when |
| 5 | + * a user upgrades to Pro. Called during `activate` after schema is |
| 6 | + * verified and credentials are saved. |
| 7 | + * |
| 8 | + * Collections migrated: |
| 9 | + * - learnings (scars, wins, patterns, anti-patterns) |
| 10 | + * - sessions |
| 11 | + * - decisions |
| 12 | + * - scar_usage |
| 13 | + * |
| 14 | + * Threads are NOT migrated — they remain local (thread lifecycle is |
| 15 | + * tied to .gitmem/threads.json and managed by session_start). |
| 16 | + * |
| 17 | + * Migration is idempotent: uses Supabase upsert (merge-duplicates) |
| 18 | + * so re-running is safe. Existing Supabase records with same ID are |
| 19 | + * updated, not duplicated. |
| 20 | + */ |
| 21 | + |
| 22 | +import * as fs from "fs"; |
| 23 | +import * as path from "path"; |
| 24 | +import { getGitmemDir } from "../services/gitmem-dir.js"; |
| 25 | + |
| 26 | +/** Collections that map to Supabase tables */ |
| 27 | +const MIGRATABLE_COLLECTIONS = ["learnings", "sessions", "decisions", "scar_usage"] as const; |
| 28 | + |
| 29 | +/** Fields that should NOT be sent to Supabase (local-only or computed) */ |
| 30 | +const STRIP_FIELDS = new Set(["is_starter"]); |
| 31 | + |
| 32 | +/** Fields that Supabase will reject if null (remove instead of sending null) */ |
| 33 | +const NULLABLE_STRIP = new Set(["embedding"]); |
| 34 | + |
| 35 | +export interface MigrationResult { |
| 36 | + migrated: Record<string, number>; |
| 37 | + skipped: Record<string, number>; |
| 38 | + errors: Record<string, string[]>; |
| 39 | + total: number; |
| 40 | + hasLocalData: boolean; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Check if there is local data worth migrating |
| 45 | + */ |
| 46 | +export function hasLocalData(gitmemDir?: string): boolean { |
| 47 | + const dir = gitmemDir || getGitmemDir(); |
| 48 | + for (const collection of MIGRATABLE_COLLECTIONS) { |
| 49 | + const filePath = path.join(dir, `${collection}.json`); |
| 50 | + if (fs.existsSync(filePath)) { |
| 51 | + try { |
| 52 | + const data = JSON.parse(fs.readFileSync(filePath, "utf-8")); |
| 53 | + if (Array.isArray(data) && data.length > 0) return true; |
| 54 | + } catch { |
| 55 | + // Corrupt file — skip |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return false; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Read a local collection JSON file |
| 64 | + */ |
| 65 | +function readLocalCollection(dir: string, collection: string): Record<string, unknown>[] { |
| 66 | + const filePath = path.join(dir, `${collection}.json`); |
| 67 | + if (!fs.existsSync(filePath)) return []; |
| 68 | + try { |
| 69 | + const data = JSON.parse(fs.readFileSync(filePath, "utf-8")); |
| 70 | + return Array.isArray(data) ? data : []; |
| 71 | + } catch { |
| 72 | + return []; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Clean a record for Supabase insertion: |
| 78 | + * - Strip local-only fields |
| 79 | + * - Remove null values for non-nullable columns |
| 80 | + * - Ensure id exists |
| 81 | + */ |
| 82 | +function cleanRecord(record: Record<string, unknown>): Record<string, unknown> | null { |
| 83 | + if (!record.id) return null; |
| 84 | + |
| 85 | + const cleaned: Record<string, unknown> = {}; |
| 86 | + for (const [key, value] of Object.entries(record)) { |
| 87 | + if (STRIP_FIELDS.has(key)) continue; |
| 88 | + if (NULLABLE_STRIP.has(key) && (value === null || value === undefined)) continue; |
| 89 | + cleaned[key] = value; |
| 90 | + } |
| 91 | + return cleaned; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Migrate local .gitmem data to Supabase |
| 96 | + * |
| 97 | + * @param supabaseUrl - User's Supabase project URL |
| 98 | + * @param supabaseKey - User's service role key |
| 99 | + * @param tablePrefix - Table prefix (default: "gitmem_") |
| 100 | + * @param gitmemDir - Override .gitmem directory path |
| 101 | + * @param onProgress - Callback for progress reporting |
| 102 | + */ |
| 103 | +export async function migrateLocalToSupabase(opts: { |
| 104 | + supabaseUrl: string; |
| 105 | + supabaseKey: string; |
| 106 | + tablePrefix?: string; |
| 107 | + gitmemDir?: string; |
| 108 | + onProgress?: (msg: string) => void; |
| 109 | +}): Promise<MigrationResult> { |
| 110 | + const { supabaseUrl, supabaseKey, tablePrefix = "gitmem_", onProgress } = opts; |
| 111 | + const dir = opts.gitmemDir || getGitmemDir(); |
| 112 | + const log = onProgress || ((msg: string) => console.log(msg)); |
| 113 | + |
| 114 | + const result: MigrationResult = { |
| 115 | + migrated: {}, |
| 116 | + skipped: {}, |
| 117 | + errors: {}, |
| 118 | + total: 0, |
| 119 | + hasLocalData: false, |
| 120 | + }; |
| 121 | + |
| 122 | + const restUrl = `${supabaseUrl}/rest/v1`; |
| 123 | + |
| 124 | + for (const collection of MIGRATABLE_COLLECTIONS) { |
| 125 | + const records = readLocalCollection(dir, collection); |
| 126 | + const tableName = `${tablePrefix}${collection}`; |
| 127 | + |
| 128 | + result.migrated[collection] = 0; |
| 129 | + result.skipped[collection] = 0; |
| 130 | + result.errors[collection] = []; |
| 131 | + |
| 132 | + if (records.length === 0) continue; |
| 133 | + result.hasLocalData = true; |
| 134 | + |
| 135 | + log(` Migrating ${records.length} ${collection}...`); |
| 136 | + |
| 137 | + for (const record of records) { |
| 138 | + const cleaned = cleanRecord(record); |
| 139 | + if (!cleaned) { |
| 140 | + result.skipped[collection]++; |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + const response = await fetch(`${restUrl}/${tableName}`, { |
| 146 | + method: "POST", |
| 147 | + headers: { |
| 148 | + "apikey": supabaseKey, |
| 149 | + "Authorization": `Bearer ${supabaseKey}`, |
| 150 | + "Content-Type": "application/json", |
| 151 | + "Prefer": "return=minimal,resolution=merge-duplicates", |
| 152 | + "Content-Profile": "public", |
| 153 | + }, |
| 154 | + body: JSON.stringify(cleaned), |
| 155 | + signal: AbortSignal.timeout(10_000), |
| 156 | + }); |
| 157 | + |
| 158 | + if (response.ok) { |
| 159 | + result.migrated[collection]++; |
| 160 | + } else { |
| 161 | + const text = await response.text(); |
| 162 | + // Only log first 3 errors per collection to avoid spam |
| 163 | + if (result.errors[collection].length < 3) { |
| 164 | + result.errors[collection].push( |
| 165 | + `${String(cleaned.id).substring(0, 8)}: ${response.status} - ${text.substring(0, 100)}` |
| 166 | + ); |
| 167 | + } |
| 168 | + result.skipped[collection]++; |
| 169 | + } |
| 170 | + } catch (err) { |
| 171 | + if (result.errors[collection].length < 3) { |
| 172 | + result.errors[collection].push( |
| 173 | + `${String(cleaned.id).substring(0, 8)}: ${err instanceof Error ? err.message : "Unknown error"}` |
| 174 | + ); |
| 175 | + } |
| 176 | + result.skipped[collection]++; |
| 177 | + } |
| 178 | + |
| 179 | + result.total++; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return result; |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * Rename local collection files after successful migration |
| 188 | + * Adds .pre-migration suffix so data isn't lost but won't be re-read by free tier |
| 189 | + */ |
| 190 | +export function archiveLocalData(gitmemDir?: string): string[] { |
| 191 | + const dir = gitmemDir || getGitmemDir(); |
| 192 | + const archived: string[] = []; |
| 193 | + |
| 194 | + for (const collection of MIGRATABLE_COLLECTIONS) { |
| 195 | + const filePath = path.join(dir, `${collection}.json`); |
| 196 | + if (fs.existsSync(filePath)) { |
| 197 | + const archivePath = `${filePath}.pre-migration`; |
| 198 | + // Don't overwrite existing archive |
| 199 | + if (!fs.existsSync(archivePath)) { |
| 200 | + fs.renameSync(filePath, archivePath); |
| 201 | + archived.push(collection); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return archived; |
| 207 | +} |
0 commit comments