mirror of
https://github.com/immich-app/immich.git
synced 2026-03-01 01:59:06 +03:00
feat: schema-check (#25904)
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
import { DisableOAuthLogin, EnableOAuthLogin } from 'src/commands/oauth-login';
|
||||
import { DisablePasswordLoginCommand, EnablePasswordLoginCommand } from 'src/commands/password-login';
|
||||
import { PromptPasswordQuestions, ResetAdminPasswordCommand } from 'src/commands/reset-admin-password.command';
|
||||
import { SchemaCheck } from 'src/commands/schema-check';
|
||||
import { VersionCommand } from 'src/commands/version.command';
|
||||
|
||||
export const commandsAndQuestions = [
|
||||
@@ -28,4 +29,5 @@ export const commandsAndQuestions = [
|
||||
ChangeMediaLocationCommand,
|
||||
PromptMediaLocationQuestions,
|
||||
PromptConfirmMoveQuestions,
|
||||
SchemaCheck,
|
||||
];
|
||||
|
||||
60
server/src/commands/schema-check.ts
Normal file
60
server/src/commands/schema-check.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { Command, CommandRunner } from 'nest-commander';
|
||||
import { ErrorMessages } from 'src/constants';
|
||||
import { CliService } from 'src/services/cli.service';
|
||||
import { asHuman } from 'src/sql-tools/schema-diff';
|
||||
|
||||
@Command({
|
||||
name: 'schema-check',
|
||||
description: 'Verify database migrations and check for schema drift',
|
||||
})
|
||||
export class SchemaCheck extends CommandRunner {
|
||||
constructor(private service: CliService) {
|
||||
super();
|
||||
}
|
||||
|
||||
async run(): Promise<void> {
|
||||
try {
|
||||
const { migrations, drift } = await this.service.schemaReport();
|
||||
|
||||
if (migrations.every((item) => item.status === 'applied')) {
|
||||
console.log('Migrations are up to date');
|
||||
} else {
|
||||
console.log('Migration issues detected:');
|
||||
for (const migration of migrations) {
|
||||
switch (migration.status) {
|
||||
case 'deleted': {
|
||||
console.log(` - ${migration.name} was applied, but the file no longer exists on disk`);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'missing': {
|
||||
console.log(` - ${migration.name} exists, but has not been applied to the database`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (drift.items.length === 0) {
|
||||
console.log('\nNo schema drift detected');
|
||||
} else {
|
||||
console.log(`\n${ErrorMessages.SchemaDrift}`);
|
||||
for (const item of drift.items) {
|
||||
console.log(` - ${item.type}: ` + asHuman(item));
|
||||
}
|
||||
|
||||
console.log(`
|
||||
|
||||
The below SQL is automatically generated and may be helpful for resolving drift. ** Use at your own risk! **
|
||||
|
||||
\`\`\`sql
|
||||
${drift.asSql().join('\n')}
|
||||
\`\`\`
|
||||
`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
console.error('Unable to debug migrations');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user