Improve domain UX and migration startup
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user