masumsoft / express-cassandra

Cassandra ORM/ODM/OGM for NodeJS with support for Apache Cassandra, ScyllaDB, Datastax Enterprise, Elassandra & JanusGraph.
http://express-cassandra.readthedocs.io
GNU Lesser General Public License v3.0
227 stars 67 forks source link

Update schema without reinitialising app gives an error because model does not match the schema #207

Closed giorgosnty closed 4 years ago

giorgosnty commented 4 years ago

Model looks like this:

module.exports = {
    fields:{
        username: "text",
        name: "text",
        age:"int",
    },
    key: ["username"]
}

And when updating it and add property e.g. weight it is ok until restarting the app. Then, it produces an error because the schema does not match the model.

It's ok in development, but what's the solution in order to be able use it in production?

masumsoft commented 4 years ago

express-cassandra migration system checks for model changes when models.setDirectory is called. You could also use your_model_instance.syncDB() to explicitly migrate a particular model.

If however NODE_ENV==production then models.setDirectory never does the migrations automatically. This is a design decision to reduce the chance of accidental deletion of production data.

Ideally you should execute migrations manually in a production environment or use syncDB() in your own scripts in the deployment stage of the production app.

However if you really want to use the automatic migration features in production, then you may actually set NODE_ENV to something other than production before doing the migrations and keep your fingers crossed! Though I'll highly discourage against enabling automatic migrations in production as this may lead to accidental deletion of data.

giorgosnty commented 4 years ago

Hi, thanks a lot for the answer.

I am using sync db like that inside ExpressCassandra.createClient() and if I do changes it is working and prints 'result true'.

const testModel =  require('./models/testModel');

...

this.testModel = this.models.loadSchema('testModel', testModel);
      this.testModel.syncDB((err, result) => {
        if (err) throw err;
        console.log('result', result)
      });

    }

The problem is when restarting that there is the difference I mentioned above.

I think this issue should not close yet.