Skip to content

Commit

Permalink
Merge pull request #133 from leapfrogtechnology/LPD-3225
Browse files Browse the repository at this point in the history
LPD-3225 : Fix error when migration field removed from sync-db.yml
  • Loading branch information
samirsilwal authored Sep 9, 2021
2 parents 29be5c7 + 8f7c512 commit d3be070
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
12 changes: 9 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import * as init from './init';
import { log } from './util/logger';
import { existsDir } from './util/fs';
import { withTransaction, mapToConnectionReferences, DatabaseConnections } from './util/db';

import Configuration from './domain/Configuration';
Expand All @@ -19,7 +20,7 @@ import OperationResult from './domain/operation/OperationResult';
// Service
import { executeProcesses } from './service/execution';
import { runSynchronize, runPrune } from './service/sync';
import { invokeMigrationApi, KnexMigrationAPI } from './migration/service/knexMigrator';
import { getMigrationPath, invokeMigrationApi, KnexMigrationAPI } from './migration/service/knexMigrator';

/**
* Synchronize all the configured database connections.
Expand All @@ -36,17 +37,22 @@ export async function synchronize(
): Promise<OperationResult[]> {
log('Synchronize');

const migrationPath = getMigrationPath(config);
const dirExist = await existsDir(migrationPath);

const params: SynchronizeParams = {
force: false,
'skip-migration': false,
'skip-migration': !dirExist,
...options
};

const { onStarted: _, ...invokeParams } = params;

// TODO: Need to preload the SQL source code under this step.
const { knexMigrationConfig } = await init.prepare(config, {
migrationPath,
loadSqlSources: true,
loadMigrations: !params['skip-migration']
loadMigrations: !params['skip-migration'] || dirExist
});

const connections = filterConnectionsAsRequired(mapToConnectionReferences(conn), params.only);
Expand Down
1 change: 1 addition & 0 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import KnexMigrationSource from './migration/KnexMigrationSource';
import { resolveMigrationContext } from './migration/service/knexMigrator';

export interface PrepareOptions {
migrationPath?: string;
loadMigrations?: boolean;
loadSqlSources?: boolean;
}
Expand Down
11 changes: 4 additions & 7 deletions src/migration/service/knexMigrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,28 @@ export async function resolveMigrationContext(
config: Configuration,
options: PrepareOptions
): Promise<MigrationSourceContext | null> {
if (options.loadMigrations !== true) {
if (options.loadMigrations !== true || !options.migrationPath) {
return null;
}

log(`Initialize migration context [sourceType=${config.migration.sourceType}]`);

const migrationPath = getMigrationPath(config);

switch (config.migration.sourceType) {
case 'sql':
const src = await resolveSqlMigrations(migrationPath);

const src = await resolveSqlMigrations(options.migrationPath);
log('Available migration sources:\n%O', src);

return new SqlMigrationSourceContext(src);

case 'javascript':
const srcJS = await resolveJavaScriptMigrations(migrationPath);
const srcJS = await resolveJavaScriptMigrations(options.migrationPath);

log('Available migration sources:\n%O', srcJS);

return new JavaScriptMigrationContext(srcJS);

case 'typescript':
const srcTS = await resolveJavaScriptMigrations(migrationPath, FileExtensions.TS);
const srcTS = await resolveJavaScriptMigrations(options.migrationPath, FileExtensions.TS);

log('Available migration sources:\n%O', srcTS);

Expand Down
21 changes: 20 additions & 1 deletion src/util/fs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { log } from './logger';
import { promisify } from 'util';

export const mkdir = promisify(fs.mkdir);

export const readDir = promisify(fs.readdir);
/**
* Create a temporary directory and return it's path.
*
Expand Down Expand Up @@ -53,6 +54,24 @@ export function exists(filename: string): Promise<boolean> {
});
}

/**
* Check if the directory exist.
*
* @param {string} pathName
* @returns {boolean}
*/
export async function existsDir(pathName: string) {
try {
await readDir(pathName);

return true;
} catch (err) {
log(err);

return false;
}
}

/**
* Read all files in a directory.
*
Expand Down
18 changes: 17 additions & 1 deletion test/unit/util/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path';
import { expect } from 'chai';
import { it, describe } from 'mocha';

import { write, read, remove, exists, mkdtemp, glob } from '../../../src/util/fs';
import { write, read, remove, exists, mkdtemp, glob, existsDir } from '../../../src/util/fs';

describe('UTIL: fs', () => {
let filePath: string;
Expand Down Expand Up @@ -60,6 +60,22 @@ describe('UTIL: fs', () => {
});
});

describe('existsDir', () => {
it('should return true if directory of given file path exist', async () => {
const dirPath = await mkdtemp();

const res = await existsDir(dirPath);

expect(res).to.equal(true);
});

it('should return false if directory does not exits in given file path', async () => {
const res = await existsDir('foo/bar');

expect(res).to.equal(false);
});
});

describe('remove', () => {
it('should remove the file', async () => {
await remove(filePath);
Expand Down

0 comments on commit d3be070

Please sign in to comment.