diff --git a/server/src/shared/database/migrations.ts b/server/src/shared/database/migrations.ts index 80a35a26..e671af7d 100644 --- a/server/src/shared/database/migrations.ts +++ b/server/src/shared/database/migrations.ts @@ -2,28 +2,29 @@ import { Database } from "better-sqlite3"; import { APP_CONFIG_TABLE_SCHEMA_SQL, LAST_SCANNED_AT_SQL, SESSIONS_TABLE_SCHEMA_SQL, WORK_SPACE_PATH_SQL } from "@/shared/database/schema.js"; import { logger } from "@/shared/utils/logger.js"; -const addColumnToUsersTableIfNotExists = ( +const addColumnToTableIfNotExists = ( db: Database, + tableName: string, columnNames: string[], columnName: string, columnType: string, ) => { if (!columnNames.includes(columnName)) { logger.info( - `Running migration: Adding ${columnName} column to users table`, + `Running migration: Adding ${columnName} column to ${tableName} table`, ); - db.exec(`ALTER TABLE users ADD COLUMN ${columnName} ${columnType}`); + db.exec(`ALTER TABLE ${tableName} ADD COLUMN ${columnName} ${columnType}`); } }; export const runMigrations = (db: Database) => { try { - const tableInfo = db.prepare("PRAGMA table_info(users)").all() as { name: string }[]; - const columnNames = tableInfo.map((col) => col.name); + const usersTableInfo = db.prepare("PRAGMA table_info(users)").all() as { name: string }[]; + const userColumnNames = usersTableInfo.map((col) => col.name); - addColumnToUsersTableIfNotExists(db, columnNames, "git_name", "TEXT"); - addColumnToUsersTableIfNotExists(db, columnNames, "git_email", "TEXT"); - addColumnToUsersTableIfNotExists(db, columnNames, "has_completed_onboarding", "BOOLEAN DEFAULT 0", + addColumnToTableIfNotExists(db, "users", userColumnNames, "git_name", "TEXT"); + addColumnToTableIfNotExists(db, "users", userColumnNames, "git_email", "TEXT"); + addColumnToTableIfNotExists(db, "users", userColumnNames, "has_completed_onboarding", "BOOLEAN DEFAULT 0", ); // Create app_config table if it doesn't exist (for existing installations) @@ -36,6 +37,15 @@ export const runMigrations = (db: Database) => { ); db.exec(WORK_SPACE_PATH_SQL); + const workspaceOriginalPathsTableInfo = db.prepare("PRAGMA table_info(workspace_original_paths)").all() as { name: string }[]; + const workspaceOriginalPathsColumnNames = workspaceOriginalPathsTableInfo.map((col) => col.name); + addColumnToTableIfNotExists( + db, + "workspace_original_paths", + workspaceOriginalPathsColumnNames, + "custom_workspace_name", + "TEXT DEFAULT NULL", + ); db.exec(LAST_SCANNED_AT_SQL); diff --git a/server/src/shared/database/repositories/workspace-original-paths.db.ts b/server/src/shared/database/repositories/workspace-original-paths.db.ts index 7a25f090..8d0f3668 100644 --- a/server/src/shared/database/repositories/workspace-original-paths.db.ts +++ b/server/src/shared/database/repositories/workspace-original-paths.db.ts @@ -1,4 +1,5 @@ import { getConnection } from '@/shared/database/connection.js'; +import type { WorkspaceOriginalPathRow } from '@/shared/database/types.js'; export const workspaceOriginalPathsDb = { createWorkspacePath(workspacePath: string): void { @@ -9,4 +10,24 @@ export const workspaceOriginalPathsDb = { ON CONFLICT(workspace_path) DO NOTHING `).run(workspacePath); }, -} \ No newline at end of file + + getCustomWorkspaceName(workspacePath: string): string | null { + const db = getConnection(); + const row = db.prepare(` + SELECT custom_workspace_name + FROM workspace_original_paths + WHERE workspace_path = ? + `).get(workspacePath) as Pick | undefined; + + return row?.custom_workspace_name ?? null; + }, + + updateCustomWorkspaceName(workspacePath: string, customWorkspaceName: string | null): void { + const db = getConnection(); + db.prepare(` + INSERT INTO workspace_original_paths (workspace_path, custom_workspace_name) + VALUES (?, ?) + ON CONFLICT(workspace_path) DO UPDATE SET custom_workspace_name = excluded.custom_workspace_name + `).run(workspacePath, customWorkspaceName); + }, +} diff --git a/server/src/shared/database/schema.ts b/server/src/shared/database/schema.ts index 9e9975c2..0d4834cb 100644 --- a/server/src/shared/database/schema.ts +++ b/server/src/shared/database/schema.ts @@ -53,7 +53,8 @@ CREATE TABLE IF NOT EXISTS sessions ( export const WORK_SPACE_PATH_SQL = ` CREATE TABLE IF NOT EXISTS workspace_original_paths ( - workspace_path TEXT PRIMARY KEY NOT NULL + workspace_path TEXT PRIMARY KEY NOT NULL, + custom_workspace_name TEXT DEFAULT NULL ); ` diff --git a/server/src/shared/database/types.ts b/server/src/shared/database/types.ts index 1881ffc2..2bc390d6 100644 --- a/server/src/shared/database/types.ts +++ b/server/src/shared/database/types.ts @@ -116,6 +116,11 @@ export type SessionWithSummary = { [key: string]: unknown; }; +export type WorkspaceOriginalPathRow = { + workspace_path: string; + custom_workspace_name: string | null; +}; + // ---------------------------------------------------------------------------