db-migrate / db-migrate/node-db-migrate
Programmable API not working with MongoDB
- Dominant language
- JavaScript
- Stars
- 2.3k
- Forks
- 361
- PR merge metrics
- No merged PRs in 30d
Description
Currently db-migrate programmable API doesn't work properly with scope.
As there are two migration scopes (root and test) and so we intent to run `down on 'test' scope`, `down on root scope`, `up on root scope`, then `up on test scope` in sequence, however, the `db-migrate API` doesn't work properly as our intention. Once we run `down on test scope`, the coming up/down would run on the same scope.
To execute up and down in sequence with different scopes, `DBMigrateHelper` was added as a mediator and now we can mix up and down at our convenience (but we should invoke `resetInstance` method between up and down):
```
let dbm = DBMigrateHelper.getInstance();
DBMigrateHelper.executeDown(dbm, 'test')
.then(() => DBMigrateHelper.executeDown(dbm))
.then(() => DBMigrateHelper.resetInstanceAsync(dbm))
.then(() => DBMigrateHelper.executeUp(dbm))
.then(() => DBMigrateHelper.executeUp(dbm, 'test'))
.then(() => resolve());
```
`DBMigrateHelper`:
```
class DBMigrateHelper {
public static getInstance() {
return DBMigrate.getInstance(true);
}
public static resetInstance(dbm: any): void {
if (dbm.internals.matching) {
delete dbm.internals.matching;
}
if (dbm.internals.migrationMode) {
delete dbm.internals.migrationMode;
}
if (dbm.internals.argv.count) {
delete dbm.internals.argv.count;
}
if (dbm.internals.locTitle) {
delete dbm.internals.locTitle;
}
if (dbm.internals.migrationsDir) {
delete dbm.internals.migrationsDir;
}
}
public static resetInstanceAsync(dbm: any): Promise {
return new Promise((resolve, reject) => {
try {
DBMigrateHelper.resetInstance(dbm);
resolve();
} catch (e) {
reject(e);
}
});
}
public static executeUp(dbm: any, scope?: string): any {
return dbm.up(100, !scope ? '' : scope);
}
public static executeDown(dbm: any, scope?: string): any {
return dbm.down(100, !scope ? '' : scope);
}
}
```
---
Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/44452534-programmable-api-not-working-with-mongodb?utm_campaign=plugin&utm_content=tracker%2F73887&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F73887&utm_medium=issues&utm_source=github).
Contributor guide
Assessment
This issue has not been assessed yet.