From 123cecc0c05bebb238018e02ad5fa0339d88d761 Mon Sep 17 00:00:00 2001 From: yadhuksp Date: Sun, 19 Jun 2022 13:56:52 +0530 Subject: [PATCH] add section to cover use of ChangeUnitConstructor --- site/v5/get-started/get-started.md | 5 ++++- site/v5/migration/migration.md | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/site/v5/get-started/get-started.md b/site/v5/get-started/get-started.md index 59e1149..b5632e5 100644 --- a/site/v5/get-started/get-started.md +++ b/site/v5/get-started/get-started.md @@ -70,6 +70,9 @@ public class ClientInitializerChange { private final MongoTemplate mongoTemplate; private final ThirPartyService thirdPartyService; + + /** This is the constructor to which mongock will inject dependencies **/ + @ChangeUnitConstructor public ClientInitializerChange(MongoTemplate mongoTemplate, ThirPartyService thirdPartyService) { this.mongoTemplate = mongoTemplate; @@ -146,4 +149,4 @@ APPLIED - ChangeEntry{"id"="client-initializer", "author"="mongock", "class"="Cl ## Examples -For code examples, visit the [resource page](/v5/resources) \ No newline at end of file +For code examples, visit the [resource page](/v5/resources) diff --git a/site/v5/migration/migration.md b/site/v5/migration/migration.md index 67a57b0..74b64d0 100644 --- a/site/v5/migration/migration.md +++ b/site/v5/migration/migration.md @@ -38,6 +38,18 @@ A migration is constituted by an ordered list of ChangeUnits. Each of the Change - On the other hand, in non-transactional environments, Mongock will try to provide an artificial transactional atmosphere by rolling back the failed change manually. 2. In targeted operations, such as `undo`, `upgrade`, etc., the ChangeUnit is the unit of the operation. For example, when performing an `undo` operation, it needs to specify the _ChangeUnitId_ until which all the ChangeUnits are reverted(inclusive). 3. A ChangeUnit has only one migration method, which is marked with the **@Execution** annotation, and a rollback method, annotated with **@RollbackExecution**. +------------------------------------------------------ + +## ChangeUnit Constructor + + +Every class marked as `@ChangeUnit` will be marked as a migration class and Mongock will inject dependencies to one of +the valid constructor as followed: + +- In a class with more than one valid constructor, It would be recommended with warning to have exactly one of the constructor to be marked with **@ChangeUnitConstructor**. + - If such class doesn't have any constructor with **ChangeUnitConstructor**, Mongock will choose the first for dependency injection, this will become error in version 6. +- A class with only one valid constructor can choose to skip annotating the constructor, but if possible to do so, would be recommended for clarity of code. + ------------------------------------------------------ @@ -81,6 +93,8 @@ public class MyMigrationChangeUnit { private final MongoTemplate template; + // Note this annotation is Optional here as only one valid constructor exist + @ChangeUnitConstructor public MyMigrationChangeUnit(MongoTemplate template) { this.template = template; }