Skip to content

Commit

Permalink
Merge pull request #6 from zishone/dev
Browse files Browse the repository at this point in the history
Fix major apply issues
  • Loading branch information
zishone committed Dec 12, 2019
2 parents 202fb0c + 49d4410 commit 94a5efb
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 39 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ $ mongover <command> [<args>] [<options>]
-s or --seedOnly specifies if mongover should only seed the database instead of migrating it when it already exists.
-m or --migrateForce specifies if mongover should migrate the database even if the specified version is the same.
* **help**: outputs Mongover usage.
**SYNOPSIS**
```shell
$ mongover help
```
## Mongover Specification
### **Format: `dir`**
#### File Structure
Expand All @@ -128,7 +139,8 @@ $ mongover <command> [<args>] [<options>]
{
"seedOnly": false, // Specifies if existing database should be migrated or only seeded.
"dropFirst": false, // Specifies if existing database should be dropped before specification is applied.
"alias": "dbName" // Alias/Name the database specification will be deployed as.
"alias": "dbName", // Alias/Name the database specification will be deployed as.
"mongoVersion": "1.0.0" // Specifies the mongoVersion of the database, this will determine if the database needs to be migrated or not.
}
```
Expand Down Expand Up @@ -177,6 +189,7 @@ $ mongover <command> [<args>] [<options>]
"seedOnly": false, // Specifies if existing database should be migrated or only seeded.
"dropFirst": false, // Specifies if existing database should be dropped before specification is applied.
"alias": "dbName", // Alias/Name the database specification will be deployed as.
"mongoVersion": "1.0.0", // Specifies the mongoVersion of the database, this will determine if the database needs to be migrated or not.
"collections": {
"dropFirst": false, // Specifies if the Collection should be dropped before specification is applied.
"dropIndexesFirst": false, // Specifies if all the Indexes of the Collection should be dropped before specification is applied.
Expand Down
37 changes: 23 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongover",
"version": "1.0.0-beta.4",
"version": "1.0.0",
"description": "A MongoDB Server Database Migration Tool",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -35,7 +35,7 @@
"@types/fs-extra": "^8.0.0",
"@types/minimist": "^1.2.0",
"@types/mocha": "^5.2.7",
"@types/mongodb": "^3.1.28",
"@types/mongodb": "^3.2.1",
"@types/node": "^12.6.8",
"chai": "^4.2.0",
"coveralls": "^3.0.5",
Expand Down
3 changes: 3 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ async function mongover(args: string[]) {
case 'extract':
await extract(options);
break;
case 'help':
console.log(usage);
break;
default:
console.log(usage);
process.exit(exit.error);
Expand Down
14 changes: 9 additions & 5 deletions src/core/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getSpec } from '../utils/get-spec';
import { importData } from '../utils/import-data';
import { parseOptions } from '../utils/parse-options';
import { structureDatabase } from '../utils/structure-database';
import { versionDatabase } from '../utils/version-database';

const logger = getLogger(__filename);

Expand All @@ -30,23 +31,23 @@ export async function apply(options: MongoverOptions = parseOptions({})): Promis
.filter((dirent) => lstatSync(join(options.specPath, dirent)).isDirectory())
.forEach((dirent) => databases.push(getSpec(join(options.specPath, dirent))));
}
const client = await connectServer(options.uri, { useNewUrlParser: true });
const client = await connectServer(options.uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
for (const database of databases) {
if (options.dbs.length === 0 || options.dbs.includes(database.name)) {
if (options.alias.length !== 0) {
database.spec.alias = options.alias[options.dbs.indexOf(database.name)];
}
if (typeof options.seedOnly === 'boolean') {
database.spec.seedOnly = options.seedOnly;
}
const db = await structureDatabase(client, database.name, database.spec);
for (const collectionName in database.spec.collections) {
if (options.collections.length === 0 || options.collections.includes(collectionName)) {
const collectionSpec = database.spec.collections[collectionName];
const existingCollection = await db
.listCollections({ name: collectionName })
.toArray();
if (!existingCollection[0] || !database.spec.seedOnly) {
if (!existingCollection[0] || !options.seedOnly) {
const collection = await createCollection(db, collectionName, collectionSpec, existingCollection[0]);
for (const indexSpec of collectionSpec.indexes) {
await buildIndex(collection, indexSpec);
Expand All @@ -62,6 +63,9 @@ export async function apply(options: MongoverOptions = parseOptions({})): Promis
}
}
}
if (!options.seedOnly) {
await versionDatabase(db, database.spec);
}
}
}
await client.close();
Expand Down
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ export interface DatabaseSpec {
seedOnly: boolean;
dropFirst: boolean;
alias: string | undefined;
mongoVersion: string;
collections: any;
}
1 change: 1 addition & 0 deletions src/utils/build-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function buildIndex(collection: Collection, indexSpec: IndexSpec):
const existing = await collection.indexExists(indexName);
if (existing && indexSpec.dropFirst) {
logger.info('Dropping Index: %s', indexName);
logger.cli('------- Dropping Index:\t\t\t\t%s', indexName);
await collection.dropIndex(indexName);
}
try {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ Usage:
-a or --alias specifies the aliases of the specified databases to apply, a database will use the alias corresponding to its index separated by commas.
-c or --collections specifies which collections to apply. Defaults to all collections in specified databases.
-s or --seedOnly specifies if mongover should only seed the database instead of migrating it when it already exists.
-m or --migrateForce specifies if mongover should migrate the database even if the specified version is the same.
`.trim();

