@rsksmart/rif-storage-pinning
Version:
Application for providing your storage space to other to use in exchange of RIF Tokens
125 lines (123 loc) • 5.55 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const command_1 = require("@oclif/command");
const logger_1 = require("../logger");
const utils_1 = __importDefault(require("../utils"));
const migrations_1 = require("../migrations");
const logger = logger_1.loggingFactory('cli:db-migration');
const MigrationTemplate = `import Sequelize, { QueryInterface } from 'sequelize'
import { Sequelize as SequelizeTs } from 'sequelize-typescript'
export default {
// eslint-disable-next-line require-await
async up (queryInterface: QueryInterface, sequelize: SequelizeTs): Promise<void> {
return Promise.reject(Error('Not implemented'))
},
// eslint-disable-next-line require-await
async down (queryInterface: QueryInterface, sequelize: SequelizeTs): Promise<void> {
return Promise.reject(Error('Not implemented'))
}
}
`;
class DbCommand extends utils_1.default {
migrate(migrator, migrations, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield migrator.pending()).length) {
this.log('No pending migrations found');
this.exit();
}
this.spinner.start('DB migration');
yield migrator.up(options);
this.spinner.stop();
});
}
undo(migrator, migrations, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield migrator.executed()).length) {
this.log('No executed migrations found');
this.exit();
}
this.spinner.start('Undo DB migration');
yield migrator.down(options);
this.spinner.stop();
});
}
generateMigration(name) {
const migrationsFolder = path_1.default.resolve(__dirname, '../migrations');
const scriptsFolder = path_1.default.resolve(__dirname, '../migrations/scripts');
const fileName = `./${Date.now()}-${name}.ts`;
const filePath = path_1.default.resolve(scriptsFolder, fileName);
if (!fs_1.default.existsSync(migrationsFolder)) {
throw new Error('Migrations folder not found. Please run command from project root and make sure that you have \'migrations\' folder setup');
}
this.spinner.start(`Creating migration ${fileName}`);
if (!fs_1.default.existsSync(scriptsFolder)) {
fs_1.default.mkdirSync(scriptsFolder);
}
fs_1.default.writeFileSync(filePath, MigrationTemplate);
this.spinner.stop();
}
run() {
return __awaiter(this, void 0, void 0, function* () {
const { flags: originalFlags } = this.parse(DbCommand);
const flags = originalFlags;
if (!flags.up && !flags.down && !flags.generate)
throw new Error('One of \'--generate, --up, --down\' required');
if (!this.sequelize) {
throw new Error('DB not instantiated!');
}
const migrator = new migrations_1.Migration(this.sequelize);
if (flags.up)
yield this.migrate(migrator, flags.migration, flags);
if (flags.down)
yield this.undo(migrator, flags.migration, flags);
if (flags.generate)
this.generateMigration(flags.generate);
this.exit();
});
}
}
exports.default = DbCommand;
DbCommand.flags = Object.assign(Object.assign({}, utils_1.default.flags), { up: command_1.flags.boolean({
char: 'u',
description: 'Migrate DB',
exclusive: ['down', 'generate']
}), down: command_1.flags.boolean({
char: 'd',
description: 'Undo db migration',
exclusive: ['up', 'generate']
}), generate: command_1.flags.string({
char: 'd',
description: 'Generate migrations using template [--generate=migration_name]',
exclusive: ['up', 'down']
}), to: command_1.flags.string({
char: 't',
description: 'Migrate to'
}), migration: command_1.flags.string({
char: 'm',
description: 'Migration file',
multiple: true
}) });
DbCommand.description = 'DB migration';
DbCommand.examples = [
'$ rif-pinning db-migration --up',
'$ rif-pinning db-migration --down',
'$ rif-pinning db-migration --up --to 0-test',
'$ rif-pinning db-migration --up --migration 01-test --migration 02-test',
'$ rif-pinning db-migration --up --db ./test.sqlite --to 09-test',
'$ rif-pinning db-migration --down --db ./test.sqlite --to 09-test',
'$ rif-pinning db-migration --generate my_first_migration'
];