seppevs / migrate-mongo

A database migration tool for MongoDB in Node
MIT License
926 stars 163 forks source link

Unable to run on the deployed instance #407

Closed bassochette closed 1 year ago

bassochette commented 2 years ago

Describe the bug Migration works on local environment but does not on the deployed instance. It failed stating: Error: Cannot find module '../lib/migrate-mongo' Symlinks in node_modules/.bin are not kept by the ci pipeline and are copies of the executable.

To Reproduce Steps to reproduce the behavior: Copy the executable into node_modules/.bin to replace the symlink.

Expected behavior Should display the migration status

Additional context

xxx@1.0.0 migrate:status /home/site/wwwroot migrate-mongo status -f dist/migrate-mongo-config.js

internal/modules/cjs/loader.js:892 throw err; ^

Error: Cannot find module '../lib/migrate-mongo' Require stack:

npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2022-01-03T16_04_12_200Z-debug.log


- scripts in package.json
"migrate:status": "migrate-mongo status -f dist/migrate-mongo-config.js",
"migrate:undo-last": "migrate-mongo down -f dist/migrate-mongo-config.js && npm run migrate:status",
"migrate": "migrate-mongo up -f dist/migrate-mongo-config.js"

- migrate-mongo-config.ts
```ts
const { config } = require('dotenv');
const { build, get } = require('@xxx/node-config');

// For local development
config();

// Not really typscripty but required for the trick of an async export
module.exports = (async function () {
    await build({
        ignoreKvMapping: !!process.env.CONFIG_IGNORE_KV_MAPPING,
    });
    return {
        mongodb: {
            url: get('db.url'),
            databaseName: get('db.options.dbName'),
            options: get('db.options') || {},
        },

        // The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
        migrationsDir: process.env.NODE_ENV === 'local' ? 'migrations' : 'dist/migrations',

        // The mongodb collection where the applied changes are stored. Only edit this when really necessary.
        changelogCollectionName: 'changelog',

        // The file extension to create migrations and search for in migration dir
        migrationFileExtension: process.env.NODE_ENV === 'local' ? '.ts' : '.js',

        // Enable the algorithm to create a checksum of the file contents and use that in the comparison to determin
        // if the file should be run.  Requires that scripts are coded to be run multiple times.
        useFileHash: false,
    };
})();
thehogfather commented 2 years ago

I had a similar problem recently and it turns out the way I was zipping the deployed packages was the problem. You can preserve symbolic links by call zip with the --symlinks argument.

seppevs commented 1 year ago

Closing this, see last comment for resolution.