export const databaseSpecTemplate: DatabaseSpec = {
seedOnly: false,
dropFirst: false,
alias: 'dbName',
mongoVersion: '1.0.0',
collections: {},
};

Expand Down
1 change: 1 addition & 0 deletions src/utils/create-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function createCollection(db: Db, collectionName: string, collectio
const collection = db.collection(collectionName);
if (existingCollection && collectionSpec.dropFirst) {
logger.info('Dropping Collection: %s', collectionName);
logger.cli('----- Dropping Collection:\t\t\t%s', collectionName);
await collection.drop();
await db.createCollection(collectionName, collectionSpec.options);
} else if (existingCollection && collectionSpec.dropIndexesFirst) {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/get-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
debug,
Debugger,
} from 'debug';
import { sep } from 'path';

interface Logger {
debug: Debugger;
Expand All @@ -12,7 +13,7 @@ interface Logger {
}

export function getLogger(fileName: string): Logger {
const component = fileName.split('.')[0].split('/').pop();
const component = fileName.split('.')[0].split(sep).pop();
return {
debug: debug(`mongover:debug:${component}`),
info: debug(`mongover:info:${component}`),
Expand Down
11 changes: 7 additions & 4 deletions src/utils/get-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import {
readdirSync,
readJSONSync,
} from 'fs-extra';
import { join } from 'path';
import {
join,
sep,
} from 'path';

export function getSpec(specPath: string) {
const segments = specPath.split('/');
const segments = specPath.split(sep);
let spec: any;
let name: string;
let dataPath: string;
Expand All @@ -19,8 +22,8 @@ export function getSpec(specPath: string) {
segments.pop();
spec = readJSONSync(specPath);
name = (segments.pop() || '').split('.')[0];
dataPath = join('/', ...segments, name, 'data');
specPath = join('/', ...segments, name);
dataPath = join(sep, ...segments, name, 'data');
specPath = join(sep, ...segments, name);
}
if (!spec.collections && existsSync(join(specPath, 'collections'))) {
spec.collections = {};
Expand Down
8 changes: 4 additions & 4 deletions src/utils/import-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ async function processDataArr(collection: Collection, dataSpec: DataSpec, dataAr
try {
await collection.insertOne(data);
} catch (error) {
logger.warn('Can\'t insert Data: %o: %s', data, error.message);
logger.cli('------- Can\'t insert Data:\t\t\t%o: %s', data, error.message);
logger.warn('Can\'t insert Data: %o: %s', `${EJSON.stringify(data).substring(0, 60)}...`, '\n\t\t\t\tMessage:\t\t\t', error.message);
logger.cli('------- Can\'t insert Data:\t\t\t%o: %s', `${EJSON.stringify(data).substring(0, 60)}...`, '\n\t\t\t\tMessage:\t\t\t', error.message);
}
} else {
const filter: any = {};
Expand All @@ -51,8 +51,8 @@ async function processDataArr(collection: Collection, dataSpec: DataSpec, dataAr
await collection.insertOne(data);
}
} catch (error) {
logger.warn('Can\'t upsert Data: %o%s', data, error.message);
logger.cli('------- Can\'t upsert Data:\t\t\t%o: %s', data, error.message);
logger.warn('Can\'t upsert Data: %o%s', `${EJSON.stringify(data).substring(0, 60)}...`, '\n\t\t\t\tMessage:\t\t\t', error.message);
logger.cli('------- Can\'t upsert Data:\t\t\t%o: %s', `${EJSON.stringify(data).substring(0, 60)}...`, '\n\t\t\t\tMessage:\t\t\t', error.message);
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/utils/parse-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,10 @@ export function parseOptions(args: any): MongoverOptions {
if (!args.query) {
args.query = {};
}
if (args.s) {
if (args.s || args.seedOnly) {
args.seedOnly = true;
}
if (!args.s) {
args.seedOnly = false;
}
if (args.seedOnly) {
args.seedOnly = true;
}
if (!args.seedOnly) {
if (!args.s && !args.seedOnly) {
args.seedOnly = false;
}
if (args.alias.length !== 0 && args.alias.length !== args.dbs.length) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/structure-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function structureDatabase(client: MongoClient, databaseName: strin
const db = client.db(databaseSpec.alias || databaseName);
if (databaseSpec.dropFirst) {
logger.info('Dropping Database: %s', databaseSpec.alias || databaseName);
logger.cli('--- Dropping Database:\t\t\t%s', databaseName + (databaseSpec.alias ? ` as ${databaseSpec.alias}` : ''));
await db.dropDatabase();
}
logger.info('Structured Database: %s', databaseName + (databaseSpec.alias ? ` as ${databaseSpec.alias}` : ''));
Expand Down
29 changes: 29 additions & 0 deletions src/utils/version-database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
Db,
MongoClient,
} from 'mongodb';
import { DatabaseSpec } from '../types/types';
import { getLogger } from './get-logger';

const logger = getLogger(__filename);

export async function versionDatabase(db: Db, databaseSpec: DatabaseSpec): Promise<Db> {
try {
logger.debug('Versioning Database: %s', `${db.databaseName}@${databaseSpec.mongoVersion}`);
logger.cli('--- Versioning Database:\t\t\t\t%s', `${db.databaseName}@${databaseSpec.mongoVersion}`);
const mongoVersion = { mongoVersion: databaseSpec.mongoVersion };
const collection = db.collection('mongover');
const currentVersion = await collection.findOne({});
if (currentVersion) {
await collection.updateOne({}, { $set: mongoVersion });
} else {
await collection.insertOne(mongoVersion);
}
logger.info('Versioned Database: %s', `${db.databaseName}@${databaseSpec.mongoVersion}`);
return db;
} catch (error) {
logger.error('Error versioning Database: %s', `${db.databaseName}@${databaseSpec.mongoVersion}`);
logger.cli('--- Error versioning Database:\t\t\t%s', `${db.databaseName}@${databaseSpec.mongoVersion}`);
throw error;
}
}

0 comments on commit 94a5efb

Please sign in to comment.