"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaCheck = void 0;
const sql_tools_1 = require("@immich/sql-tools");
const nest_commander_1 = require("nest-commander");
const constants_1 = require("../constants");
const cli_service_1 = require("../services/cli.service");
let SchemaCheck = class SchemaCheck extends nest_commander_1.CommandRunner {
    service;
    constructor(service) {
        super();
        this.service = service;
    }
    async run() {
        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 on disk, but has not been applied to the database`);
                            break;
                        }
                        case 'applied': {
                            break;
                        }
                    }
                }
            }
            if (drift.items.length === 0) {
                console.log('\nNo schema drift detected');
            }
            else {
                console.log(`\n${constants_1.ErrorMessages.SchemaDrift}`);
                for (const item of drift.items) {
                    console.log(`  - ${item.type}: ` + (0, sql_tools_1.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');
        }
    }
};
exports.SchemaCheck = SchemaCheck;
exports.SchemaCheck = SchemaCheck = __decorate([
    (0, nest_commander_1.Command)({
        name: 'schema-check',
        description: 'Verify database migrations and check for schema drift',
    }),
    __metadata("design:paramtypes", [cli_service_1.CliService])
], SchemaCheck);
//# sourceMappingURL=schema-check.js.map