From 4708839fd6a0ed29af464a899951245bc0ea37da Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 5 Sep 2023 16:04:23 -0500 Subject: [PATCH] db: improve output of migrations, exit on failure --- src/db.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/db.ts b/src/db.ts index 2deca9a..a5f52af 100644 --- a/src/db.ts +++ b/src/db.ts @@ -63,8 +63,26 @@ const migrator = new Migrator({ }), }); -console.log('Running migrations...'); -const results = await migrator.migrateToLatest(); -console.log('Migrations finished:', results); +/** Migrate the database to the latest version. */ +async function migrate() { + console.log('Running migrations...'); + const results = await migrator.migrateToLatest(); + + if (results.error) { + console.error(results.error); + Deno.exit(1); + } else { + if (!results.results?.length) { + console.log('Everything up-to-date.'); + } else { + console.log('Migrations finished!'); + for (const { migrationName, status } of results.results) { + console.log(` - ${migrationName}: ${status}`); + } + } + } +} + +await migrate(); export { db, type DittoDB, type EventRow, type TagRow, type UserRow };