Catch up to 3 JIT migrations

This commit is contained in:
Sam Becker 2025-03-30 20:08:49 -05:00
parent 3a8af98235
commit 09d7a07722

View File

@ -65,7 +65,8 @@ const createPhotosTable = () =>
) )
`; `;
// Wrapper for most queries for JIT table creation/migration running // Safe wrapper for most queries with JIT table creation/migration
// Catch up to 3 migrations in older installations
const safelyQueryPhotos = async <T>( const safelyQueryPhotos = async <T>(
callback: () => Promise<T>, callback: () => Promise<T>,
debugMessage: string, debugMessage: string,
@ -78,6 +79,7 @@ const safelyQueryPhotos = async <T>(
try { try {
result = await callback(); result = await callback();
} catch (e: any) { } catch (e: any) {
// Catch 1st migration
let migration = migrationForError(e); let migration = migrationForError(e);
if (migration) { if (migration) {
console.log(`Running Migration ${migration.label} ...`); console.log(`Running Migration ${migration.label} ...`);
@ -85,15 +87,26 @@ const safelyQueryPhotos = async <T>(
try { try {
result = await callback(); result = await callback();
} catch (e: any) { } catch (e: any) {
// Catch potential second migration, // Catch 2nd migration
// which otherwise would not be caught
migration = migrationForError(e); migration = migrationForError(e);
if (migration) { if (migration) {
console.log(`Running Migration ${migration.label} ...`); console.log(`Running Migration ${migration.label} ...`);
await migration.run(); await migration.run();
result = await callback(); result = await callback();
} else { } else {
throw e; try {
result = await callback();
} catch (e: any) {
// Catch 3rd migration
migration = migrationForError(e);
if (migration) {
console.log(`Running Migration ${migration.label} ...`);
await migration.run();
result = await callback();
} else {
throw e;
}
}
} }
} }
} else if (/relation "photos" does not exist/i.test(e.message)) { } else if (/relation "photos" does not exist/i.test(e.message)) {