feat: migration commands (#828)

* feat: migration commands

* Update packages/server/src/modules/CLI/commands/TenantsMigrateRollback.command.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/server/src/modules/CLI/commands/TenantsMigrateLatest.command.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/server/src/modules/CLI/commands/TenantsList.command.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/server/src/modules/CLI/commands/SystemMigrateRollback.command.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/server/src/modules/CLI/commands/TenantsMigrateLatest.command.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Ahmed Bouhuolia
2025-10-22 21:58:02 +02:00
committed by GitHub
parent 9d714ac78e
commit 3bd0e89146
17 changed files with 608 additions and 21 deletions

View File

@@ -0,0 +1,83 @@
import { CommandRunner } from 'nest-commander';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import Knex from 'knex';
import { knexSnakeCaseMappers } from 'objection';
@Injectable()
export abstract class BaseCommand extends CommandRunner {
constructor(protected readonly configService: ConfigService) {
super();
}
protected initSystemKnex(): any {
return Knex({
client: this.configService.get('systemDatabase.client'),
connection: {
host: this.configService.get('systemDatabase.host'),
user: this.configService.get('systemDatabase.user'),
password: this.configService.get('systemDatabase.password'),
database: this.configService.get('systemDatabase.databaseName'),
charset: 'utf8',
},
migrations: {
directory: this.configService.get('systemDatabase.migrationDir'),
},
seeds: {
directory: this.configService.get('systemDatabase.seedsDir'),
},
pool: { min: 0, max: 7 },
...knexSnakeCaseMappers({ upperCase: true }),
});
}
protected initTenantKnex(organizationId: string = ''): any {
return Knex({
client: this.configService.get('tenantDatabase.client'),
connection: {
host: this.configService.get('tenantDatabase.host'),
user: this.configService.get('tenantDatabase.user'),
password: this.configService.get('tenantDatabase.password'),
database: `${this.configService.get('tenantDatabase.dbNamePrefix')}${organizationId}`,
charset: 'utf8',
},
migrations: {
directory: this.configService.get('tenantDatabase.migrationsDir') || './src/database/migrations',
},
seeds: {
directory: this.configService.get('tenantDatabase.seedsDir') || './src/database/seeds/core',
},
pool: {
min: 0,
max: 5,
},
...knexSnakeCaseMappers({ upperCase: true }),
});
}
protected getAllSystemTenants(knex: any) {
return knex('tenants');
}
protected getAllInitializedTenants(knex: any) {
return knex('tenants').whereNotNull('initializedAt');
}
protected exit(text: any): never {
if (text instanceof Error) {
console.error(`Error: ${text.message}\n${text.stack}`);
} else {
console.error(`Error: ${text}`);
}
process.exit(1);
}
protected success(text: string): never {
console.log(text);
process.exit(0);
}
protected log(text: string): void {
console.log(text);
}
}