|
| 1 | += Migration from EclipseStore |
| 2 | + |
| 3 | +Given the flexibility of EclipseStore and its diverse use cases, there is **no universal, one-size-fits-all solution for migrating data** between the two systems. |
| 4 | + |
| 5 | +The first step is to define the appropriate entities and repositories tailored to your requirements, ensuring they align with the existing data structure in EclipseStore. |
| 6 | + |
| 7 | +Next, the data must be transferred to the Spring-Data-Eclipse-Store storage. |
| 8 | +To facilitate this, both EclipseStore and {product-name} must run within the same JVM (Java Virtual Machine). |
| 9 | +This configuration enables the developer to operate both systems simultaneously and transfer data by making simple save calls to the respective repositories. |
| 10 | + |
| 11 | +== Example |
| 12 | + |
| 13 | +=== 0. Status Quo |
| 14 | + |
| 15 | +Let's say that this is the current state of the code: |
| 16 | + |
| 17 | +[source,java,title="``package software.xdev.example.eclipse.store``"] |
| 18 | +---- |
| 19 | +public record Root(List<Owner> owners, List<Vet> vets){} |
| 20 | +public record Owner(String name, List<Pet> pets, List<Visit> visits){} |
| 21 | +public record Pet(String name){} |
| 22 | +public record Vet(String name, List<Owner> clients){} |
| 23 | +public record Visit(LocalDate date, Owner owner, Pet pet){} |
| 24 | +---- |
| 25 | + |
| 26 | +In this case ``Root`` is the root-object of EclipseStore. |
| 27 | + |
| 28 | +=== 1. Building Repositories |
| 29 | + |
| 30 | +Now entities with its corresponding repositories must be created in the same project: |
| 31 | + |
| 32 | +[source,java,title="``package software.xdev.example.sdes.enitities``"] |
| 33 | +---- |
| 34 | + public class Owner{ |
| 35 | + @Id |
| 36 | + @GeneratedValue(strategy = GenerationType.AUTO) |
| 37 | + private long id; |
| 38 | + private final String name; |
| 39 | + private final List<Pet> pets; |
| 40 | + private final List<Visit> visits; |
| 41 | + //... |
| 42 | + } |
| 43 | + public class Vet{ |
| 44 | + @Id |
| 45 | + @GeneratedValue(strategy = GenerationType.AUTO) |
| 46 | + private long id; |
| 47 | + private final String name; |
| 48 | + private final List<Owner> clients; |
| 49 | + //... |
| 50 | + } |
| 51 | + public class Visit{ |
| 52 | + @Id |
| 53 | + @GeneratedValue(strategy = GenerationType.AUTO) |
| 54 | + private long id; |
| 55 | + private final LocalDate date; |
| 56 | + private final Owner owner; |
| 57 | + private final Pet pet; |
| 58 | + //... |
| 59 | + } |
| 60 | + public class Pet{ |
| 61 | + @Id |
| 62 | + @GeneratedValue(strategy = GenerationType.AUTO) |
| 63 | + private long id; |
| 64 | + private final String name; |
| 65 | + //... |
| 66 | + } |
| 67 | +---- |
| 68 | + |
| 69 | +[source,java,title="``package software.xdev.example.sdes.repositories``"] |
| 70 | +---- |
| 71 | + public interface OwnerRepository extends CrudRepository<Owner, Long>{} |
| 72 | + public interface VetRepository extends CrudRepository<Vet, Long>{} |
| 73 | + public interface VisitRepository extends CrudRepository<Visit, Long>{} |
| 74 | + public interface PetRepository extends CrudRepository<Pet, Long>{} |
| 75 | +---- |
| 76 | + |
| 77 | +Note that the ``root`` object is not needed in the new structure. |
| 78 | +Since we want to transfer the data in the next step, it is good practice to keep these classes in the same project but in separate packages. |
| 79 | +It is recommended that **ids** fields are defined to improve performance. |
| 80 | + |
| 81 | +=== 3. Copy Data |
| 82 | + |
| 83 | +Now it's time to copy the actual data. |
| 84 | +For that we need to start up the old EclipseStore storage additionally to the {product-name} storage. |
| 85 | +Then we simply iterate over the existing data, repackage it in our new objects and store them through the repositories. |
| 86 | + |
| 87 | +[source,java,title="``package software.xdev.example.sdes.repositories``"] |
| 88 | +---- |
| 89 | +package software.xdev.example; |
| 90 | +
|
| 91 | +import org.eclipse.store.storage.embedded.types.EmbeddedStorage; |
| 92 | +import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager; |
| 93 | +import org.springframework.beans.factory.annotation.Autowired; |
| 94 | +import org.springframework.context.event.ContextRefreshedEvent; |
| 95 | +import org.springframework.context.event.EventListener; |
| 96 | +import org.springframework.stereotype.Service; |
| 97 | +import software.xdev.example.sdes.repositories.*; |
| 98 | +
|
| 99 | +@Service |
| 100 | +public class EclipseStoreDataMigrater |
| 101 | +{ |
| 102 | + OwnerRepository ownerRepository; |
| 103 | + VetRepository vetRepository; |
| 104 | + VisitRepository visitRepository; |
| 105 | + PetRepository petRepository; |
| 106 | +
|
| 107 | + @Autowired |
| 108 | + public EclipseStoreDataMigrater(OwnerRepository ownerRepository, VetRepository vetRepository, VisitRepository visitRepository, PetRepository petRepository |
| 109 | + ) |
| 110 | + { |
| 111 | + this.ownerRepository = ownerRepository; |
| 112 | + this.vetRepository = vetRepository; |
| 113 | + this.visitRepository = visitRepository; |
| 114 | + this.petRepository = petRepository; |
| 115 | + } |
| 116 | +
|
| 117 | + @EventListener |
| 118 | + public void migrateData(final ContextRefreshedEvent event) |
| 119 | + { |
| 120 | + final software.xdev.example.eclipse.store.Root root = new software.xdev.example.eclipse.store.Root(null, null); |
| 121 | + try(final EmbeddedStorageManager storageManager = EmbeddedStorage.start(root)) |
| 122 | + { |
| 123 | + root.owners.forEach(owner -> { |
| 124 | + owner.pets.forEach(pet -> this.petRepository.save(new software.xdev.example.sdes.entities.Pet(pet.name))); |
| 125 | + //... |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | +---- |
| 131 | + |
| 132 | +This is very simplified but shows the general strategy to migrate data from EclipseStore to {product-name}. |
0 commit comments