refactor: restructure db logic and add import alias using tsc-alias

Note: the legacy githubTokensDb migration is not included in this commit.
It's used only in `agents.js` and will be removed in a future commit. We
will directly use credentials repository instead of github tokens repository.
This commit is contained in:
Haileyesus
2026-03-13 17:00:26 +03:00
parent e67738c9fc
commit 695da128f3
21 changed files with 1144 additions and 15 deletions

View File

@@ -0,0 +1,44 @@
import { Database } from "better-sqlite3";
import { APP_CONFIG_TABLE_SCHEMA_SQL, SESSION_NAMES_TABLE_SCHEMA_SQL } from "@/shared/database/schema.js";
import { logger } from "@/shared/utils/logger.js";
const addColumnToUsersTableIfNotExists = (
db: Database,
columnNames: string[],
columnName: string,
columnType: string,
) => {
if (!columnNames.includes(columnName)) {
logger.info(
`Running migration: Adding ${columnName} column to users table`,
);
db.exec(`ALTER TABLE users 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);
addColumnToUsersTableIfNotExists(db, columnNames, "git_name", "TEXT");
addColumnToUsersTableIfNotExists(db, columnNames, "git_email", "TEXT");
addColumnToUsersTableIfNotExists(db, columnNames, "has_completed_onboarding", "BOOLEAN DEFAULT 0",
);
// Create app_config table if it doesn't exist (for existing installations)
db.exec(APP_CONFIG_TABLE_SCHEMA_SQL);
// Create session_names table if it doesn't exist (for existing installations)
db.exec(SESSION_NAMES_TABLE_SCHEMA_SQL);
db.exec(
"CREATE INDEX IF NOT EXISTS idx_session_names_lookup ON session_names(session_id, provider)",
);
logger.info("Database migrations completed successfully");
} catch (error: any) {
logger.error("Error running migrations: ", error.message);
throw error;
}
};