[DEV] correct migration engine and add many test
This commit is contained in:
parent
7fd6502e60
commit
01de431f5a
@ -75,13 +75,48 @@ public class MigrationEngine {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Process the automatic migration of the system The function wait the Administrator intervention to correct the bug.
|
||||||
|
* @param config SQL connection for the migration.
|
||||||
|
* @throws InterruptedException user interrupt the migration */
|
||||||
|
public void migrateWaitAdmin(final DBConfig config) throws InterruptedException {
|
||||||
|
try {
|
||||||
|
migrateErrorThrow(config);
|
||||||
|
} catch (final Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
while (true) {
|
||||||
|
LOGGER.error("Fail to create the local DB SQL model for migaration ==> wait administrator interventions");
|
||||||
|
Thread.sleep(60 * 60 * 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Process the automatic migration of the system
|
/** Process the automatic migration of the system
|
||||||
* @param config SQL connection for the migration
|
* @param config SQL connection for the migration
|
||||||
* @throws InterruptedException user interrupt the migration
|
|
||||||
* @throws IOException Error if access on the DB */
|
* @throws IOException Error if access on the DB */
|
||||||
public void migrate(final DBConfig config) throws InterruptedException, IOException {
|
public void migrateErrorThrow(final DBConfig config) throws MigrationException {
|
||||||
LOGGER.info("Execute migration ... [BEGIN]");
|
LOGGER.info("Execute migration ... [BEGIN]");
|
||||||
|
|
||||||
|
// check the integrity of the migrations:
|
||||||
|
for (final MigrationInterface elem : this.datas) {
|
||||||
|
if (elem == null) {
|
||||||
|
throw new MigrationException("Add a null migration");
|
||||||
|
}
|
||||||
|
if (elem == this.init) {
|
||||||
|
throw new MigrationException("Add a migration that is the initialization migration");
|
||||||
|
}
|
||||||
|
if (this.init != null && elem.getName().equals(this.init.getName())) {
|
||||||
|
throw new MigrationException("Two migration have the same name as initilaisation: " + elem.getName());
|
||||||
|
}
|
||||||
|
for (final MigrationInterface elemCheck : this.datas) {
|
||||||
|
if (elem == elemCheck) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (elem.getName().equals(elemCheck.getName())) {
|
||||||
|
throw new MigrationException("Two migration have the same name...: " + elem.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// STEP 1: Check the DB exist:
|
// STEP 1: Check the DB exist:
|
||||||
LOGGER.info("Verify existance of '{}'", config.getDbName());
|
LOGGER.info("Verify existance of '{}'", config.getDbName());
|
||||||
boolean exist = DataAccess.isDBExist(config.getDbName());
|
boolean exist = DataAccess.isDBExist(config.getDbName());
|
||||||
@ -94,7 +129,12 @@ public class MigrationEngine {
|
|||||||
while (!exist) {
|
while (!exist) {
|
||||||
LOGGER.error("DB: '{}' DOES NOT EXIST after trying to create one ", config.getDbName());
|
LOGGER.error("DB: '{}' DOES NOT EXIST after trying to create one ", config.getDbName());
|
||||||
LOGGER.error("Waiting administrator create a new one, we check after 30 seconds...");
|
LOGGER.error("Waiting administrator create a new one, we check after 30 seconds...");
|
||||||
Thread.sleep(30000);
|
try {
|
||||||
|
Thread.sleep(30000);
|
||||||
|
} catch (final InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
exist = DataAccess.isDBExist(config.getDbName());
|
exist = DataAccess.isDBExist(config.getDbName());
|
||||||
}
|
}
|
||||||
LOGGER.info("DB '{}' exist.", config.getDbName());
|
LOGGER.info("DB '{}' exist.", config.getDbName());
|
||||||
@ -109,57 +149,33 @@ public class MigrationEngine {
|
|||||||
sqlQuery = DataFactory.createTable(Migration.class);
|
sqlQuery = DataFactory.createTable(Migration.class);
|
||||||
} catch (final Exception ex) {
|
} catch (final Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
while (true) {
|
throw new MigrationException("Fail to create the local DB SQL model for migaration ==> wait administrator interventions");
|
||||||
LOGGER.error("Fail to create the local DB SQL model for migaration ==> wait administrator interventions");
|
|
||||||
Thread.sleep(60 * 60 * 1000);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
LOGGER.info("Create Table with : {}", sqlQuery.get(0));
|
LOGGER.info("Create Table with : {}", sqlQuery.get(0));
|
||||||
try {
|
try {
|
||||||
DataAccess.executeQuerry(sqlQuery.get(0));
|
DataAccess.executeQuerry(sqlQuery.get(0));
|
||||||
} catch (SQLException | IOException ex) {
|
} catch (SQLException | IOException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
while (true) {
|
throw new MigrationException("Fail to create the local DB model for migaration ==> wait administrator interventions");
|
||||||
LOGGER.error("Fail to create the local DB model for migaration ==> wait administrator interventions");
|
|
||||||
Thread.sleep(60 * 60 * 1000);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final Migration currentVersion = getCurrentVersion();
|
final Migration currentVersion = getCurrentVersion();
|
||||||
List<MigrationInterface> toApply = new ArrayList<>();
|
List<MigrationInterface> toApply = new ArrayList<>();
|
||||||
|
boolean needPlaceholder = false;
|
||||||
if (currentVersion == null) {
|
if (currentVersion == null) {
|
||||||
// This is a first migration
|
// This is a first migration
|
||||||
LOGGER.info("First installation of the system ==> Create the DB");
|
LOGGER.info("First installation of the system ==> Create the DB");
|
||||||
if (this.init == null) {
|
if (this.init == null) {
|
||||||
|
// No initialization class ==> manage a historical creation mode...
|
||||||
toApply = this.datas;
|
toApply = this.datas;
|
||||||
} else {
|
} else {
|
||||||
|
// Select Initialization class if it exist
|
||||||
toApply.add(this.init);
|
toApply.add(this.init);
|
||||||
}
|
needPlaceholder = true;
|
||||||
if (this.datas.size() == 0) {
|
|
||||||
// nothing to do the initialization model is alone and it is the first time
|
|
||||||
} else {
|
|
||||||
// we insert a placeholder to simulate all migration is well done.
|
|
||||||
final String placeholderName = this.datas.get(this.datas.size() - 1).getName();
|
|
||||||
Migration migrationResult = new Migration();
|
|
||||||
migrationResult.id = 1000L;
|
|
||||||
migrationResult.name = placeholderName;
|
|
||||||
migrationResult.stepId = 0;
|
|
||||||
migrationResult.terminated = true;
|
|
||||||
migrationResult.count = 0;
|
|
||||||
migrationResult.log = "Place-holder for first initialization";
|
|
||||||
try {
|
|
||||||
migrationResult = DataAccess.insert(migrationResult);
|
|
||||||
} catch (final Exception e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!currentVersion.terminated) {
|
if (!currentVersion.terminated) {
|
||||||
while (true) {
|
throw new MigrationException("An error occured in the last migration: '" + currentVersion.name + "' defect @" + currentVersion.stepId + "/" + currentVersion.count);
|
||||||
LOGGER.error("An error occured in the last migration: '{}' defect @{}/{} ==> wait administrator interventions", currentVersion.name, currentVersion.stepId, currentVersion.count);
|
|
||||||
Thread.sleep(60 * 60 * 1000);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
LOGGER.info("Upgrade the system Current version: {}", currentVersion.name);
|
LOGGER.info("Upgrade the system Current version: {}", currentVersion.name);
|
||||||
boolean find = this.init != null && this.init.getName() == currentVersion.name;
|
boolean find = this.init != null && this.init.getName() == currentVersion.name;
|
||||||
@ -177,16 +193,43 @@ public class MigrationEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final DBEntry entry = DBEntry.createInterface(config);
|
DBEntry entry;
|
||||||
final int id = 0;
|
try {
|
||||||
final int count = toApply.size();
|
entry = DBEntry.createInterface(config);
|
||||||
for (final MigrationInterface elem : toApply) {
|
final int id = 0;
|
||||||
migrateSingle(entry, elem, id, count);
|
final int count = toApply.size();
|
||||||
|
for (final MigrationInterface elem : toApply) {
|
||||||
|
migrateSingle(entry, elem, id, count);
|
||||||
|
}
|
||||||
|
} catch (final IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new MigrationException("An error occured in the migration (can not access to the DB): '" + currentVersion.name + "' defect @" + currentVersion.stepId + "/" + currentVersion.count);
|
||||||
|
}
|
||||||
|
if (needPlaceholder) {
|
||||||
|
if (this.datas.size() == 0) {
|
||||||
|
// No placeholder needed, the model have no migration in the current version...
|
||||||
|
} else {
|
||||||
|
// we insert a placeholder to simulate the last migration is well done.
|
||||||
|
final String placeholderName = this.datas.get(this.datas.size() - 1).getName();
|
||||||
|
Migration migrationResult = new Migration();
|
||||||
|
migrationResult.id = 1000L;
|
||||||
|
migrationResult.name = placeholderName;
|
||||||
|
migrationResult.stepId = 0;
|
||||||
|
migrationResult.terminated = true;
|
||||||
|
migrationResult.count = 0;
|
||||||
|
migrationResult.log = "Place-holder for first initialization";
|
||||||
|
try {
|
||||||
|
migrationResult = DataAccess.insert(migrationResult);
|
||||||
|
} catch (final Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
LOGGER.info("Execute migration ... [ END ]");
|
LOGGER.info("Execute migration ... [ END ]");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void migrateSingle(final DBEntry entry, final MigrationInterface elem, final int id, final int count) {
|
public void migrateSingle(final DBEntry entry, final MigrationInterface elem, final int id, final int count) throws MigrationException {
|
||||||
LOGGER.info("---------------------------------------------------------");
|
LOGGER.info("---------------------------------------------------------");
|
||||||
LOGGER.info("-- Migrate: [{}/{}] {} [BEGIN]", id, count, elem.getName());
|
LOGGER.info("-- Migrate: [{}/{}] {} [BEGIN]", id, count, elem.getName());
|
||||||
LOGGER.info("---------------------------------------------------------");
|
LOGGER.info("---------------------------------------------------------");
|
||||||
@ -220,16 +263,7 @@ public class MigrationEngine {
|
|||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
while (true) {
|
throw new MigrationException("An error occured in the migration (OUTSIDE detection): '" + migrationResult.name + "' defect @" + migrationResult.stepId + "/" + migrationResult.count);
|
||||||
LOGGER.error("An error occured in the migration (OUTSIDE detection): '{}' defect @{}/{} ==> wait administrator interventions", migrationResult.name, migrationResult.stepId,
|
|
||||||
migrationResult.count);
|
|
||||||
try {
|
|
||||||
Thread.sleep(60 * 60 * 1000);
|
|
||||||
} catch (final InterruptedException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
LOGGER.info("Migrate: [{}/{}] {} [ END ]", id, count, elem.getName());
|
LOGGER.info("Migrate: [{}/{}] {} [ END ]", id, count, elem.getName());
|
||||||
}
|
}
|
||||||
|
@ -4,4 +4,7 @@ public class MigrationException extends Exception {
|
|||||||
|
|
||||||
private static final long serialVersionUID = 20230502L;
|
private static final long serialVersionUID = 20230502L;
|
||||||
|
|
||||||
|
public MigrationException(final String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
package org.kar.archidata.model;
|
|
||||||
|
|
||||||
public class Migration extends GenericDataSoftDelete {
|
|
||||||
public String migrationId;
|
|
||||||
|
|
||||||
}
|
|
@ -5,7 +5,7 @@ import org.junit.jupiter.api.extension.ExecutionCondition;
|
|||||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||||
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
|
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
|
||||||
|
|
||||||
class StepwiseExtension implements ExecutionCondition, TestExecutionExceptionHandler {
|
public class StepwiseExtension implements ExecutionCondition, TestExecutionExceptionHandler {
|
||||||
@Override
|
@Override
|
||||||
public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext extensionContext) {
|
public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext extensionContext) {
|
||||||
final ExtensionContext.Namespace namespace = namespaceFor(extensionContext);
|
final ExtensionContext.Namespace namespace = namespaceFor(extensionContext);
|
||||||
|
@ -458,9 +458,9 @@ public class TestTypes {
|
|||||||
Assertions.assertNotNull(retrieve2.id);
|
Assertions.assertNotNull(retrieve2.id);
|
||||||
Assertions.assertEquals(insertedData.id, retrieve2.id);
|
Assertions.assertEquals(insertedData.id, retrieve2.id);
|
||||||
Assertions.assertNotNull(retrieve2.textData);
|
Assertions.assertNotNull(retrieve2.textData);
|
||||||
Assertions.assertEquals(retrieve.textData, retrieve2.textData);
|
Assertions.assertEquals("test 2", retrieve2.textData);
|
||||||
Assertions.assertNotNull(retrieve2.booleanData);
|
Assertions.assertNotNull(retrieve2.booleanData);
|
||||||
Assertions.assertEquals(retrieve.booleanData, retrieve2.booleanData);
|
Assertions.assertEquals(true, retrieve2.booleanData);
|
||||||
Assertions.assertNull(retrieve2.varcharData);
|
Assertions.assertNull(retrieve2.varcharData);
|
||||||
|
|
||||||
DataAccess.delete(TypesTable.class, insertedData.id);
|
DataAccess.delete(TypesTable.class, insertedData.id);
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
package test.kar.archidata.migration;
|
||||||
|
|
||||||
|
import org.kar.archidata.migration.MigrationSqlStep;
|
||||||
|
|
||||||
|
import test.kar.archidata.migration.model.TypesMigrationInitialisationCurrent;
|
||||||
|
|
||||||
|
class InitializationCurrent extends MigrationSqlStep {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Initialization";
|
||||||
|
}
|
||||||
|
|
||||||
|
public InitializationCurrent() throws Exception {
|
||||||
|
addClass(TypesMigrationInitialisationCurrent.class);
|
||||||
|
addAction("""
|
||||||
|
ALTER TABLE `TestTableMigration` AUTO_INCREMENT = 1000;
|
||||||
|
""", "mysql");
|
||||||
|
display();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package test.kar.archidata.migration;
|
||||||
|
|
||||||
|
import org.kar.archidata.migration.MigrationSqlStep;
|
||||||
|
|
||||||
|
import test.kar.archidata.migration.model.TypesMigrationInitialisationFirst;
|
||||||
|
|
||||||
|
class InitializationFirst extends MigrationSqlStep {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Initialization";
|
||||||
|
}
|
||||||
|
|
||||||
|
public InitializationFirst() throws Exception {
|
||||||
|
addClass(TypesMigrationInitialisationFirst.class);
|
||||||
|
addAction("""
|
||||||
|
ALTER TABLE `TestTableMigration` AUTO_INCREMENT = 1000;
|
||||||
|
""", "mysql");
|
||||||
|
display();
|
||||||
|
}
|
||||||
|
}
|
21
test/src/test/kar/archidata/migration/Migration1.java
Normal file
21
test/src/test/kar/archidata/migration/Migration1.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package test.kar.archidata.migration;
|
||||||
|
|
||||||
|
import org.kar.archidata.migration.MigrationSqlStep;
|
||||||
|
|
||||||
|
class Migration1 extends MigrationSqlStep {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "first migratiion";
|
||||||
|
}
|
||||||
|
|
||||||
|
public Migration1() throws Exception {
|
||||||
|
|
||||||
|
addAction("""
|
||||||
|
ALTER TABLE `TestTableMigration`
|
||||||
|
RENAME COLUMN `testData` TO `testDataMigration1`
|
||||||
|
""");
|
||||||
|
display();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
21
test/src/test/kar/archidata/migration/Migration2.java
Normal file
21
test/src/test/kar/archidata/migration/Migration2.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package test.kar.archidata.migration;
|
||||||
|
|
||||||
|
import org.kar.archidata.migration.MigrationSqlStep;
|
||||||
|
|
||||||
|
class Migration2 extends MigrationSqlStep {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Second migration";
|
||||||
|
}
|
||||||
|
|
||||||
|
public Migration2() throws Exception {
|
||||||
|
|
||||||
|
addAction("""
|
||||||
|
ALTER TABLE `TestTableMigration`
|
||||||
|
RENAME COLUMN `testDataMigration1` TO `testDataMigration2`
|
||||||
|
""");
|
||||||
|
display();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
21
test/src/test/kar/archidata/migration/MigrationFail.java
Normal file
21
test/src/test/kar/archidata/migration/MigrationFail.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package test.kar.archidata.migration;
|
||||||
|
|
||||||
|
import org.kar.archidata.migration.MigrationSqlStep;
|
||||||
|
|
||||||
|
class MigrationFail extends MigrationSqlStep {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Fail migration Test";
|
||||||
|
}
|
||||||
|
|
||||||
|
public MigrationFail() throws Exception {
|
||||||
|
|
||||||
|
addAction("""
|
||||||
|
ALTER TABLE `TestTableMigrationqs`
|
||||||
|
RENAME COLUMN `testDataMisqdgration1` TO `testDataMiqsdgration2`
|
||||||
|
""");
|
||||||
|
display();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
76
test/src/test/kar/archidata/migration/TestMigrationFail.java
Normal file
76
test/src/test/kar/archidata/migration/TestMigrationFail.java
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package test.kar.archidata.migration;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.MethodOrderer;
|
||||||
|
import org.junit.jupiter.api.Order;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.kar.archidata.GlobalConfiguration;
|
||||||
|
import org.kar.archidata.dataAccess.DataAccess;
|
||||||
|
import org.kar.archidata.db.DBEntry;
|
||||||
|
import org.kar.archidata.migration.MigrationEngine;
|
||||||
|
import org.kar.archidata.migration.MigrationException;
|
||||||
|
import org.kar.archidata.util.ConfigBaseVariable;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import test.kar.archidata.StepwiseExtension;
|
||||||
|
import test.kar.archidata.migration.model.TypesMigrationInitialisationFirst;
|
||||||
|
|
||||||
|
@ExtendWith(StepwiseExtension.class)
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
public class TestMigrationFail {
|
||||||
|
final static private Logger LOGGER = LoggerFactory.getLogger(TestMigrationFirstInit.class);
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void configureWebServer() throws Exception {
|
||||||
|
ConfigBaseVariable.dbType = "sqlite";
|
||||||
|
ConfigBaseVariable.dbHost = "memory";
|
||||||
|
// for test we need to connect all time the DB
|
||||||
|
ConfigBaseVariable.dbKeepConnected = "true";
|
||||||
|
// Connect the dataBase...
|
||||||
|
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||||
|
entry.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void removeDataBase() throws IOException {
|
||||||
|
LOGGER.info("Remove the test db");
|
||||||
|
DBEntry.closeAllForceMode();
|
||||||
|
ConfigBaseVariable.clearAllValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(1)
|
||||||
|
@Test
|
||||||
|
public void testInitializeTable() throws Exception {
|
||||||
|
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||||
|
// add initialization:
|
||||||
|
migrationEngine.setInit(new InitializationFirst());
|
||||||
|
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||||
|
|
||||||
|
final TypesMigrationInitialisationFirst test = new TypesMigrationInitialisationFirst();
|
||||||
|
test.testData = 95.0;
|
||||||
|
final TypesMigrationInitialisationFirst insertedData = DataAccess.insert(test);
|
||||||
|
Assertions.assertNotNull(insertedData);
|
||||||
|
Assertions.assertEquals(95.0, insertedData.testData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(2)
|
||||||
|
@Test
|
||||||
|
public void testUpdateTwoMigration() throws Exception {
|
||||||
|
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||||
|
// add initialization:
|
||||||
|
migrationEngine.setInit(new InitializationCurrent());
|
||||||
|
migrationEngine.add(new Migration1());
|
||||||
|
migrationEngine.add(new MigrationFail());
|
||||||
|
Assertions.assertThrows(MigrationException.class, () -> {
|
||||||
|
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,111 @@
|
|||||||
|
package test.kar.archidata.migration;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.MethodOrderer;
|
||||||
|
import org.junit.jupiter.api.Order;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.kar.archidata.GlobalConfiguration;
|
||||||
|
import org.kar.archidata.dataAccess.DataAccess;
|
||||||
|
import org.kar.archidata.db.DBEntry;
|
||||||
|
import org.kar.archidata.migration.MigrationEngine;
|
||||||
|
import org.kar.archidata.util.ConfigBaseVariable;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import test.kar.archidata.StepwiseExtension;
|
||||||
|
import test.kar.archidata.migration.model.TypesMigrationInitialisationCurrent;
|
||||||
|
import test.kar.archidata.migration.model.TypesMigrationInitialisationFirst;
|
||||||
|
|
||||||
|
@ExtendWith(StepwiseExtension.class)
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
public class TestMigrationFirstInit {
|
||||||
|
final static private Logger LOGGER = LoggerFactory.getLogger(TestMigrationFail.class);
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void configureWebServer() throws Exception {
|
||||||
|
ConfigBaseVariable.dbType = "sqlite";
|
||||||
|
ConfigBaseVariable.dbHost = "memory";
|
||||||
|
// for test we need to connect all time the DB
|
||||||
|
ConfigBaseVariable.dbKeepConnected = "true";
|
||||||
|
// Connect the dataBase...
|
||||||
|
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||||
|
entry.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void removeDataBase() throws IOException {
|
||||||
|
LOGGER.info("Remove the test db");
|
||||||
|
DBEntry.closeAllForceMode();
|
||||||
|
ConfigBaseVariable.clearAllValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(1)
|
||||||
|
@Test
|
||||||
|
public void testInitialMigration() throws Exception {
|
||||||
|
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||||
|
// add initialization:
|
||||||
|
migrationEngine.setInit(new InitializationFirst());
|
||||||
|
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||||
|
|
||||||
|
final TypesMigrationInitialisationFirst test = new TypesMigrationInitialisationFirst();
|
||||||
|
test.testData = 95.0;
|
||||||
|
final TypesMigrationInitialisationFirst insertedData = DataAccess.insert(test);
|
||||||
|
Assertions.assertNotNull(insertedData);
|
||||||
|
Assertions.assertEquals(95.0, insertedData.testData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(2)
|
||||||
|
@Test
|
||||||
|
public void testInitialMigrationReopen() throws Exception {
|
||||||
|
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||||
|
// add initialization:
|
||||||
|
migrationEngine.setInit(new InitializationFirst());
|
||||||
|
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||||
|
|
||||||
|
final TypesMigrationInitialisationFirst test = new TypesMigrationInitialisationFirst();
|
||||||
|
test.testData = 99.0;
|
||||||
|
final TypesMigrationInitialisationFirst insertedData = DataAccess.insert(test);
|
||||||
|
Assertions.assertNotNull(insertedData);
|
||||||
|
Assertions.assertEquals(99.0, insertedData.testData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(3)
|
||||||
|
@Test
|
||||||
|
public void testUpdateTwoMigration() throws Exception {
|
||||||
|
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||||
|
// add initialization:
|
||||||
|
migrationEngine.setInit(new InitializationCurrent());
|
||||||
|
migrationEngine.add(new Migration1());
|
||||||
|
migrationEngine.add(new Migration2());
|
||||||
|
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||||
|
|
||||||
|
final TypesMigrationInitialisationCurrent test = new TypesMigrationInitialisationCurrent();
|
||||||
|
test.testDataMigration2 = 125.0;
|
||||||
|
final TypesMigrationInitialisationCurrent insertedData = DataAccess.insert(test);
|
||||||
|
Assertions.assertNotNull(insertedData);
|
||||||
|
Assertions.assertEquals(125.0, insertedData.testDataMigration2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(4)
|
||||||
|
@Test
|
||||||
|
public void testUpdateTwoMigrationReopen() throws Exception {
|
||||||
|
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||||
|
// add initialization:
|
||||||
|
migrationEngine.setInit(new InitializationCurrent());
|
||||||
|
migrationEngine.add(new Migration1());
|
||||||
|
migrationEngine.add(new Migration2());
|
||||||
|
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||||
|
|
||||||
|
final TypesMigrationInitialisationCurrent test = new TypesMigrationInitialisationCurrent();
|
||||||
|
test.testDataMigration2 = 2563.0;
|
||||||
|
final TypesMigrationInitialisationCurrent insertedData = DataAccess.insert(test);
|
||||||
|
Assertions.assertNotNull(insertedData);
|
||||||
|
Assertions.assertEquals(2563.0, insertedData.testDataMigration2);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,90 @@
|
|||||||
|
package test.kar.archidata.migration;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.MethodOrderer;
|
||||||
|
import org.junit.jupiter.api.Order;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.kar.archidata.GlobalConfiguration;
|
||||||
|
import org.kar.archidata.dataAccess.DataAccess;
|
||||||
|
import org.kar.archidata.db.DBEntry;
|
||||||
|
import org.kar.archidata.migration.MigrationEngine;
|
||||||
|
import org.kar.archidata.migration.model.Migration;
|
||||||
|
import org.kar.archidata.util.ConfigBaseVariable;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import test.kar.archidata.StepwiseExtension;
|
||||||
|
import test.kar.archidata.migration.model.TypesMigrationInitialisationCurrent;
|
||||||
|
|
||||||
|
@ExtendWith(StepwiseExtension.class)
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
public class TestMigrationFirstInitWithMigration {
|
||||||
|
final static private Logger LOGGER = LoggerFactory.getLogger(TestMigrationFirstInitWithMigration.class);
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void configureWebServer() throws Exception {
|
||||||
|
ConfigBaseVariable.dbType = "sqlite";
|
||||||
|
ConfigBaseVariable.dbHost = "memory";
|
||||||
|
// for test we need to connect all time the DB
|
||||||
|
ConfigBaseVariable.dbKeepConnected = "true";
|
||||||
|
// Connect the dataBase...
|
||||||
|
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||||
|
entry.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void removeDataBase() throws IOException {
|
||||||
|
LOGGER.info("Remove the test db");
|
||||||
|
DBEntry.closeAllForceMode();
|
||||||
|
ConfigBaseVariable.clearAllValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(1)
|
||||||
|
@Test
|
||||||
|
public void testInitialMigration() throws Exception {
|
||||||
|
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||||
|
// add initialization:
|
||||||
|
migrationEngine.setInit(new InitializationCurrent());
|
||||||
|
// add migration for old version
|
||||||
|
migrationEngine.add(new Migration1());
|
||||||
|
migrationEngine.add(new Migration2());
|
||||||
|
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||||
|
|
||||||
|
final TypesMigrationInitialisationCurrent test = new TypesMigrationInitialisationCurrent();
|
||||||
|
test.testDataMigration2 = 95.0;
|
||||||
|
final TypesMigrationInitialisationCurrent insertedData = DataAccess.insert(test);
|
||||||
|
Assertions.assertNotNull(insertedData);
|
||||||
|
Assertions.assertEquals(95.0, insertedData.testDataMigration2);
|
||||||
|
|
||||||
|
final List<Migration> elements = DataAccess.gets(Migration.class);
|
||||||
|
LOGGER.info("List of migrations:");
|
||||||
|
for (final Migration elem : elements) {
|
||||||
|
LOGGER.info(" - {} => {}", elem.id, elem.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(2)
|
||||||
|
@Test
|
||||||
|
public void testInitialMigrationReopen() throws Exception {
|
||||||
|
final MigrationEngine migrationEngine = new MigrationEngine();
|
||||||
|
// add initialization:
|
||||||
|
migrationEngine.setInit(new InitializationCurrent());
|
||||||
|
// add migration for old version
|
||||||
|
migrationEngine.add(new Migration1());
|
||||||
|
migrationEngine.add(new Migration2());
|
||||||
|
migrationEngine.migrateErrorThrow(GlobalConfiguration.dbConfig);
|
||||||
|
|
||||||
|
final TypesMigrationInitialisationCurrent test = new TypesMigrationInitialisationCurrent();
|
||||||
|
test.testDataMigration2 = 99.0;
|
||||||
|
final TypesMigrationInitialisationCurrent insertedData = DataAccess.insert(test);
|
||||||
|
Assertions.assertNotNull(insertedData);
|
||||||
|
Assertions.assertEquals(99.0, insertedData.testDataMigration2);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package test.kar.archidata.migration.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Table(name = "TestTableMigration")
|
||||||
|
public class TypesMigration1 {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(nullable = false, unique = true)
|
||||||
|
public Long id = null;
|
||||||
|
public Double testDataMigration1;
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package test.kar.archidata.migration.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Table(name = "TestTableMigration")
|
||||||
|
public class TypesMigration2 {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(nullable = false, unique = true)
|
||||||
|
public Long id = null;
|
||||||
|
public Double testDataMigration2;
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package test.kar.archidata.migration.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Table(name = "TestTableMigration")
|
||||||
|
public class TypesMigrationInitialisationCurrent {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(nullable = false, unique = true)
|
||||||
|
public Long id = null;
|
||||||
|
public Double testDataMigration2;
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package test.kar.archidata.migration.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Table(name = "TestTableMigration")
|
||||||
|
public class TypesMigrationInitialisationFirst {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(nullable = false, unique = true)
|
||||||
|
public Long id = null;
|
||||||
|
public Double testData;
|
||||||
|
}
|
@ -3,7 +3,7 @@ package test.kar.archidata.model;
|
|||||||
public enum Enum2ForTest {
|
public enum Enum2ForTest {
|
||||||
ENUM_VALUE_1(5), ENUM_VALUE_2(6), ENUM_VALUE_3(55), ENUM_VALUE_4(84241), ENUM_VALUE_5(54546);
|
ENUM_VALUE_1(5), ENUM_VALUE_2(6), ENUM_VALUE_3(55), ENUM_VALUE_4(84241), ENUM_VALUE_5(54546);
|
||||||
|
|
||||||
private final int value;
|
final int value;
|
||||||
|
|
||||||
private Enum2ForTest(final int value) {
|
private Enum2ForTest(final int value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user