Skip to content

Commit

Permalink
feat: add some checks for better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MRVMV committed Jan 11, 2021
1 parent 076932a commit a1d9ba1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
12 changes: 7 additions & 5 deletions bin/migration/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ const make = async (argv) => {
const { previousState, currentState, migration } = migrationResult;

if (migration.commandsUp.length === 0) {
console.log('No changes found, No new migration needed!');
log(3, 'No changes found, No new migration needed!');
return;
}

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

if (argv.preview) {
console.log('Migration result:');
console.log(
log(3, 'Migration result:');
log(
3,
prettier.format(`[ \n${migration.commandsUp.join(', \n')} \n];\n`, {
parser: 'babel',
}),
Expand All @@ -56,7 +57,8 @@ const make = async (argv) => {
argv.es6 !== null ? argv.es6 : type === 'module',
);

console.log(
log(
3,
`New migration to revision ${currentState.revision} has been saved to file\n'${info.filename}'`,
);
};
Expand Down
13 changes: 7 additions & 6 deletions bin/migration/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@ const sync = async (argv) => {
try {
migrationResult = await migrate(configOptions);
} catch (e) {
console.error(e.message);
log(4, e.message);
return;
}

const { previousState, currentState, migration } = migrationResult;
if (migration.commandsUp.length === 0) {
console.log('No changes found, No new migration needed!');
log(3, 'No changes found, No new migration needed!');
return;
}

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

if (argv.preview) {
console.log('Migration result:');
console.log(
log(3, 'Migration result:');
log(
3,
prettier.format(`[ \n${migration.commandsUp.join(', \n')} \n];\n`, {
parser: 'babel',
}),
Expand All @@ -40,7 +41,7 @@ const sync = async (argv) => {

await updateMigrationState(currentState, previousState);

console.log('State file synced to db successfully');
log(3, `State file synced to db successfully`);
};

export default sync;
27 changes: 15 additions & 12 deletions bin/migration/undo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const require = createRequire(import.meta.url);

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

const configOptions = pathConfig(argv);

const { migrationsDir, stateDir } = configOptions;
Expand All @@ -35,9 +35,9 @@ const undo = async (argv) => {
bakStatePath = curState.backupPath;

curStateRevision = curState.revision;
console.log(`Current state file: ${curStateName}, Revision: ${curStateRevision}`);
log(3, `Current state file: ${curStateName}, Revision: ${curStateRevision}`);
} else {
console.log("Can't find current state. Skipping");
log(3, `Can't find current state. Skipping`);
}

if (fs.existsSync(migrationsDir)) {
Expand All @@ -48,12 +48,13 @@ const undo = async (argv) => {
curMigPath = path.join(migrationsDir, curMigName);

curMigRevision = (await import(`file:////${curMigPath}`)).default.info.revision;
console.log(`Current migration file: ${curMigName}, Revision: ${curMigRevision}`);

log(3, `Current migration file: ${curMigName}, Revision: ${curMigRevision}`);
} else {
console.log("Can't find any migrations files. Skipping");
log(3, `Can't find any migrations files. Skipping`);
}
} else {
console.log("Can't find migrations folder. Skipping");
log(3, `Can't find any migrations folder. Skipping`);
}

if (fs.existsSync(bakStatePath)) bakStateRevision = curStateRevision - 1;
Expand All @@ -69,27 +70,29 @@ const undo = async (argv) => {
) {
if (curStateRevision && argv.delCurStt) {
fs.unlinkSync(curStatePath);
console.log(`Deleted current state file: ${curStateName}`);
log(3, `Deleted current state file: ${curStateName}`);
}

if (curMigRevision && argv.delCurMig) {
fs.unlinkSync(curMigPath);
console.log(`Deleted current migration file: ${curMigName}`);
log(3, `Deleted current migration file: ${curMigName}`);
}

if (bakStateRevision && argv.renBakStt) {
fs.renameSync(bakStatePath, curStatePath);

bakStateName = getFileName(bakStatePath);
console.log(
log(
3,
`Reverted to backup state: ${bakStateName} new name: ${curStateName}, Revision: ${bakStateRevision}`,
);
} else if (!bakStateRevision) {
console.log("Can't find backup state. Skipping.");
log(3, "Can't find backup state. Skipping.");
}
console.log('We are done!');
log(3, 'We are done!');
} else {
console.log(
log(
3,
`Revisions from current state and current migration Are not equal.
So they are not synced. anyway you can force tool running with -f or turn on specific options`,
);
Expand Down
6 changes: 3 additions & 3 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const migrate = async (options) => {
throw new Error("Can't find sequelize index file. Use `sequelize init` to create it");

if (!fs.existsSync(stateDir)) {
console.log("Can't find State directory. Creating it...");
log(3, "Can't find State directory. Creating it...");
fs.mkdirSync(stateDir, { recursive: true });
}

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

try {
Expand Down

0 comments on commit a1d9ba1

Please sign in to comment.