mongock | Lightweight MongoDB migration tool for Java | Microservice library
kandi X-RAY | mongock Summary
kandi X-RAY | mongock Summary
Lightweight MongoDB migration tool for Java
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Executes the migration
- Execute the change set
- Prepare change log service
- Rollback changes if present
- Ensures the default lock is true
- Refreshes the lock
- Handle lock exception
- Wait for the lock to be acquired
- Get executor by operation
- Sets the runner if it is enabled
- Returns a new MongoSync4Driver with the given number of times
- Compares two triple objects
- Returns a copy of this database
- Returns a new object with lock strategy
- Executes the given operation in a new transaction
- Returns a copy of this driver
- Returns the set of client session - related dependencies
- Create an instance of the specified type
- Executes the given operation in a transaction
- Returns a string representation of the change set item
- Acquires the default lock
- Waits until the specified table is not active
- Executes migration
- Compare by order
- This method executes the given runnable in a transaction
- Starts the lock daemon
mongock Key Features
mongock Examples and Code Snippets
Community Discussions
Trending Discussions on mongock
QUESTION
Please tell me how can I fulfill the following condition - if the time in the info.startDate field is not equal to 00 hours, increase the date (2021-05-27) by 1 day ahead, set the time to 00:00:00.000Z. I tried to do it clumsily, through Mongock, getting all the elements of the collection and doing a check through LocalDateTime, but my heap overflowed, which is logical because the collection is large. How can I do this through Mongock or at least a manual request to MongoDB. So far I've only written this:
...ANSWER
Answered 2022-Feb-27 at 18:47If you're using Mongo version 5.0+ then you can use $dateTrunc and $dateAdd to achieve this quite easily, like so:
QUESTION
We recently migrated from MongoBee to Mongock, and with Mongock 5 version the @ChangeLog
and @ChangeSet
are depricated. Writing the @ChangeUnit
is easy enough and rollback methods are very helpful.
However, I'm unable to figure out how to write a test simulating the migration in test DB and validating the changes in DB, as there are @BeforeExecution
, @RollbackBeforeExecution
, @Execution
and @RollbackExecution
attributes or lifecycle methods in a @ChangeUnit.
Earlier, I used to just call the method with @ChangeSet
annotation like
ANSWER
Answered 2022-Feb-17 at 10:48In the new version 5, the basic change is that a ChangeUnit holds the unit of execution. That's normally done in the method annotated with @Execution
, so the first approach is just doing the same you are doing but calling the @Execution
method:
QUESTION
I'm using Mongock to init a MongoDb database with some configs, An array of authorized users by calling system. {systemId: 'a', authorizedUsers: ['user1', user2]}
.
My problem come from the fact that in dev user1 and user2 are authorized but in prod only user1 is authorized.
- is it better to add an env property
{env: 'dev', systemId: 'a', authorizedUsers: ['user1', user2]}
and use it as a filter when we search for authorized users ? The downside is to have different configs available in DB and we need to choose the right one. - Or is it better to read in Mongock init function (the ChangeSet) the env where Mongock is running on and init the right data ? The downside is that we can't test the correctness of the init before to be in the right environnement
- A better solution ?
Thank you in advance for your responses
Edit to clarify the two options
- Option 1
ANSWER
Answered 2021-Jul-05 at 09:15If I am not wrong, I see two parts in this problem
- Where to store the user's configuration/how to pass it to Mongock
- Based on that configuration, initialise your MongoDB database.
I guess we both agree the second step is clear: Mongock somehow takes the configuration and performs the migration.
Regarding of how to provide that configuration to Mongock, in your first option you mention to add a field for the environment, right? Are you saying you have the same database for production and dev environments? In my opinion, unless you have a really good reason for it, I wouldn't recommend that. In my ideal world, you would have a database per environment, so that field for environment is not needed. Mongock just needs to take the information from that collection that has been previously added.
Then the issue is how that configuration data about users is added to the database. If it can be hardcoded from your java code you can easily do it in Mongock. If you are using Spring you can use @Profile to talle which changeLog execute in which environment.
If not, another idea is passing those users as environment variables or configuration. If you are using kubernetes, ansible of that kind of tool, providing it by environment variable is not a bad idea. But I guess I don't have enough information to get more committed with any option.
QUESTION
I'm using the following pom in my spring boot app:
...ANSWER
Answered 2021-Apr-28 at 17:08That's due to incompatible versions.
As you can see in this compatibility table, the version of Mongock you are using is compatible with spring-data 3.x, but you are using 2.x instead.
Mongock provides two drivers for spring-data, mongodb-springdata-v3-driver, for spring-data 3.x and mongodb-springdata-v2-drver for spring-data 2.2.X
I have a couple of suggestions:
- Either upgrade your spring-data dependency to 3.x, or use mongodb-springdata-v2-drver(first one recommended)
- Use dependency management so you don't need to specify the version for each dependency. You can do it for spring and for Mongock
With that you should fix your issue ;)
QUESTION
I'm running a spring boot 2.4.3 app and I have a mongodb 4 cluster which supports transactions(I run it locally using run-rs). I'm also using spring data mongodb. I'm trying to integrate mongock(latest version - 4.3.8) for db migrations but I face a problem, which I can't resolve. As we know the the latest version of mongock is using transactions by default. I have the following changeset which just creates a mongodb collection for one of my entities.
...ANSWER
Answered 2021-Apr-15 at 12:29As you have mentioned, MongoDB transaction model has some limitations. Please see official MongoDB documentation.
Having that into account, you cannot wrap that operation in a transaction, so you cannot use it in a standard ChangeLog, if you are using transactions.
However, the good new i that, as always :D, Mongock team thought about it and will provide a mechanism for pre-transactions operations.
We expect to have a release candidate ready around June. However, if this is an urgent need, please send us an email to dev@cloudyrock.io
and we'll try to find a solution.
QUESTION
Mongock looks very promising. We want to use it inside a kubernetes service that has multiple replicas that run in parallel.
We are hoping that when our service is deployed, the first replica will acquire the mongockLock and all of its ChangeLogs/ChangeSets will be completed before the other replicas attempt to run them.
We have a single instance of mongodb running in our kubernetes environment, and we want the mongock ChangeLogs/ChangeSets to execute only once.
Will the mongockLock guarantee that only one replica will run the ChangeLogs/ChangeSets to completion?
Or do I need to enable transactions (or some other configuration)?
...ANSWER
Answered 2021-Feb-23 at 10:04I am going to provide the short answer first and then the long one. I suggest you to read the long one too in order to understand it properly.
Short answerBy default, Mongock guarantees that the ChangeLogs/changeSets will be run only by one pod at a time. The one owning the lock.
Long answerWhat really happens behind the scenes(if it's not configured otherwise) is that when a pod takes the lock, the others will try to acquire it too, but they can't, so they are forced to wait for a while(configurable, but 4 mins by default)as many times as the lock is configured(3 times by default). After this, if i's not able to acquire it and there is still pending changes to apply, Mongock will throw an MongockException, which should mean the JVM startup fail(what happens by default in Spring).
This is fine in Kubernetes, because it ensures it will restart the pods. So now, assuming the pods start again and changeLogs/changeSets are already applied, the pods start successfully because they don't even need to acquire the lock as there aren't pending changes to apply.
Potential problem with MongoDB without transaction support and Frameworks like SpringNow, assuming the lock and the mutual exclusion is clear, I'd like to point out a potential issue that needs to be mitigated by the the changeLog/changeSet design.
This issue applies if you are in an environment such as Kubernetes, which has a pod initialisation time, your migration take longer than that initialisation time an the Mongock process is executed before the pod becomes ready/health(and it's a condition for it). This last condition is highly desired as it ensures the application runs with the right version of the data.
In this situation imagine the Pod starts the Mongock process. After the Kubernetes initialisation time, the process is still not finished, but Kubernetes stops the JVM abruptly. This means that some changeSets were successfully executed, some other not even started(no problem, they will be processed in the next attempt), but one changeSet was partially executed and marked as not done. This is the potential issue. The next time Mongock runs, it will see the changeSet as pending and it will execute it from the beginning. If you haven't designed your changeLogs/changeSets accordingly, you may experience some unexpected results because some part of the data process covered by that changeSet has already taken place and it will happen again.
This, somehow needs to be mitigated. Either with the help of mechanisms like transactions, with a changeLog/changeSet design that takes this into account or both.
Mongock currently provides transactions with “all or nothing”, but it doesn’t really help much as it will retry every time from scratch and will probably end up in an infinite loop. The next version 5 will provide transactions per ChangeLogs and changeSets, which together with good organisation, is the right solution for this.
Meanwhile this issue can be addressed by following this design suggestions.
QUESTION
My application uses mongock 4.1.19 and when ever there is a changeSet with runAlways=true, there are duplicate entries getting created in the dbchangelog collection.
the below line does not seem to consider already executed case and may be resulting in duplicate changelog entries
Any pointers on how this can be addressed
Below Logger logged at startup - RE-APPLIED - ChangeEntry{...}
...ANSWER
Answered 2021-Mar-31 at 06:48It's not really duplicated. It creates a changelog entry per execution.
However, we understand this is not the more common desired behaviour, we are releasing a bugfix(4.3.8) for version 4 in the next days, probably today.
In version 5, which is under development, we'll keep this by default, plus updating the last_execution
field we'll add, and add the option to insert a new entry per execution if desired.
QUESTION
I am using Mongck to migrate data in my Spring Boot application. Spring Boot application runs perfectly locally. But when running on docker, the following error occurs:
...ANSWER
Answered 2021-Mar-14 at 22:30As discussed in this Github issue, the issue was resolved by upgrading to version 4.3.7.
QUESTION
I have a Collection of documents that looks like:
...ANSWER
Answered 2021-Feb-26 at 23:06Anything you can do with normal MongoTemplate, you can do it as well with MongockTemplate.
In this case, you can tune the UpdateOptions and some other stuff, but it can be as easy as
QUESTION
We have an application that runs as a 24x7 service in Kubernetes, and we can't take it down to run our migration scripts. I just want to verify that the mongock framework won't interfere with our application's operations - for instance, by locking a collection for an excessively long period of time.
I know the question sounds very broad, as the impacts/effects of a migration depend on the code we write in our ChangeLog/ChangeSets.
But I'm wondering whether the mongock framework itself has any effect on mongo collections other than its own internal collections (mongockChangeLog and mongockLock)
For instance, does mongock hold any locks on mongo collections other than its own?
I'm assuming that the lock owned by mongockLock won't have an effect on any collection other than mongockChangeLog.
Likewise, when transactions are enabled, does mongock own any transactions that might impact or affect the transactions owned by our code?
...ANSWER
Answered 2021-Feb-26 at 11:39Your institution is right. Mongock's lock is just internal. Also it's worthy knowing that you can configure how long this lock is taken for, how long Mongock waits for it, if it cannot acquire it and how long it will be trying. All that it's in the Mongock's documentation
Speaking about the transactions, Mongock delegates completely the transaction to the underneath framework. I don't fully understand your question, but I can explain which type of transactions are offered by Mongock and you can have a better understanding of the impact.
- All or nothing: The entire Mongock process is wrapped in a transaction, if it fails at any point, the entire migration is rolled back.(currently provided)
- Per changeSet(method): Every changeSet successfully executed is committed. If it fails in the middle, it's rolled back.(Will be provided in version 5)
- Per changeLog(class): The entire changeLog class is wrapped in a transaction, so it will be committed only if all its changeSets are successfully executed.(Will be provided in version 5)
General speaking, the recommended way to organise your migrations is per changeLog. A changeLog should represent a migration step, it shouldn't be designed in a feature approach. For example if you have in your model Client and Bill, you shouldn't design a changeLog for Client and another for Bill, having in them the different migration you apply over the time. Instead, you should do it in such a way that a changeLog represent a migration step, affecting the entire model, so you can identify your migration history through your changeLogs. From this point of view, Mongock's best practice is to use per changeLog transactions
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install mongock
You can use mongock like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the mongock component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page