|
| 1 | += Versioned Migration |
| 2 | + |
| 3 | +To keep the data in the store up-to-date, {product-name} utilizes https://github.com/xdev-software/micro-migration[XDEV's Micro-Migration]. |
| 4 | +This means the user can use versioning for the stored data and only apply changes for certain versions of data. |
| 5 | +This can be very useful specifically with build-pipelines. https://github.com/xdev-software/micro-migration#intro[More info at Micro-Migration...] |
| 6 | + |
| 7 | +== Implementation |
| 8 | + |
| 9 | +This can be easily achieved by either of these 3 methods: |
| 10 | + |
| 11 | +=== 1. Reflective Scripts |
| 12 | + |
| 13 | +Simply implement a new component with a specific pattern of naming, that extends the ``ReflectiveDataMigrationScript``. |
| 14 | + |
| 15 | +[source,java,title="https://github.com/xdev-software/spring-data-eclipse-store/blob/develop/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/complex/migration/v1_0_0_Init.java[Reflective example from complex demo]"] |
| 16 | +---- |
| 17 | +package software.xdev.spring.data.eclipse.store.demo.complex.migration; |
| 18 | +
|
| 19 | +import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.stereotype.Component; |
| 21 | +//... |
| 22 | +import software.xdev.spring.data.eclipse.store.repository.root.data.version.ReflectiveDataMigrationScript; |
| 23 | +
|
| 24 | +@Component |
| 25 | +public class v1_0_0_Init extends ReflectiveDataMigrationScript |
| 26 | +{ |
| 27 | + private final OwnerService service; |
| 28 | +
|
| 29 | + @Autowired |
| 30 | + public v1_0_0_Init(final OwnerService service) |
| 31 | + { |
| 32 | + this.service = service; |
| 33 | + } |
| 34 | +
|
| 35 | + @Override |
| 36 | + public void migrate(final Context<VersionedRoot, MigrationEmbeddedStorageManager> context) |
| 37 | + { |
| 38 | + this.service.createNewOwnerAndVisit("Mick", "Fleetwood", "Isabella"); |
| 39 | + } |
| 40 | +} |
| 41 | +---- |
| 42 | + |
| 43 | +Here the version number on which the data is updated on execution is derived from the class name. |
| 44 | + |
| 45 | +The ``MigrationVersion`` is stored in the root object in the data store. |
| 46 | +Therefore, the storage always knows on which version the current data is and the ``DataMigrater`` will only execute the newer scripts. |
| 47 | + |
| 48 | +The scripts are automatically registered by declaring them as ``@Component``s. |
| 49 | +That means that they can be anywhere as long as they are discovered by Spring as a component. |
| 50 | + |
| 51 | +=== 2. Custom Scripts |
| 52 | + |
| 53 | +Implementing a script without special naming is possible by implementing the |
| 54 | +``DataMigrationScript``. |
| 55 | + |
| 56 | +[source,java,title="https://github.com/xdev-software/spring-data-eclipse-store/blob/develop/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/complex/migration/CustomNameScript.java[Custom script example from complex demo]"] |
| 57 | +---- |
| 58 | +package software.xdev.spring.data.eclipse.store.demo.complex.migration; |
| 59 | +
|
| 60 | +import org.springframework.beans.factory.annotation.Autowired; |
| 61 | +import org.springframework.stereotype.Component; |
| 62 | +//... |
| 63 | +import software.xdev.spring.data.eclipse.store.repository.root.data.version.DataMigrationScript; |
| 64 | +
|
| 65 | +@Component |
| 66 | +public class CustomNameScriptAddOwner implements DataMigrationScript |
| 67 | +{ |
| 68 | + private final OwnerService service; |
| 69 | +
|
| 70 | + public CustomNameScriptAddOwner(@Autowired final OwnerService service) |
| 71 | + { |
| 72 | + this.service = service; |
| 73 | + } |
| 74 | +
|
| 75 | + @Override |
| 76 | + public MigrationVersion getTargetVersion() |
| 77 | + { |
| 78 | + return new MigrationVersion(1, 1, 0); |
| 79 | + } |
| 80 | +
|
| 81 | + @Override |
| 82 | + public void migrate(final Context<VersionedRoot, MigrationEmbeddedStorageManager> context) |
| 83 | + { |
| 84 | + this.service.createNewOwnerAndVisit("John", "McVie", "Ivan"); |
| 85 | + } |
| 86 | +} |
| 87 | +---- |
| 88 | + |
| 89 | +The version number must be returned explicitly in the ``#getTargetVersion``-method. |
| 90 | + |
| 91 | +=== 3. Custom Migrater |
| 92 | + |
| 93 | +If more customization is needed it is also possible to replace the ``DataMigrater`` completely and implement your own ``MicroMigrater``. |
| 94 | +This should only be used if necessary since it adds a lot of complexity to the code. |
| 95 | + |
| 96 | +[source,java,title="https://github.com/xdev-software/spring-data-eclipse-store/blob/develop/spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/data/migration/with/migrater/CustomMigrater.java[Custom migrater from tests]"] |
| 97 | +---- |
| 98 | +package software.xdev.spring.data.eclipse.store.integration.isolated.tests.data.migration.with.migrater; |
| 99 | +
|
| 100 | +import org.springframework.beans.factory.annotation.Autowired; |
| 101 | +import org.springframework.stereotype.Component; |
| 102 | +import software.xdev.micromigration.migrater.ExplicitMigrater; |
| 103 | +import software.xdev.micromigration.migrater.MicroMigrater; |
| 104 | +//... |
| 105 | +
|
| 106 | +@Component |
| 107 | +public class CustomMigrater implements MicroMigrater |
| 108 | +{ |
| 109 | +private final ExplicitMigrater explicitMigrater; |
| 110 | +
|
| 111 | + @Autowired |
| 112 | + public CustomMigrater(final PersistedEntityRepository repository) |
| 113 | + { |
| 114 | + this.explicitMigrater = new ExplicitMigrater(new v1_0_0_Init(repository)); |
| 115 | + } |
| 116 | +---- |
0 commit comments