Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🗣 [Suggestion] Error catching & Empty Databases handling #61

Open
ulfgebhardt opened this issue Aug 10, 2018 · 0 comments
Open

🗣 [Suggestion] Error catching & Empty Databases handling #61

ulfgebhardt opened this issue Aug 10, 2018 · 0 comments

Comments

@ulfgebhardt
Copy link

ulfgebhardt commented Aug 10, 2018

  • Im having trouble catching Errors in migrations.
  • I have to use a catchblock within the up() function.
  • I have to handle empty databases myself

What am I doing wrong regarding error handling?
Can we handle empty databases with this lib?

  • I don't like to use try{} catch(e) {} in every migration
  • If possible I would like to have automatic handling of empty databases (we have no initial dataschema - thats created via mongoose)

Main call

await migrate().catch((err) => {
  console.log(err);
  console.log('Migration not successful - I die now!');
  process.exit();
});

Ref: https://github.com/demokratie-live/democracy-server/blob/sprint%2323/sms_verification/src/index.js#L41

Ecapsulated migrate called from above

export const migrate = async () =>
  new Promise((resolve, reject) => {
    const migrator = new mm.Migrator(config);
    migrator.runFromDir(`${__dirname}/../`, (err) => {
      if (err) {
        reject(err);
      }
      migrator.dispose();
      resolve();
    });
  });

Ref: https://github.com/demokratie-live/democracy-server/blob/sprint%2323/sms_verification/src/migrations/scripts/index.js#L16

The actual Migration

module.exports.up = async function (done) { // eslint-disable-line
  // Why do we have to catch here - makes no sense!
  try {
    //generate an error
    some.error ? 'no-error' : 'error';

    // Do we have a fresh install? - Mark as done
    const collections = await new Promise((resolve, reject) => {
      this.db.listCollections().toArray((err, names) => {
        if (err) {
          reject(err);
        } else {
          resolve(names);
        }
      });
    });
    if (collections.length === 0) {
      done();
      return;
    }

    // Migration here
   // ...

   done();
  } catch (err) {
    done(err);
  }
};

Ref: https://github.com/demokratie-live/democracy-server/blob/sprint%2323/sms_verification/src/migrations/1-sms-verification.js#L10

Output:

[nodemon] restarting due to changes...
[nodemon] starting `node --require dotenv/config --require babel-register src/index.js`
the server/replset/mongos options are deprecated, all their options are supported at the top level of the options object [poolSize,ssl,sslValidate,sslCA,sslCert,ciphers,ecdhCurve,sslKey,sslPass,sslCRL,autoReconnect,noDelay,keepAlive,connectTimeoutMS,family,socketTimeoutMS,reconnectTries,reconnectInterval,ha,haInterval,replicaSet,secondaryAcceptableLatencyMS,acceptableLatencyMS,connectWithNoPrimary,authSource,w,wtimeout,j,forceServerObjectId,serializeFunctions,ignoreUndefined,raw,bufferMaxEntries,readPreference,pkFactory,promiseLibrary,readConcern,maxStalenessSeconds,loggerLevel,logger,promoteValues,promoteBuffers,promoteLongs,domainsEnabled,keepAliveInitialDelay,checkServerIdentity,validateOptions,appname,auth]
     Migration 'sms-verification': error
       ReferenceError: some is not defined
ReferenceError: some is not defined
    at Object.<anonymous> (D:/sources/demokratie-live/democracy-development/server/src/migrations/1-sms-verification.js:14:5)
    at Generator.next (<anonymous>)
    at step (D:\sources\demokratie-live\democracy-development\server\src\migrations\1-sms-verification.js:25:191)
    at D:\sources\demokratie-live\democracy-development\server\src\migrations\1-sms-verification.js:25:437
    at new Promise (<anonymous>)
    at Object.<anonymous> (D:\sources\demokratie-live\democracy-development\server\src\migrations\1-sms-verification.js:25:99)
    at Object.module.exports.up (D:/sources/demokratie-live/democracy-development/server/src/migrations/1-sms-verification.js:10:1)
    at D:\sources\demokratie-live\democracy-development\server\node_modules\mongodb-migrations\lib\mongodb-migrations.js:202:21
    at Migrator._run (D:\sources\demokratie-live\democracy-development\server\node_modules\mongodb-migrations\lib\mongodb-migrations.js:222:14)
    at D:\sources\demokratie-live\democracy-development\server\node_modules\mongodb-migrations\lib\mongodb-migrations.js:78:26
    at handleCallback (D:\sources\demokratie-live\democracy-development\server\node_modules\mongodb-migrations\node_modules\mongodb\lib\utils.js:120:56)
    at D:\sources\demokratie-live\democracy-development\server\node_modules\mongodb-migrations\node_modules\mongodb\lib\cursor.js:861:16
    at handleCallback (D:\sources\demokratie-live\democracy-development\server\node_modules\mongodb-core\lib\cursor.js:171:5)
    at setCursorNotified (D:\sources\demokratie-live\democracy-development\server\node_modules\mongodb-core\lib\cursor.js:515:3)
    at D:\sources\demokratie-live\democracy-development\server\node_modules\mongodb-core\lib\cursor.js:599:16
    at queryCallback (D:\sources\demokratie-live\democracy-development\server\node_modules\mongodb-core\lib\cursor.js:232:18)
    at D:\sources\demokratie-live\democracy-development\server\node_modules\mongodb-core\lib\connection\pool.js:469:18
    at process._tickCallback (internal/process/next_tick.js:61:11)
Migration not successful - I die now!
[nodemon] clean exit - waiting for changes before restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant