Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | 257x 257x 257x 257x 2028x 2028x 2017x 2017x 2017x 2017x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2028x 11x 2017x 2016x 2016x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* One-time migration from config.json to SQLite.
*/
import { rmSync } from "node:fs";
import { createRequire } from "node:module";
import { join } from "node:path";
const _require = createRequire(import.meta.url);
import { logger } from "../logger.js";
import { getConfigDir } from "./index.js";
import type { Database } from "./sqlite.js";
const log = logger.withTag("migration");
const OLD_CONFIG_FILENAME = "config.json";
const MIGRATION_COMPLETED_KEY = "json_migration_completed";
/**
* Check if migration was already completed by looking at SQLite metadata.
* This is the authoritative source - not the presence of config.json.
*/
function isMigrationCompleted(db: Database): boolean {
const row = db
.query("SELECT value FROM metadata WHERE key = ?")
.get(MIGRATION_COMPLETED_KEY) as { value: string } | null;
return row?.value === "true";
}
/** Mark migration as completed in SQLite metadata. */
function markMigrationCompleted(db: Database): void {
db.query("INSERT OR REPLACE INTO metadata (key, value) VALUES (?, ?)").run(
MIGRATION_COMPLETED_KEY,
"true"
);
}
function oldConfigExists(): boolean {
const configPath = join(getConfigDir(), OLD_CONFIG_FILENAME);
const { existsSync } = _require("node:fs");
return existsSync(configPath);
}
function readOldConfig(): OldConfig | null {
const configPath = join(getConfigDir(), OLD_CONFIG_FILENAME);
try {
const { readFileSync } = _require("node:fs");
const content = readFileSync(configPath, "utf-8");
return JSON.parse(content);
} catch {
return null;
}
}
/**
* Attempt to delete the old config.json file.
* Returns true if deletion succeeded (or file was already gone), false otherwise.
*/
function deleteOldConfig(): boolean {
const configPath = join(getConfigDir(), OLD_CONFIG_FILENAME);
try {
rmSync(configPath);
return true;
} catch (error) {
// ENOENT means file already deleted - that's fine
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
return true;
}
// Other errors (permissions, locked file) - deletion failed
log.warn("Could not delete old config.json:", error);
return false;
}
}
type OldConfig = {
auth?: {
token?: string;
refreshToken?: string;
expiresAt?: number;
issuedAt?: number;
};
defaults?: {
organization?: string;
project?: string;
};
projectCache?: Record<
string,
{
orgSlug: string;
orgName: string;
projectSlug: string;
projectName: string;
cachedAt: number;
}
>;
dsnCache?: Record<
string,
{
dsn: string;
projectId: string;
orgId?: string;
source: string;
sourcePath?: string;
resolved?: {
orgSlug: string;
orgName: string;
projectSlug: string;
projectName: string;
};
cachedAt: number;
}
>;
projectAliases?: {
aliases: Record<string, { orgSlug: string; projectSlug: string }>;
cachedAt: number;
dsnFingerprint?: string;
};
};
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: one-time migration
export function migrateFromJson(db: Database): void {
// Check SQLite metadata first - this is the authoritative source
if (isMigrationCompleted(db)) {
return;
}
// No old config file means nothing to migrate
if (!oldConfigExists()) {
// Mark as completed so we don't check every time
markMigrationCompleted(db);
return;
}
const oldConfig = readOldConfig();
Iif (!oldConfig) {
// Config exists but couldn't be read (corrupted?) - mark complete to avoid retry loops
markMigrationCompleted(db);
return;
}
log.info("Migrating config to SQLite...");
db.exec("BEGIN TRANSACTION");
try {
Eif (oldConfig.auth?.token) {
// Direct write (not via setAuthToken) — safe only because migration
// runs during DB bootstrap, before getIdentityFingerprint() memoizes.
db.query(`
INSERT OR REPLACE INTO auth (id, token, refresh_token, expires_at, issued_at, updated_at)
VALUES (1, ?, ?, ?, ?, ?)
`).run(
oldConfig.auth.token,
oldConfig.auth.refreshToken ?? null,
oldConfig.auth.expiresAt ?? null,
oldConfig.auth.issuedAt ?? null,
Date.now()
);
}
Eif (oldConfig.defaults?.organization) {
db.query(
"INSERT OR REPLACE INTO metadata (key, value) VALUES (?, ?)"
).run("defaults.org", oldConfig.defaults.organization);
}
Eif (oldConfig.defaults?.project) {
db.query(
"INSERT OR REPLACE INTO metadata (key, value) VALUES (?, ?)"
).run("defaults.project", oldConfig.defaults.project);
}
Iif (oldConfig.projectCache) {
const insertStmt = db.query(`
INSERT OR REPLACE INTO project_cache
(cache_key, org_slug, org_name, project_slug, project_name, cached_at, last_accessed)
VALUES (?, ?, ?, ?, ?, ?, ?)
`);
for (const [key, entry] of Object.entries(oldConfig.projectCache)) {
// Skip malformed entries missing required fields
if (
!(
entry?.orgSlug &&
entry?.orgName &&
entry?.projectSlug &&
entry?.projectName
) ||
entry?.cachedAt === null ||
entry?.cachedAt === undefined
) {
continue;
}
insertStmt.run(
key,
entry.orgSlug,
entry.orgName,
entry.projectSlug,
entry.projectName,
entry.cachedAt,
entry.cachedAt // last_accessed = cachedAt for migrated entries
);
}
}
Iif (oldConfig.dsnCache) {
const insertStmt = db.query(`
INSERT OR REPLACE INTO dsn_cache
(directory, dsn, project_id, org_id, source, source_path,
resolved_org_slug, resolved_org_name, resolved_project_slug, resolved_project_name,
cached_at, last_accessed)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
for (const [directory, entry] of Object.entries(oldConfig.dsnCache)) {
// Skip malformed entries missing required fields
if (
!(entry?.dsn && entry?.projectId && entry?.source) ||
entry?.cachedAt === null ||
entry?.cachedAt === undefined
) {
continue;
}
insertStmt.run(
directory,
entry.dsn,
entry.projectId,
entry.orgId ?? null,
entry.source,
entry.sourcePath ?? null,
entry.resolved?.orgSlug ?? null,
entry.resolved?.orgName ?? null,
entry.resolved?.projectSlug ?? null,
entry.resolved?.projectName ?? null,
entry.cachedAt,
entry.cachedAt
);
}
}
Iif (oldConfig.projectAliases?.aliases) {
const insertStmt = db.query(`
INSERT OR REPLACE INTO project_aliases
(alias, org_slug, project_slug, dsn_fingerprint, cached_at, last_accessed)
VALUES (?, ?, ?, ?, ?, ?)
`);
const fingerprint = oldConfig.projectAliases.dsnFingerprint ?? null;
const cachedAt = oldConfig.projectAliases.cachedAt;
// Skip all aliases if cachedAt is missing (required for NOT NULL column)
if (cachedAt !== null && cachedAt !== undefined) {
for (const [alias, entry] of Object.entries(
oldConfig.projectAliases.aliases
)) {
// Skip malformed entries missing required fields
if (!(entry?.orgSlug && entry?.projectSlug)) {
continue;
}
insertStmt.run(
alias.toLowerCase(),
entry.orgSlug,
entry.projectSlug,
fingerprint,
cachedAt,
cachedAt
);
}
}
}
// Mark migration as completed in SQLite BEFORE attempting file deletion.
// This ensures we never re-run migration even if file deletion fails.
markMigrationCompleted(db);
db.exec("COMMIT");
// Best-effort cleanup of old file - if it fails, we're still safe
// because SQLite metadata is the authoritative source
deleteOldConfig();
log.success("Migration complete.");
} catch (error) {
db.exec("ROLLBACK");
log.error("Migration failed, keeping config.json:", error);
throw error;
}
}
|