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

How to implement migrations with SQL databases #274

Open
davidszanto opened this issue Aug 10, 2021 · 2 comments
Open

How to implement migrations with SQL databases #274

davidszanto opened this issue Aug 10, 2021 · 2 comments

Comments

@davidszanto
Copy link

Hi,

Is there a recommended way of implementing migration? I use sequelizer, which is capable of generating migration scripts but is there a way to integrate is with moleculer?

@devalexandre
Copy link
Contributor

wouldn't it be safer to use migrations with sequelize-cli?
You can also run the script after after the service, but I don't recommend it.

@drone12
Copy link

drone12 commented Jan 19, 2022

Hi @davidszanto ,

For sequelize migrations you have 2 options, running them programmatically or during CICD pipeline. To run them programmatically you should use umzug library.

We added a mixin called dbExtensions.js and inside we have a migrate() method with the following code:

const sequelize = this.adapter.db;
const umzug = new Umzug({
migrations: {
// indicates the folder containing the migration .js files
path: path.join(__dirname, "../db/migrations/" + this.name),
// inject sequelize's QueryInterface in the migrations
params: [sequelize.getQueryInterface()],
},
storage: "sequelize",
storageOptions: {
sequelize: sequelize,
},
});
await umzug.up();
console.log("Migrated " + this.name);
return true;

and then you add the mixin to service that you want to migrate and the following code:

async afterConnected() {
await this.migrate();
},

Keep in mind that there are some inter-version issues so I am giving you the version of umzug that we currently use:

"umzug": "2.3.0",

This approach carries with it various potential problems, one of them being not able to do canary deployments or run multiple service versions at the same time. That is a whole other can of worms that you need to investigate. If you approach this carefully it will work fine in production environment (we have a daily, hourly backup PLUS before update backup).

For CICD migrations it depends of your pipeline and CICD strategy and thus is way out of scope of github comment.

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

3 participants