Skip to content

Programmable API not working with MongoDB #474

Open
@BorntraegerMarc

Description

@BorntraegerMarc

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<any> {
        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! We accept bounties via Bountysource.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions