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

Prevent migrations from running twice #30

Closed
simplenotezy opened this issue Mar 29, 2022 · 8 comments
Closed

Prevent migrations from running twice #30

simplenotezy opened this issue Mar 29, 2022 · 8 comments

Comments

@simplenotezy
Copy link

First of all; thanks for great work put into this extension. Just what we needed!

Is there a way to prevent migrations from running twice?

If you have a migration job that takes a while to execute, and run migrate:up in a separate environment, it will re-run the existing (i.e. already running) migration.

E.g. when having a multi container setup in Kubernetes this feature to prevent migrations from running twice (i.e. before the other migration finished) would be helpful.

@qqilihq
Copy link
Member

qqilihq commented Mar 29, 2022

Hi there,

thank you for the kind words!

I get your use case, and in fact we faced the same challenge. My solution was to have a dedicated lock entry in the database, which would prevent more than one migration running simultaneously. Once the migration is finished, the lock will be removed, the next instance would continue (simply with no further migrations executed), and so on.

The code for that currently exists in our private code base, but I'd be fine with contributing it to this plugin. Let me have a look later today and get back here!

qqilihq added a commit that referenced this issue Mar 29, 2022
@qqilihq
Copy link
Member

qqilihq commented Mar 29, 2022

@simplenotezy I've copied our solution (and added tests). If you already want to have a look, feel free :-) I'm on the run now, but I can release this to NPM tomorrow.

@simplenotezy
Copy link
Author

That's really cool!! And thanks for prompt reply. I'll look into it.

Just curious: where in your k8s setup do you run migrations?

@qqilihq
Copy link
Member

qqilihq commented Mar 30, 2022

@simplenotezy I’ve pushed these changes to NPM as 3.1.0-beta.0 -- please do let me know how this works for you!

Just curious: where in your k8s setup do you run migrations?

Disappointing fact: We're not running on K8s at all!

We “orchestrate” our nodes manually and via scripts, originally starting from one single container instance back then to now now several distributed across a cluster.

To update containers we just pull them automatically from a registry, restart them, and upon container start, the migrations are being run. Dead simplea and boring, but reliable and sufficient for us 🙂

@simplenotezy
Copy link
Author

@qqilihq wow, that was awesome. Really quick!

I tried, pulling it down, and then running the same migration in two seperate terminal windows, but I still do see my console log from the "up" appear twice.

Do I need to configure something?

@simplenotezy I’ve pushed these changes to NPM as 3.1.0-beta.0 -- please do let me know how this works for you!

Just curious: where in your k8s setup do you run migrations?

Disappointing fact: We're not running on K8s at all!

We “orchestrate” our nodes manually and via scripts, originally starting from one single container instance back then to now now several distributed across a cluster.

To update containers we just pull them automatically from a registry, restart them, and upon container start, the migrations are being run. Dead simplea and boring, but reliable and sufficient for us 🙂

Ah well, as long as it works and you're happy with it - why fix it! 😊 Kubernetes is really fun though.

@qqilihq
Copy link
Member

qqilihq commented Mar 31, 2022

@simplenotezy

Do I need to configure something?

You'll explicitly need to set the lockCollectionName so that locking will happen (it's off by default to remain backwards-compatible). See the updated readme in the current branch here: https://github.com/NodePit/node-migrate-state-store-mongodb/tree/lock

Let me know if this helps!

@qqilihq
Copy link
Member

qqilihq commented Apr 5, 2022

@simplenotezy Any feedback?

@simplenotezy
Copy link
Author

@qqilihq Very sorry for late response; been swamped lately. I haven't forgot about it and will test soon.

@qqilihq qqilihq closed this as completed Mar 25, 2023
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

2 participants