seppevs / migrate-mongo

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

API Error: migrations directory does not exist. #269

Open cristianst opened 3 years ago

cristianst commented 3 years ago

Describe the bug Migrations folder can not be found in same root folder.

I20201026-19:28:18.793(1)? Error: migrations directory does not exist: /home/cristian-sanchez/editor/.meteor/local/build/programs/server/migrations I20201026-19:28:18.794(1)? at Object.shouldExist (/home/cristian-sanchez/editor/node_modules/migrate-mongo/lib/env/migrationsDir.js:60:13) I20201026-19:28:18.794(1)? => awaited here: I20201026-19:28:18.794(1)? at Function.Promise.await (/home/cristian-sanchez/.meteor/packages/promise/.0.11.2.1i14sd.5id2r++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/promise_server.js:56:12) I20201026-19:28:18.794(1)? at imports/api/migrations/healthAPI.js:21:30

To Reproduce I am currently implementing a heath check endpoint and I want to make sure within that endpoint that all migrations have been applied.

File Structure :

/migrations
-- healthApi.js
-- migrate-mongo-config.js
-- migrations (folder containing migrations)
// healthAPI.js
const migrateMongoConfig = require('./migrate-mongo-config');

const {
    database,
    config,
    status
  } = require('migrate-mongo');

const healthCheckAPI  = express();

healthCheckAPI.use(express.json());

healthCheckAPI.get('/ready', async function(req, res){
    try {
        config.set(migrateMongoConfig);

        const { db } = await database.connect();
        const migrationStatus = await status(db); // it fails here.

        migrationStatus.forEach(({ fileName, appliedAt }) => console.log(fileName, ':', appliedAt));
    } catch(e){
        console.log(e);
    }
    res.sendStatus(200); // despite the status, it will be changed.
});
// migrate-mongo.config.js
const config = {
    mongodb: {
      // TODO Change (or review) the url to your MongoDB:
      url: "mongodb://localhost:45853",

      // TODO Change this to your database name:
      databaseName: "test",

      options: {
        useNewUrlParser: true, // removes a deprecation warning when connecting
        useUnifiedTopology: true, // removes a deprecating warning when connecting
        //   connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
        //   socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
      }
    },

    // The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
    migrationsDir: "./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: ".js"
  };

  // Return the config as a promise
  module.exports = config;

Expected behavior I expect the connection to succeed and to get the migration status properly.

Additional context Not sure if its relevant but im using meteor as main framework for the app.

MattG561 commented 2 years ago

Also running into this problem via the API on Meteor.js. I think it may have to do with Meteor's build system. They bundle everything into the .meteor directory in your project. It seems the development file structure if not the same as what actually gets minified and run in your local development environment.

I would rather stay framework agnostic and use this library over a Meteor specific package.

Everything works as expected from the cli but I'd rather have this code run on startup.

Any solutions or ideas?