@nestjs-mod/pg-flyway
Version:
PgFlyway - utility for working with database migrations (site: https://www.npmjs.com/package/pg-flyway, preview version only for Postgres)
150 lines (148 loc) • 8.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PgFlywayInfrastructureUpdaterService = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs-mod/common");
const common_2 = require("@nestjs/common");
const case_anything_1 = require("case-anything");
const fs_1 = require("fs");
const path_1 = require("path");
const pg_flyway_configuration_1 = require("../pg-flyway.configuration");
const pg_flyway_infrastructure_constants_1 = require("./pg-flyway-infrastructure.constants");
let PgFlywayInfrastructureUpdaterService = class PgFlywayInfrastructureUpdaterService {
constructor(packageJsonService, nxProjectJsonService, pgFlywayConfiguration, wrapApplicationOptionsService) {
this.packageJsonService = packageJsonService;
this.nxProjectJsonService = nxProjectJsonService;
this.pgFlywayConfiguration = pgFlywayConfiguration;
this.wrapApplicationOptionsService = wrapApplicationOptionsService;
}
onModuleInit() {
this.update();
}
update() {
this.updatePackageJsonFile();
this.updateProjectJsonFile();
this.createFirstMigrations();
}
updatePackageJsonFile() {
const projectJson = this.nxProjectJsonService.read(this.pgFlywayConfiguration.nxProjectJsonFile);
if (projectJson) {
const projectName = projectJson.name;
const packageJson = this.packageJsonService.read();
if (packageJson) {
this.packageJsonService.addScripts(pg_flyway_infrastructure_constants_1.PG_FLYWAY_SCRIPTS_CATEGORY_NAME, {
[`pg-flyway:create:${projectName}`]: {
commands: [`./node_modules/.bin/nx run ${projectName}:pg-flyway-create-migration`],
comments: [
`Command to create new empty migration for ${projectName}, for set name pass name to --args, example: npm run pg-flyway:create:appname --args=Init`,
],
},
[`pg-flyway:migrate:${projectName}`]: {
commands: [`./node_modules/.bin/nx run ${projectName}:pg-flyway-migrate`],
comments: [`Applying migrations for ${projectName}`],
},
'pg-flyway:migrate': {
commands: ['./node_modules/.bin/nx run-many -t=pg-flyway-migrate'],
comments: ['Applying migrations of all applications and modules'],
},
}, packageJson);
this.packageJsonService.write(packageJson);
}
}
}
updateProjectJsonFile() {
const projectJson = this.nxProjectJsonService.read(this.pgFlywayConfiguration.nxProjectJsonFile);
const packageJsonFilePath = this.packageJsonService.getPackageJsonFilePath();
const nxProjectJsonFilePath = this.pgFlywayConfiguration.nxProjectJsonFile || this.nxProjectJsonService.getNxProjectJsonFilePath();
if (projectJson && packageJsonFilePath && nxProjectJsonFilePath) {
const pgFlywayMigrationsPath = this.pgFlywayConfiguration.migrationsFolder.replace((0, path_1.dirname)(packageJsonFilePath), '');
const pgFlywayHistoryTable = this.pgFlywayConfiguration.pgFlywayHistoryTable || `__migrations_${(0, case_anything_1.snakeCase)(projectJson.name || '')}`;
const { databaseName } = this.getDbConnectionEnvKeys();
// new migration
this.nxProjectJsonService.addRunCommands([
`export PG_FLYWAY_DATABASE_URL=\${${databaseName}} && export PG_FLYWAY_HISTORY_TABLE=${pgFlywayHistoryTable} && export PG_FLYWAY_LOCATIONS=.${pgFlywayMigrationsPath} && ./node_modules/.bin/pg-flyway create --name=\${npm_config_args:-NewMigration}`,
], 'pg-flyway-create-migration', undefined, this.pgFlywayConfiguration.nxProjectJsonFile);
// migrate
this.nxProjectJsonService.addRunCommands([
`export PG_FLYWAY_DATABASE_URL=\${${databaseName}} && export PG_FLYWAY_HISTORY_TABLE=${pgFlywayHistoryTable} && export PG_FLYWAY_LOCATIONS=.${pgFlywayMigrationsPath} && ./node_modules/.bin/pg-flyway migrate`,
], 'pg-flyway-migrate', undefined, this.pgFlywayConfiguration.nxProjectJsonFile);
// info
this.nxProjectJsonService.addRunCommands([
`export PG_FLYWAY_DATABASE_URL=\${${databaseName}} && export PG_FLYWAY_HISTORY_TABLE=${pgFlywayHistoryTable} && export PG_FLYWAY_LOCATIONS=.${pgFlywayMigrationsPath} && ./node_modules/.bin/pg-flyway info`,
], 'pg-flyway-info', undefined, this.pgFlywayConfiguration.nxProjectJsonFile);
// baseline
this.nxProjectJsonService.addRunCommands([
`export PG_FLYWAY_DATABASE_URL=\${${databaseName}} && export PG_FLYWAY_HISTORY_TABLE=${pgFlywayHistoryTable} && export PG_FLYWAY_LOCATIONS=.${pgFlywayMigrationsPath} && ./node_modules/.bin/pg-flyway baseline`,
], 'pg-flyway-baseline', undefined, this.pgFlywayConfiguration.nxProjectJsonFile);
// validate
this.nxProjectJsonService.addRunCommands([
`export PG_FLYWAY_DATABASE_URL=\${${databaseName}} && export PG_FLYWAY_HISTORY_TABLE=${pgFlywayHistoryTable} && export PG_FLYWAY_LOCATIONS=.${pgFlywayMigrationsPath} && ./node_modules/.bin/pg-flyway validate`,
], 'pg-flyway-validate', undefined, this.pgFlywayConfiguration.nxProjectJsonFile);
// repair
this.nxProjectJsonService.addRunCommands([
`export PG_FLYWAY_DATABASE_URL=\${${databaseName}} && export PG_FLYWAY_HISTORY_TABLE=${pgFlywayHistoryTable} && export PG_FLYWAY_LOCATIONS=.${pgFlywayMigrationsPath} && ./node_modules/.bin/pg-flyway repair`,
], 'pg-flyway-repair', undefined, this.pgFlywayConfiguration.nxProjectJsonFile);
}
}
createFirstMigrations() {
const pgFlywayFeatureName = this.pgFlywayConfiguration.featureName
? (0, case_anything_1.upperCamelCase)(this.pgFlywayConfiguration.featureName)
: '';
const constantCasePgFlywayFeatureName = this.pgFlywayConfiguration.featureName
? (0, case_anything_1.constantCase)(this.pgFlywayConfiguration.featureName)
: '';
const migrationFileName = `V202401212130__Create${pgFlywayFeatureName}User.sql`;
const packageJsonFilePath = this.packageJsonService.getPackageJsonFilePath();
const firstMigrationFilePath = (0, path_1.join)(this.pgFlywayConfiguration.migrationsFolder, migrationFileName);
if (packageJsonFilePath) {
try {
const firstMigration = `-- CreateTable
CREATE TABLE "${pgFlywayFeatureName}User" (
"id" UUID NOT NULL DEFAULT uuid_generate_v4(),
"externalUserId" UUID NOT NULL,
"createdAt" TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "PK_${constantCasePgFlywayFeatureName}_USER" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "UQ_${constantCasePgFlywayFeatureName}_USER" ON "${pgFlywayFeatureName}User"("externalUserId");
`;
if (!firstMigrationFilePath) {
return;
}
const fileDir = (0, path_1.dirname)(firstMigrationFilePath);
if (fileDir) {
if (!(0, fs_1.existsSync)(fileDir)) {
(0, fs_1.mkdirSync)(fileDir, { recursive: true });
(0, fs_1.writeFileSync)(firstMigrationFilePath, firstMigration);
}
}
}
catch (err) {
//
}
}
}
getDbConnectionEnvKeys() {
const concatedDatabaseName = [
this.wrapApplicationOptionsService.project?.name,
this.pgFlywayConfiguration.featureName,
'DATABASE_URL',
]
.filter(Boolean)
.join('_');
const databaseName = this.pgFlywayConfiguration.featureName
? `${(0, case_anything_1.constantCase)(concatedDatabaseName)}`
: `DATABASE_URL`;
return { databaseName };
}
};
exports.PgFlywayInfrastructureUpdaterService = PgFlywayInfrastructureUpdaterService;
exports.PgFlywayInfrastructureUpdaterService = PgFlywayInfrastructureUpdaterService = tslib_1.__decorate([
(0, common_2.Injectable)(),
tslib_1.__metadata("design:paramtypes", [common_1.PackageJsonService,
common_1.NxProjectJsonService,
pg_flyway_configuration_1.PgFlywayConfiguration,
common_1.WrapApplicationOptionsService])
], PgFlywayInfrastructureUpdaterService);
//# sourceMappingURL=pg-flyway-infrastructure-updater.service.js.map