mirror of
https://github.com/immich-app/immich.git
synced 2026-03-01 18:19:10 +03:00
chore: add "pnpm run migrations:revert" command (#23869)
This commit is contained in:
@@ -360,18 +360,7 @@ export class DatabaseRepository {
|
||||
async runMigrations(): Promise<void> {
|
||||
this.logger.debug('Running migrations');
|
||||
|
||||
const migrator = new Migrator({
|
||||
db: this.db,
|
||||
migrationLockTableName: 'kysely_migrations_lock',
|
||||
allowUnorderedMigrations: this.configRepository.isDev(),
|
||||
migrationTableName: 'kysely_migrations',
|
||||
provider: new FileMigrationProvider({
|
||||
fs: { readdir },
|
||||
path: { join },
|
||||
// eslint-disable-next-line unicorn/prefer-module
|
||||
migrationFolder: join(__dirname, '..', 'schema/migrations'),
|
||||
}),
|
||||
});
|
||||
const migrator = this.createMigrator();
|
||||
|
||||
const { error, results } = await migrator.migrateToLatest();
|
||||
|
||||
@@ -477,4 +466,50 @@ export class DatabaseRepository {
|
||||
private async releaseLock(lock: DatabaseLock, connection: Kysely<DB>): Promise<void> {
|
||||
await sql`SELECT pg_advisory_unlock(${lock})`.execute(connection);
|
||||
}
|
||||
|
||||
async revertLastMigration(): Promise<string | undefined> {
|
||||
this.logger.debug('Reverting last migration');
|
||||
|
||||
const migrator = this.createMigrator();
|
||||
const { error, results } = await migrator.migrateDown();
|
||||
|
||||
for (const result of results ?? []) {
|
||||
if (result.status === 'Success') {
|
||||
this.logger.log(`Reverted migration "${result.migrationName}"`);
|
||||
}
|
||||
|
||||
if (result.status === 'Error') {
|
||||
this.logger.warn(`Failed to revert migration "${result.migrationName}"`);
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
this.logger.error(`Failed to revert migrations: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
|
||||
const reverted = results?.find((result) => result.direction === 'Down' && result.status === 'Success');
|
||||
if (!reverted) {
|
||||
this.logger.debug('No migrations to revert');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
this.logger.debug('Finished reverting migration');
|
||||
return reverted.migrationName;
|
||||
}
|
||||
|
||||
private createMigrator(): Migrator {
|
||||
return new Migrator({
|
||||
db: this.db,
|
||||
migrationLockTableName: 'kysely_migrations_lock',
|
||||
allowUnorderedMigrations: this.configRepository.isDev(),
|
||||
migrationTableName: 'kysely_migrations',
|
||||
provider: new FileMigrationProvider({
|
||||
fs: { readdir },
|
||||
path: { join },
|
||||
// eslint-disable-next-line unicorn/prefer-module
|
||||
migrationFolder: join(__dirname, '..', 'schema/migrations'),
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user