Skip to content

Commit 7528d8d

Browse files
Made the DataMigrater optional
1 parent 706b340 commit 7528d8d

File tree

16 files changed

+332
-21
lines changed

16 files changed

+332
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 2.4.2
22

33
* Updated org.springframework.boot.version to v3.4.1
4+
* Added support for the [micro-migration-Framework](https://github.com/xdev-software/micro-migration)
45

56
# 2.4.1
67

spring-data-eclipse-store/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<jakarta.el-api.version>6.0.1</jakarta.el-api.version>
5959
<expressly.version>6.0.0-M1</expressly.version>
6060
<hibernate-core.version>6.6.4.Final</hibernate-core.version>
61+
<micro-migration.version>3.0.1</micro-migration.version>
6162
</properties>
6263

6364
<repositories>
@@ -156,7 +157,7 @@
156157
<dependency>
157158
<groupId>software.xdev</groupId>
158159
<artifactId>micro-migration</artifactId>
159-
<version>3.0.1</version>
160+
<version>${micro-migration.version}</version>
160161
<exclusions>
161162
<exclusion>
162163
<artifactId>storage-embedded</artifactId>

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/config/EclipseStoreClientConfiguration.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.beans.factory.annotation.Autowired;
3131
import org.springframework.beans.factory.annotation.Value;
3232
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
33-
import org.springframework.context.ApplicationContext;
3433
import org.springframework.context.annotation.Bean;
3534
import org.springframework.context.annotation.ComponentScan;
3635
import org.springframework.context.annotation.Configuration;
@@ -229,7 +228,6 @@ public Validator getValidator()
229228
@EventListener
230229
public void migrateDataOnContextStarted(final ContextRefreshedEvent event)
231230
{
232-
final ApplicationContext applicationContext = event.getApplicationContext();
233231
try
234232
{
235233
final MicroMigrater dataMigrater = event.getApplicationContext().getBean(MicroMigrater.class);

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/root/data/version/DataMigrater.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,21 @@
2020
import java.util.TreeSet;
2121
import java.util.function.Consumer;
2222

23-
import org.springframework.beans.factory.annotation.Autowired;
24-
import org.springframework.stereotype.Component;
25-
2623
import software.xdev.micromigration.migrater.ExplicitMigrater;
2724
import software.xdev.micromigration.migrater.MicroMigrater;
2825
import software.xdev.micromigration.notification.ScriptExecutionNotificationWithScriptReference;
2926
import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
3027
import software.xdev.micromigration.version.MigrationVersion;
3128
import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationEmbeddedStorageManager;
3229

33-
@Component
3430
public class DataMigrater implements MicroMigrater
3531
{
3632
private final List<DataMigrationScript> scripts;
3733
private ExplicitMigrater explicitMigrater;
3834

39-
@Autowired
40-
public DataMigrater(final Optional<List<DataMigrationScript>> scripts)
35+
public DataMigrater(final List<DataMigrationScript> scripts)
4136
{
42-
this.scripts = scripts.orElse(List.of());
37+
this.scripts = scripts;
4338
}
4439

4540
private ExplicitMigrater ensureExplicitMigrater()
@@ -63,6 +58,7 @@ private boolean isUseful()
6358
{
6459
return this.ensureExplicitMigrater().getSortedScripts();
6560
}
61+
// noinspection SortedCollectionWithNonComparableKeys
6662
return new TreeSet<>();
6763
}
6864

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.repository.root.data.version;
17+
18+
import java.util.List;
19+
import java.util.Optional;
20+
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.stereotype.Component;
24+
25+
import software.xdev.micromigration.migrater.MicroMigrater;
26+
27+
28+
@Component
29+
public class DataMigraterFactory
30+
{
31+
@Bean
32+
@ConditionalOnMissingBean(MicroMigrater.class)
33+
public MicroMigrater getDataMigrater(final Optional<List<DataMigrationScript>> scripts)
34+
{
35+
return new DataMigrater(scripts.orElse(List.of()));
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.data.migration.with.migrater;
17+
18+
import java.util.TreeSet;
19+
import java.util.function.Consumer;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.stereotype.Component;
23+
24+
import software.xdev.micromigration.migrater.ExplicitMigrater;
25+
import software.xdev.micromigration.migrater.MicroMigrater;
26+
import software.xdev.micromigration.notification.ScriptExecutionNotificationWithScriptReference;
27+
import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
28+
import software.xdev.micromigration.version.MigrationVersion;
29+
import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationEmbeddedStorageManager;
30+
31+
32+
@Component
33+
public class CustomMigrater implements MicroMigrater
34+
{
35+
private ExplicitMigrater explicitMigrater;
36+
final PersistedEntityRepository repository;
37+
38+
@Autowired
39+
public CustomMigrater(final PersistedEntityRepository repository)
40+
{
41+
this.repository = repository;
42+
}
43+
44+
private ExplicitMigrater ensureExplicitMigrater()
45+
{
46+
if(this.explicitMigrater == null)
47+
{
48+
this.explicitMigrater = new ExplicitMigrater(new v1_0_0_Init(this.repository));
49+
}
50+
return this.explicitMigrater;
51+
}
52+
53+
@Override
54+
public TreeSet<? extends VersionAgnosticMigrationScript<?, ?>> getSortedScripts()
55+
{
56+
return this.ensureExplicitMigrater().getSortedScripts();
57+
}
58+
59+
@Override
60+
public <E extends VersionAgnosticMigrationEmbeddedStorageManager<?, ?>> MigrationVersion migrateToNewest(
61+
final MigrationVersion fromVersion,
62+
final E storageManager,
63+
final Object root)
64+
{
65+
return this.ensureExplicitMigrater().migrateToNewest(fromVersion, storageManager, root);
66+
}
67+
68+
@Override
69+
public <E extends VersionAgnosticMigrationEmbeddedStorageManager<?, ?>> MigrationVersion migrateToVersion(
70+
final MigrationVersion fromVersion,
71+
final MigrationVersion targetVersion,
72+
final E storageManager,
73+
final Object objectToMigrate)
74+
{
75+
return this.ensureExplicitMigrater()
76+
.migrateToVersion(fromVersion, targetVersion, storageManager, objectToMigrate);
77+
}
78+
79+
@Override
80+
public void registerNotificationConsumer(
81+
final Consumer<ScriptExecutionNotificationWithScriptReference> notificationConsumer
82+
)
83+
{
84+
this.ensureExplicitMigrater().registerNotificationConsumer(notificationConsumer);
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.data.migration.with.migrater;
17+
18+
import org.junit.jupiter.api.Assertions;
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.test.context.ContextConfiguration;
22+
23+
import software.xdev.micromigration.version.MigrationVersion;
24+
import software.xdev.spring.data.eclipse.store.helper.TestUtil;
25+
import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations;
26+
27+
28+
@IsolatedTestAnnotations
29+
@ContextConfiguration(classes = {DataMigrationWithMigraterTestConfiguration.class})
30+
class DataMigrationWithMigraterTest
31+
{
32+
@Autowired
33+
private DataMigrationWithMigraterTestConfiguration configuration;
34+
@Autowired
35+
private PersistedEntityRepository repository;
36+
37+
@Test
38+
void assertUpdateV1Executed()
39+
{
40+
Assertions.assertEquals(1, this.repository.count());
41+
Assertions.assertEquals(
42+
new MigrationVersion(1, 0, 0),
43+
this.configuration.getStorageInstance().getRoot().getDataVersion().getVersion());
44+
}
45+
46+
@Test
47+
void assertNotUpdatedAfterMigration()
48+
{
49+
TestUtil.doBeforeAndAfterRestartOfDatastore(
50+
this.configuration,
51+
() -> {
52+
Assertions.assertEquals(1, this.repository.count());
53+
Assertions.assertEquals(
54+
new MigrationVersion(1, 0, 0),
55+
this.configuration.getStorageInstance().getRoot().getDataVersion().getVersion());
56+
}
57+
);
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.data.migration.with.migrater;
17+
18+
import org.eclipse.serializer.reflect.ClassLoaderProvider;
19+
import org.eclipse.store.integrations.spring.boot.types.configuration.EclipseStoreProperties;
20+
import org.eclipse.store.integrations.spring.boot.types.factories.EmbeddedStorageFoundationFactory;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.ComponentScan;
23+
import org.springframework.context.annotation.Configuration;
24+
25+
import software.xdev.spring.data.eclipse.store.integration.TestConfiguration;
26+
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
27+
28+
29+
@ComponentScan({"software.xdev.spring.data.eclipse.store.integration.isolated.tests.data.migration.with.migrater"})
30+
@Configuration
31+
@EnableEclipseStoreRepositories
32+
public class DataMigrationWithMigraterTestConfiguration extends TestConfiguration
33+
{
34+
@Autowired
35+
protected DataMigrationWithMigraterTestConfiguration(
36+
final EclipseStoreProperties defaultEclipseStoreProperties,
37+
final EmbeddedStorageFoundationFactory defaultEclipseStoreProvider,
38+
final ClassLoaderProvider classLoaderProvider)
39+
{
40+
super(defaultEclipseStoreProperties, defaultEclipseStoreProvider, classLoaderProvider);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.data.migration.with.migrater;
17+
18+
import jakarta.persistence.GeneratedValue;
19+
import jakarta.persistence.GenerationType;
20+
import jakarta.persistence.Id;
21+
22+
23+
public class PersistedEntity
24+
{
25+
@Id
26+
@GeneratedValue(strategy = GenerationType.AUTO)
27+
private int id;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.data.migration.with.migrater;
17+
18+
import org.springframework.data.repository.CrudRepository;
19+
20+
21+
public interface PersistedEntityRepository extends CrudRepository<PersistedEntity, Integer>
22+
{
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.data.migration.with.migrater;
17+
18+
import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19+
import software.xdev.micromigration.scripts.Context;
20+
import software.xdev.spring.data.eclipse.store.repository.root.VersionedRoot;
21+
import software.xdev.spring.data.eclipse.store.repository.root.data.version.DataMigrationScript;
22+
23+
24+
@SuppressWarnings("CheckStyle")
25+
public class v1_0_0_Init extends DataMigrationScript
26+
{
27+
private final PersistedEntityRepository
28+
repository;
29+
30+
public v1_0_0_Init(final PersistedEntityRepository repository)
31+
{
32+
this.repository = repository;
33+
}
34+
35+
@Override
36+
public void migrate(final Context<VersionedRoot, MigrationEmbeddedStorageManager> context)
37+
{
38+
this.repository.save(new PersistedEntity());
39+
}
40+
}

0 commit comments

Comments
 (0)