Skip to content

Commit

Permalink
feat: added a lot of checks for errors and new log
Browse files Browse the repository at this point in the history
  • Loading branch information
MRVMV committed Jan 8, 2021
1 parent dc23493 commit 076932a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 43 deletions.
4 changes: 4 additions & 0 deletions bin/migration/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { createRequire } from 'module';
import prettier from 'prettier';
import { pathConfig } from '../../lib/helpers.js';
import { migrate, updateMigrationState, writeMigration } from '../../lib/migration.js';
import { setLogLevel, log } from '../../lib/functions.js';

const require = createRequire(import.meta.url);

const make = async (argv) => {
setLogLevel(argv.logLevel);

const configOptions = pathConfig(argv);
log(1, `configOptions:${JSON.stringify(configOptions, null, 2)}`);

let migrationResult;
try {
Expand Down
8 changes: 7 additions & 1 deletion bin/migration/sync.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import prettier from 'prettier';
import { pathConfig } from '../../lib/helpers.js';
import { migrate, updateMigrationState } from '../../lib/migration.js';
import { setLogLevel, log } from '../../lib/functions.js';

const sync = async (argv) => {
setLogLevel(argv.logLevel);
const configOptions = pathConfig(argv);

log(1, `configOptions:${JSON.stringify(configOptions, null, 2)}`);

let migrationResult;
try {
migrationResult = await migrate(configOptions);
Expand All @@ -20,7 +24,9 @@ const sync = async (argv) => {
}

// log migration actions
migration.consoles.forEach((action, index) => console.log(`[Action #${index}] ${action}`));
migration.consoles.forEach((action, index) =>
console.log(`[Action #${(index + 1).toString().padStart(2, '0')}] ${action}`),
);

if (argv.preview) {
console.log('Migration result:');
Expand Down
10 changes: 8 additions & 2 deletions bin/migration/undo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import fs from 'fs';
import path from 'path';

import { pathConfig } from '../../lib/helpers.js';
import { getFileName } from '../../lib/functions.js';
import { getFileName, setLogLevel, log } from '../../lib/functions.js';

const require = createRequire(import.meta.url);

const undo = async (argv) => {
const { migrationsDir, stateDir } = pathConfig(argv);
setLogLevel(argv.logLevel);

const configOptions = pathConfig(argv);

const { migrationsDir, stateDir } = configOptions;

log(1, `configOptions:${JSON.stringify(configOptions, null, 2)}`);

const curStatePath = path.join(stateDir, '_current.json');
const curStateName = getFileName(curStatePath);
Expand Down
70 changes: 38 additions & 32 deletions bin/sequelize-mig.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ yargs(hideBin(process.argv))
(yargsA) =>
yargsA
.positional('name', {
describe: 'Set migration name (default: "noname")',
type: 'string',
alias: 'n',
type: 'string',
describe: 'Set migration name (default: "noname")',
})
.positional('preview', {
describe: 'Preview migration actions without writing migration file',
type: 'boolean',
alias: 'p',
type: 'boolean',
describe: 'Preview migration actions without writing migration file',
})
.positional('es6', {
describe: 'Force .cjs file extension',
type: 'boolean',
alias: 'cjs',
type: 'boolean',
describe: 'Force .cjs file extension',
})
.positional('comment', {
describe: 'Set migration comment',
type: 'string',
alias: 'c',
type: 'string',
describe: 'Set migration comment',
})
.example('sequelize-mig migration:make -n InitDb -p').argv,
(argv) => make(argv),
Expand All @@ -44,9 +44,9 @@ yargs(hideBin(process.argv))
(yargsA) =>
yargsA
.positional('preview', {
describe: 'Preview sync actions without updating state',
type: 'boolean',
alias: 'p',
type: 'boolean',
describe: 'Preview sync actions without updating state',
})
.example('sequelize-mig migration:sync -p').argv,
(argv) => sync(argv),
Expand All @@ -57,65 +57,71 @@ yargs(hideBin(process.argv))
(yargsA) =>
yargsA
.positional('force', {
describe: 'force undo even if revisions not equal',
type: 'boolean',
alias: 'f',
type: 'boolean',
describe: 'force undo even if revisions not equal',
})
.positional('delete-current-state', {
describe: 'delete current state (default: true)',
type: 'boolean',
alias: 'del-cur-stt',
type: 'boolean',
default: true,
describe: 'delete current state (default: true)',
})
.positional('delete-current-migration', {
describe: 'delete current migration (default: true)',
type: 'boolean',
alias: 'del-cur-mig',
type: 'boolean',
default: true,
describe: 'delete current migration (default: true)',
})
.positional('rename-backup-state', {
describe: 'rename backup state (default: true)',
type: 'boolean',
alias: 'ren-bak-stt',
type: 'boolean',
default: true,
describe: 'rename backup state (default: true)',
})
.example('sequelize-mig migration:undo').argv,
(argv) => undo(argv),
)
.option('pwd-path', {
describe: 'Override PWD (or just navigate to specified folder in it) (default to ./)',
type: 'string',
alias: 'pwdp',
type: 'string',
describe: 'Override PWD (or just navigate to specified folder in it) (default to ./)',
})
.option('ignore-sequelizerc', {
describe: 'force pwd even on sequelizerc (default to false)',
type: 'boolean',
alias: 'ignrc',
type: 'boolean',
describe: 'force pwd even on sequelizerc (default to false)',
})
.option('sequelizerc-path', {
describe: 'The path to the .sequelizerc file (default to ./.sequelizerc)',
type: 'string',
alias: 'seqrcp',
type: 'string',
describe: 'The path to the .sequelizerc file (default to ./.sequelizerc)',
})
.option('migrations-path', {
describe: 'The path to the migrations folder (default to ./migrations)',
type: 'string',
alias: 'migp',
type: 'string',
describe: 'The path to the migrations folder (default to ./migrations)',
})
.option('models-path', {
describe: 'The path to the models folder (default to ./models)',
type: 'string',
alias: 'modp',
type: 'string',
describe: 'The path to the models folder (default to ./models)',
})
.option('state-path', {
describe: 'The path to the state folder where schema is saved (default to migrations-path)',
type: 'string',
alias: 'statp',
type: 'string',
describe: 'The path to the state folder where schema is saved (default to migrations-path)',
})
.option('index-file-path', {
describe: 'The path to the index file (default to models-path/index.js)',
type: 'string',
alias: 'indxp',
type: 'string',
describe: 'The path to the index file (default to models-path/index.js)',
})
.option('log-level', {
alias: 'll',
type: 'number',
default: 3,
describe: 'The less, The more you see, default: 3...',
})
.alias('help', 'h')
.alias('version', 'v')
Expand Down
10 changes: 10 additions & 0 deletions lib/functions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let logLevel = 3;

export const getYYYYMMDDHHMMSS = (date = new Date()) => {
return [
date.getUTCFullYear(),
Expand Down Expand Up @@ -30,3 +32,11 @@ export const template = (text, params) => {
export const getFileName = (path) => {
return path.replace(/^.*(\\|\/|:)/, '');
};

export const log = (logLvl, message) => {
if (logLvl >= logLevel) console.log(message);
}

export const setLogLevel = (logLvl) => {
logLevel = logLvl;
};
4 changes: 4 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { fileURLToPath } from 'url';

import Sequelize from 'sequelize';

import {log} from './functions.js'

const { dirname: _dirname, join } = path;
const dirname = _dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
Expand Down Expand Up @@ -206,6 +208,8 @@ export const pathConfig = (options) => {
if (options.indexFilePath) indexDir = join(PWD, options.indexFilePath);
if (options.statePath) stateDir = join(PWD, options.statePath);

log(1, `pathConfig options:${JSON.stringify(options, null, 2)}`);

return {
modelsDir,
migrationsDir,
Expand Down
20 changes: 12 additions & 8 deletions lib/migration.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import fs, { writeFileSync } from 'fs';
import path, { join } from 'path';
import prettier from 'prettier';
import { getYYYYMMDDHHMMSS, template } from './functions.js';
import { getYYYYMMDDHHMMSS, template, log } from './functions.js';
import { readAsset, sortActions } from './helpers.js';
import { parseDifference, reverseModels } from './models.js';

const { log } = console;

export const getPartialMigration = (actions) => {
const stringifyModel = (obj) => {
const vals = [];
Expand Down Expand Up @@ -120,7 +118,7 @@ export const getPartialMigration = (actions) => {

default:
action = null;
log('Error: action not specified');
log(4, 'Error: action not specified');
}

if (action) {
Expand Down Expand Up @@ -151,6 +149,8 @@ export const getMigration = (upActions, downActions) => {
export const migrate = async (options) => {
const { modelsDir, migrationsDir, indexDir, stateDir } = options;

log(1, `migrate options:${JSON.stringify(options, null, 2)}`);

if (!fs.existsSync(modelsDir))
throw new Error("Can't find models directory. Use `sequelize init` to create it");

Expand Down Expand Up @@ -184,16 +184,20 @@ export const migrate = async (options) => {
try {
previousState = JSON.parse(fs.readFileSync(currentState.path));
} catch (e) {
console.log('_current.json syntax not valid');
console.log('_current.json syntax is not valid, overriding it');
}
} else {
console.log('_current.json not found. first time running this tool');
}

const { sequelize } = (await import(`file:////${indexDir}`)).default;
const { models } = sequelize;
try {
const { sequelize } = (await import(`file:////${indexDir}`)).default;
const { models } = sequelize;

currentState.tables = reverseModels(sequelize, models);
currentState.tables = reverseModels(sequelize, models);
} catch (e) {
throw new Error(`sequelize index file is not valid probably. solve it and try again: ${e}`);
}

const upActions = parseDifference(previousState.tables, currentState.tables);
const downActions = parseDifference(currentState.tables, previousState.tables);
Expand Down
1 change: 1 addition & 0 deletions test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test('pathConfig', async () => {
const options = {
pwdPath: 'example/example',
ignoreSequelizerc: true,
debug: false,
};
const Config = pathConfig(options);
Object.entries(Config).forEach(([key, val]) => {
Expand Down

0 comments on commit 076932a

Please sign in to comment.