Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add a "skip" for fresh db's #274

Merged
merged 4 commits into from
Jan 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions src/server/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,50 @@ export async function migrations() {
const logger = Logger.get('database::migrations');

try {
logger.debug('establishing database connection');
logger.info('establishing database connection');
const migrate = new Migrate('./prisma/schema.prisma');

logger.debug('ensuring database exists, if not creating database - may error if no permissions');
logger.info('ensuring database exists, if not creating database - may error if no permissions');
await ensureDatabaseExists('apply', './prisma/schema.prisma');

const diagnose = await migrate.diagnoseMigrationHistory({
optInToShadowDatabase: false,
});

if (diagnose.history?.diagnostic === 'databaseIsBehind') {
logger.debug('database is behind, attempting to migrate');
try {
logger.debug('migrating database');
await migrate.applyMigrations();
} finally {
if (!diagnose.hasMigrationsTable) {
logger.debug('no migrations table found, attempting schema push');
try {
logger.debug('pushing schema');
const migration = await migrate.push({ force: false });
if (migration.unexecutable && migration.unexecutable.length > 0)
throw new Error('This database is not empty, schema push is not possible.');
} catch (e) {
migrate.stop();
logger.error('failed to push schema');
throw e;
}
logger.debug('finished pushing schema, marking migrations as applied');
for (const migration of diagnose.history.unappliedMigrationNames) {
await migrate.markMigrationApplied({ migrationId: migration });
}
migrate.stop();
logger.info('finished migrating database');
} else if (diagnose.hasMigrationsTable) {
logger.debug('database is behind, attempting to migrate');
try {
logger.debug('migrating database');
await migrate.applyMigrations();
} catch (e) {
logger.error('failed to migrate database');
migrate.stop();
throw e;
}
migrate.stop();
logger.info('finished migrating database');
}
} else {
logger.debug('exiting migrations engine - database is up to date');
logger.info('exiting migrations engine - database is up to date');
migrate.stop();
}
} catch (error) {
Expand Down