Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added service agreements operational db migration #2780

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* eslint-disable no-console */
import 'dotenv/config';
import fs from 'fs-extra';
import path from 'path';
import appRootPath from 'app-root-path';
import { execSync } from 'child_process';
import semver from 'semver';
import OTNode from './ot-node.js';
import { NODE_ENVIRONMENTS } from './src/constants/constants.js';

Expand All @@ -30,26 +26,26 @@ process.env.NODE_ENV =
await node.start();
} catch (e) {
console.error(`Error occurred while start ot-node, error message: ${e}. ${e.stack}`);
console.error(`Trying to recover from older version`);
if (process.env.NODE_ENV !== NODE_ENVIRONMENTS.DEVELOPMENT) {
const rootPath = path.join(appRootPath.path, '..');
const oldVersionsDirs = (await fs.promises.readdir(rootPath, { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.filter((name) => semver.valid(name) && !appRootPath.path.includes(name));

if (oldVersionsDirs.length === 0) {
console.error(
`Failed to start OT-Node, no backup code available. Error message: ${e.message}`,
);
process.exit(1);
}

const oldVersion = oldVersionsDirs.sort(semver.compare).pop();
const oldversionPath = path.join(rootPath, oldVersion);
execSync(`ln -sfn ${oldversionPath} ${rootPath}/current`);
await fs.promises.rm(appRootPath.path, { force: true, recursive: true });
}
// console.error(`Trying to recover from older version`);
NZT48 marked this conversation as resolved.
Show resolved Hide resolved
// if (process.env.NODE_ENV !== NODE_ENVIRONMENTS.DEVELOPMENT) {
// const rootPath = path.join(appRootPath.path, '..');
// const oldVersionsDirs = (await fs.promises.readdir(rootPath, { withFileTypes: true }))
// .filter((dirent) => dirent.isDirectory())
// .map((dirent) => dirent.name)
// .filter((name) => semver.valid(name) && !appRootPath.path.includes(name));
//
// if (oldVersionsDirs.length === 0) {
// console.error(
// `Failed to start OT-Node, no backup code available. Error message: ${e.message}`,
// );
// process.exit(1);
// }
//
// const oldVersion = oldVersionsDirs.sort(semver.compare).pop();
// const oldversionPath = path.join(rootPath, oldVersion);
// execSync(`ln -sfn ${oldversionPath} ${rootPath}/current`);
// await fs.promises.rm(appRootPath.path, { force: true, recursive: true });
// }
process.exit(1);
}
})();
Expand Down
298 changes: 51 additions & 247 deletions ot-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@ import { createRequire } from 'module';
import { execSync } from 'child_process';
import DependencyInjection from './src/service/dependency-injection.js';
import Logger from './src/logger/logger.js';
import { MIN_NODE_VERSION, NODE_ENVIRONMENTS } from './src/constants/constants.js';
import { MIN_NODE_VERSION } from './src/constants/constants.js';
import FileService from './src/service/file-service.js';
import OtnodeUpdateCommand from './src/commands/common/otnode-update-command.js';
import OtAutoUpdater from './src/modules/auto-updater/implementation/ot-auto-updater.js';
import PullBlockchainShardingTableMigration from './src/migration/pull-sharding-table-migration.js';
import TripleStoreUserConfigurationMigration from './src/migration/triple-store-user-configuration-migration.js';
import TelemetryModuleUserConfigurationMigration from './src/migration/telemetry-module-user-configuration-migration.js';
import PrivateAssetsMetadataMigration from './src/migration/private-assets-metadata-migration.js';
import ServiceAgreementsMetadataMigration from './src/migration/service-agreements-metadata-migration.js';
import RemoveAgreementStartEndTimeMigration from './src/migration/remove-agreement-start-end-time-migration.js';
import MarkOldBlockchainEventsAsProcessedMigration from './src/migration/mark-old-blockchain-events-as-processed-migration.js';
import TripleStoreMetadataMigration from './src/migration/triple-store-metadata-migration.js';
import RemoveOldEpochCommandsMigration from './src/migration/remove-old-epoch-commands-migration.js';
import PendingStorageMigration from './src/migration/pending-storage-migration.js';
import MigrationExecutor from './src/migration/migration-executor.js';

const require = createRequire(import.meta.url);
const pjson = require('./package.json');
Expand All @@ -36,8 +27,14 @@ class OTNode {
async start() {
await this.checkForUpdate();
await this.removeUpdateFile();
await this.executeTripleStoreUserConfigurationMigration();
await this.executeTelemetryModuleUserConfigurationMigration();
await MigrationExecutor.executeTripleStoreUserConfigurationMigration(
this.logger,
this.config,
);
await MigrationExecutor.executeTelemetryModuleUserConfigurationMigration(
this.logger,
this.config,
);
this.logger.info(' ██████╗ ████████╗███╗ ██╗ ██████╗ ██████╗ ███████╗');
this.logger.info('██╔═══██╗╚══██╔══╝████╗ ██║██╔═══██╗██╔══██╗██╔════╝');
this.logger.info('██║ ██║ ██║ ██╔██╗ ██║██║ ██║██║ ██║█████╗');
Expand All @@ -54,15 +51,47 @@ class OTNode {
this.initializeEventEmitter();

await this.initializeModules();
await this.executePullShardingTableMigration();
await this.executePrivateAssetsMetadataMigration();
await this.executeRemoveAgreementStartEndTimeMigration();
await this.executeMarkOldBlockchainEventsAsProcessedMigration();
await this.executeTripleStoreMetadataMigration();
await this.executeServiceAgreementsMetadataMigration();
await this.executeRemoveOldEpochCommandsMigration();
await this.executePendingStorageMigration();

await MigrationExecutor.executePullShardingTableMigration(
this.container,
this.logger,
this.config,
);
await MigrationExecutor.executePrivateAssetsMetadataMigration(
this.container,
this.logger,
this.config,
);
await MigrationExecutor.executeRemoveAgreementStartEndTimeMigration(
this.container,
this.logger,
this.config,
);
await MigrationExecutor.executeMarkOldBlockchainEventsAsProcessedMigration(
this.container,
this.logger,
this.config,
);
await MigrationExecutor.executeTripleStoreMetadataMigration(
this.container,
this.logger,
this.config,
);
await MigrationExecutor.executeServiceAgreementsMetadataMigration(
this.container,
this.logger,
this.config,
);
await MigrationExecutor.executeRemoveOldEpochCommandsMigration(
this.container,
this.logger,
this.config,
);
await MigrationExecutor.executePendingStorageMigration(this.logger, this.config);
await MigrationExecutor.executeServiceAgreementsOpDatabaseMigration(
this.container,
this.logger,
this.config,
);
await this.createProfiles();

await this.initializeCommandExecutor();
Expand Down Expand Up @@ -292,211 +321,6 @@ class OTNode {
});
}

async executePrivateAssetsMetadataMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST
)
return;
const blockchainModuleManager = this.container.resolve('blockchainModuleManager');
const tripleStoreService = this.container.resolve('tripleStoreService');
const serviceAgreementService = this.container.resolve('serviceAgreementService');
const ualService = this.container.resolve('ualService');
const dataService = this.container.resolve('dataService');

const migration = new PrivateAssetsMetadataMigration(
'privateAssetsMetadataMigration',
this.logger,
this.config,
tripleStoreService,
blockchainModuleManager,
serviceAgreementService,
ualService,
dataService,
);

if (!(await migration.migrationAlreadyExecuted())) {
await migration.migrate();
this.logger.info('Node will now restart!');
this.stop(1);
}
}

async executeTelemetryModuleUserConfigurationMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST
)
return;

const migration = new TelemetryModuleUserConfigurationMigration(
'telemetryModuleUserConfigurationMigration',
this.logger,
this.config,
);
if (!(await migration.migrationAlreadyExecuted())) {
await migration.migrate();
this.logger.info('Node will now restart!');
this.stop(1);
}
}

async executeTripleStoreUserConfigurationMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST
)
return;

const migration = new TripleStoreUserConfigurationMigration(
'tripleStoreUserConfigurationMigration',
this.logger,
this.config,
);
if (!(await migration.migrationAlreadyExecuted())) {
await migration.migrate();
this.logger.info('Node will now restart!');
this.stop(1);
}
}

async executePullShardingTableMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST
)
return;

const blockchainModuleManager = this.container.resolve('blockchainModuleManager');
const repositoryModuleManager = this.container.resolve('repositoryModuleManager');
const validationModuleManager = this.container.resolve('validationModuleManager');

const migration = new PullBlockchainShardingTableMigration(
'pullShardingTableMigrationV612',
this.logger,
this.config,
repositoryModuleManager,
blockchainModuleManager,
validationModuleManager,
);
if (!(await migration.migrationAlreadyExecuted())) {
await migration.migrate();
}
}

async executeServiceAgreementsMetadataMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST
)
return;

const blockchainModuleManager = this.container.resolve('blockchainModuleManager');
const repositoryModuleManager = this.container.resolve('repositoryModuleManager');
const tripleStoreService = this.container.resolve('tripleStoreService');
const serviceAgreementService = this.container.resolve('serviceAgreementService');
const ualService = this.container.resolve('ualService');

const migration = new ServiceAgreementsMetadataMigration(
'serviceAgreementsMetadataMigration',
this.logger,
this.config,
tripleStoreService,
blockchainModuleManager,
repositoryModuleManager,
serviceAgreementService,
ualService,
);
if (!(await migration.migrationAlreadyExecuted())) {
await migration.migrate();
}
}

async executeRemoveAgreementStartEndTimeMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST
)
return;

const tripleStoreService = this.container.resolve('tripleStoreService');

const migration = new RemoveAgreementStartEndTimeMigration(
'removeAgreementStartEndTimeMigration',
this.logger,
this.config,
tripleStoreService,
);
if (!(await migration.migrationAlreadyExecuted())) {
await migration.migrate();
}
}

async executeTripleStoreMetadataMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST
)
return;
const blockchainModuleManager = this.container.resolve('blockchainModuleManager');
const tripleStoreService = this.container.resolve('tripleStoreService');
const serviceAgreementService = this.container.resolve('serviceAgreementService');
const ualService = this.container.resolve('ualService');
const dataService = this.container.resolve('dataService');

const migration = new TripleStoreMetadataMigration(
'tripleStoreMetadataMigration',
this.logger,
this.config,
tripleStoreService,
blockchainModuleManager,
serviceAgreementService,
ualService,
dataService,
);

if (!(await migration.migrationAlreadyExecuted())) {
await migration.migrate();
}
}

async executeRemoveOldEpochCommandsMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST
)
return;

const repositoryModuleManager = this.container.resolve('repositoryModuleManager');

const migration = new RemoveOldEpochCommandsMigration(
'removeOldEpochCommandsMigration',
this.logger,
this.config,
repositoryModuleManager,
);
if (!(await migration.migrationAlreadyExecuted())) {
await migration.migrate();
}
}

async executePendingStorageMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST
)
return;

const migration = new PendingStorageMigration(
'pendingStorageMigration',
this.logger,
this.config,
);
if (!(await migration.migrationAlreadyExecuted())) {
await migration.migrate();
}
}

async initializeShardingTableService() {
try {
const shardingTableService = this.container.resolve('shardingTableService');
Expand Down Expand Up @@ -533,26 +357,6 @@ class OTNode {
this.logger.info('Stopping node...');
process.exit(code);
}

async executeMarkOldBlockchainEventsAsProcessedMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST
)
return;

const repositoryModuleManager = this.container.resolve('repositoryModuleManager');

const migration = new MarkOldBlockchainEventsAsProcessedMigration(
'markOldBlockchainEventsAsProcessedMigration',
this.logger,
this.config,
repositoryModuleManager,
);
if (!(await migration.migrationAlreadyExecuted())) {
await migration.migrate();
}
}
}

export default OTNode;
Loading