morpheus4j
Version:
Morpheus is a migration tool for Neo4j. It aims to be a simple and intuitive way to migrate your database.
78 lines • 4.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeleteService = void 0;
const constants_1 = require("../constants");
const errors_1 = require("../errors");
const logger_1 = require("./logger");
class DeleteService {
repository;
fileService;
constructor(repository, fileService) {
this.repository = repository;
this.fileService = fileService;
}
async delete(identifier, options = {}) {
try {
const state = await this.repository.getMigrationState();
if (!state.baselineExists) {
throw new errors_1.MigrationError('No migrations exist in the database');
}
const targetMigration = this.findTargetMigration(state.appliedMigrations, identifier);
if (!targetMigration) {
throw new errors_1.MigrationError(`No migration found matching identifier: ${identifier}`);
}
if (options.dryRun) {
await this.previewDeletion(targetMigration);
return;
}
await this.performDeletion(targetMigration, state.appliedMigrations);
}
catch (error) {
throw new errors_1.MigrationError(`Delete operation failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
findTargetMigration(migrations, identifier) {
return migrations.find((migration) => migration.node.version === identifier); // || migration.node.source === identifier)
}
async performDeletion(targetMigration, allMigrations) {
logger_1.Logger.info(`Deleting migration: ${targetMigration.node.source}`);
// Sort migrations by version to ensure correct order
const sortedMigrations = [...allMigrations].sort((a, b) => this.fileService.compareVersions(a.node.version, b.node.version));
// Find the previous and next migrations in the chain
const currentIndex = sortedMigrations.findIndex((m) => m.node.version === targetMigration.node.version);
const previousMigration = currentIndex > 0 ? sortedMigrations[currentIndex - 1] : undefined;
const nextMigration = currentIndex < sortedMigrations.length - 1 ? sortedMigrations[currentIndex + 1] : undefined;
try {
// Handle chain maintenance before deletion
if (nextMigration) {
// If there's a next migration, we need to connect it to the previous one
// (which might be BASELINE or another migration)
const previousVersion = previousMigration?.node.version || constants_1.BASELINE;
await this.repository.createMigrationRelation(previousVersion, nextMigration.node.version);
}
else if (previousMigration) {
// If this is the last migration, mark the previous one as latest
await this.repository.markAsLatestMigration(previousMigration.node.version);
}
// Delete the target migration node and its relationships
await this.repository.deleteMigration(targetMigration.node.version);
logger_1.Logger.info('Migration deleted successfully');
}
catch (error) {
throw new errors_1.MigrationError(`Failed to delete migration ${targetMigration.node.source}: ${error instanceof Error ? error.message : String(error)}`);
}
}
async previewDeletion(migration) {
logger_1.Logger.info('Dry run - no changes will be made to the database');
logger_1.Logger.info(`Would delete migration:`);
logger_1.Logger.info(` Version: ${migration.node.version}`);
logger_1.Logger.info(` Source: ${migration.node.source}`);
logger_1.Logger.info(` Type: ${migration.node.type}`);
logger_1.Logger.info(` Description: ${migration.node.description}`);
logger_1.Logger.info(` Applied at: ${migration.relation.at}`);
logger_1.Logger.info(` Duration: ${migration.relation.in}`);
logger_1.Logger.info('Dry run complete');
}
}
exports.DeleteService = DeleteService;
//# sourceMappingURL=delete.service.js.map