Skip to content

Applying migrations to the database whenever you run npx migrate-mongo up #1

@kasir-barati

Description

@kasir-barati

Is your feature request related to a problem? Please describe.
I'm always frustrated when I have to change file name to apply my migrations on database. I think it is developer's responsibility to take care of applied migrations (adding another key-value to separate migrated documents from unmigrated ones). The same pull request opened here

Describe the solution you'd like
But I think it is not right to check database changelog. Basically I think we have to run migrations regardless of database changelog and it's developers responsibility to take care of it. E.X:

// @ts-check
const path = require('path');
const figlet = require('figlet');
const { promises: fs } = require('fs');
const dotenv = require('dotenv');

dotenv.config({
    path: path.join('..', '.env'),
});

const { CustomError } = require('../commons/custom-error');
const MIGRATED_VERSION = process.env.MIGRATED_VERSION;
const MIGRATED_COLLECTION = 'admin-user';

module.exports = {
    /**
     *
     * @param {import('mongodb').Db} db
     * @param {import('mongodb').MongoClient} client
     */
    async up(db, client) {
        const session = await client.startSession();
        try {
            session.startTransaction();

            console.log(figlet.textSync('admin'));
            console.log(figlet.textSync(' ||'));
            console.log(figlet.textSync('\\/'));
            console.log(figlet.textSync('user'));

            // START
            const unMigratedDocumentsCount = await db
                .collection('admins')
                .countDocuments({
                    migratedVersion: {
                        $ne: MIGRATED_VERSION,
                    },
                    migratedCollection: {
                        $ne: MIGRATED_COLLECTION,
                    },
                });
                await session.commitTransaction();
        } catch (error) {
            await fs.appendFile(
                `${new Date().getTime()}-admin-error.json`,
                JSON.stringify(error),
                'utf-8',
            );
            await session.abortTransaction();
            throw error;
        } finally {
            await session.endSession();
        }
    },
    
    /**
     *
     * @param {import('mongodb').Db} db
     * @param {import('mongodb').MongoClient} client
     */
    async down(db, client) {},
};

And with this way I will be sure that no matter how many times you will run my migration script, it will change the database documents just once. And if it fails you can be sure that it will just migrate rest of the unmigrated documents. not all of them (It will be good in large dataset cases).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions