Improve domain UX and migration startup

This commit is contained in:
ecki
2026-04-06 18:30:16 +02:00
parent 39f5f262fe
commit 130563d644
3 changed files with 246 additions and 55 deletions
+30
View File
@@ -1,6 +1,34 @@
const db = require("./db");
const migrations = require("./migrations");
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function waitForDatabase({
retries = 30,
delayMs = 2000,
} = {}) {
let lastError;
for (let attempt = 1; attempt <= retries; attempt++) {
try {
await db.query("SELECT 1");
return;
} catch (error) {
lastError = error;
console.log(`Database not ready yet (${attempt}/${retries})`);
if (attempt < retries) {
await sleep(delayMs);
}
}
}
throw lastError;
}
async function ensureMigrationsTable() {
await db.query(`
CREATE TABLE IF NOT EXISTS schema_migrations (
@@ -21,6 +49,7 @@ async function getAppliedMigrationIds() {
}
async function runMigrations() {
await waitForDatabase();
await ensureMigrationsTable();
const appliedIds = await getAppliedMigrationIds();
@@ -43,4 +72,5 @@ async function runMigrations() {
module.exports = {
runMigrations,
waitForDatabase,
};