[DEV] update migraation model and manage sqlite support update with milisecond

This commit is contained in:
Edouard DUPIN 2023-11-24 00:07:52 +01:00
parent 01de431f5a
commit ed3bfa0604
11 changed files with 160 additions and 121 deletions

View File

@ -193,7 +193,8 @@ public class DataFactory {
triggerBuilder.append(tableName); triggerBuilder.append(tableName);
triggerBuilder.append(" SET "); triggerBuilder.append(" SET ");
triggerBuilder.append(name); triggerBuilder.append(name);
triggerBuilder.append(" = datetime('now') WHERE id = NEW.id; \n"); //triggerBuilder.append(" = datetime('now') WHERE id = NEW.id; \n");
triggerBuilder.append(" = strftime('%Y-%m-%d %H:%M:%f', 'now') WHERE id = NEW.id; \n");
triggerBuilder.append("END;"); triggerBuilder.append("END;");
postOtherTables.add(triggerBuilder.toString()); postOtherTables.add(triggerBuilder.toString());

View File

@ -84,7 +84,10 @@ public class MigrationEngine {
} catch (final Exception ex) { } catch (final Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
while (true) { while (true) {
LOGGER.error("Fail to create the local DB SQL model for migaration ==> wait administrator interventions"); LOGGER.error("ERROR: {}", ex.getMessage());
LOGGER.error("========================================================================");
LOGGER.error("== Fail to migrate ==> wait administrator interventions ==");
LOGGER.error("========================================================================");
Thread.sleep(60 * 60 * 1000); Thread.sleep(60 * 60 * 1000);
} }
} }
@ -95,7 +98,6 @@ public class MigrationEngine {
* @throws IOException Error if access on the DB */ * @throws IOException Error if access on the DB */
public void migrateErrorThrow(final DBConfig config) throws MigrationException { public void migrateErrorThrow(final DBConfig config) throws MigrationException {
LOGGER.info("Execute migration ... [BEGIN]"); LOGGER.info("Execute migration ... [BEGIN]");
// check the integrity of the migrations: // check the integrity of the migrations:
for (final MigrationInterface elem : this.datas) { for (final MigrationInterface elem : this.datas) {
if (elem == null) { if (elem == null) {
@ -247,8 +249,16 @@ public class MigrationEngine {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
boolean ret = true;
if (elem.applyMigration(entry, log, migrationResult)) { try {
ret = elem.applyMigration(entry, log, migrationResult);
} catch (final Exception e) {
log.append("\nFail in the migration apply ");
log.append(e.getLocalizedMessage());
e.printStackTrace();
throw new MigrationException("Migration fial: '" + migrationResult.name + "' defect @" + migrationResult.stepId + "/" + migrationResult.count);
}
if (ret) {
migrationResult.terminated = true; migrationResult.terminated = true;
try { try {
DataAccess.update(migrationResult, migrationResult.id, List.of("terminated")); DataAccess.update(migrationResult, migrationResult.id, List.of("terminated"));

View File

@ -13,13 +13,13 @@ public interface MigrationInterface {
* @param log Stored data in the BDD for the migration progression. * @param log Stored data in the BDD for the migration progression.
* @param migration Migration post data on each step... * @param migration Migration post data on each step...
* @return true if migration is finished. */ * @return true if migration is finished. */
boolean applyMigration(DBEntry entry, StringBuilder log, Migration model); boolean applyMigration(DBEntry entry, StringBuilder log, Migration model) throws Exception;
/** Remove a migration the system to the previous version. /** Remove a migration the system to the previous version.
* @param entry DB interface for the migration. * @param entry DB interface for the migration.
* @param log Stored data in the BDD for the migration progression. * @param log Stored data in the BDD for the migration progression.
* @return true if migration is finished. */ * @return true if migration is finished. */
boolean revertMigration(DBEntry entry, StringBuilder log); boolean revertMigration(DBEntry entry, StringBuilder log) throws Exception;
/** Get the number of step in the migration process. /** Get the number of step in the migration process.
* @return count of SQL access. */ * @return count of SQL access. */

View File

@ -13,7 +13,9 @@ import org.kar.archidata.util.ConfigBaseVariable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
record Action(String action, List<String> filterDB) { record Action(
String action,
List<String> filterDB) {
public Action(final String action) { public Action(final String action) {
this(action, List.of()); this(action, List.of());
} }
@ -39,8 +41,17 @@ public class MigrationSqlStep implements MigrationInterface {
} }
} }
public void generateStep() throws Exception {
throw new Exception("Forward is not implemented");
}
public void generateRevertStep() throws Exception {
throw new Exception("Backward is not implemented");
}
@Override @Override
public boolean applyMigration(final DBEntry entry, final StringBuilder log, final Migration model) { public boolean applyMigration(final DBEntry entry, final StringBuilder log, final Migration model) throws Exception {
generateStep();
for (int iii = 0; iii < this.actions.size(); iii++) { for (int iii = 0; iii < this.actions.size(); iii++) {
log.append("action [" + (iii + 1) + "/" + this.actions.size() + "]\n"); log.append("action [" + (iii + 1) + "/" + this.actions.size() + "]\n");
LOGGER.info(" >>>> SQL ACTION : {}/{}", iii + 1, this.actions.size()); LOGGER.info(" >>>> SQL ACTION : {}/{}", iii + 1, this.actions.size());
@ -98,7 +109,8 @@ public class MigrationSqlStep implements MigrationInterface {
} }
@Override @Override
public boolean revertMigration(final DBEntry entry, final StringBuilder log) { public boolean revertMigration(final DBEntry entry, final StringBuilder log) throws Exception {
generateRevertStep();
return false; return false;
} }

View File

@ -108,12 +108,7 @@ public class TestSimpleTable {
@Order(3) @Order(3)
@Test @Test
public void testUpdateData() throws Exception { public void testUpdateData() throws Exception {
if ("sqlite".equalsIgnoreCase(ConfigBaseVariable.getDBType())) { Thread.sleep(Duration.ofMillis(15));
Thread.sleep(Duration.ofMillis(1100));
} else {
Thread.sleep(Duration.ofMillis(15));
}
// Delete the entry: // Delete the entry:
final SimpleTable test = new SimpleTable(); final SimpleTable test = new SimpleTable();
test.data = TestSimpleTable.DATA_INJECTED_2; test.data = TestSimpleTable.DATA_INJECTED_2;

View File

@ -111,11 +111,7 @@ public class TestSimpleTableSoftDelete {
@Order(3) @Order(3)
@Test @Test
public void testUpdateData() throws Exception { public void testUpdateData() throws Exception {
if ("sqlite".equalsIgnoreCase(ConfigBaseVariable.getDBType())) { Thread.sleep(Duration.ofMillis(15));
Thread.sleep(Duration.ofMillis(1100));
} else {
Thread.sleep(Duration.ofMillis(15));
}
// Delete the entry: // Delete the entry:
final SimpleTableSoftDelete test = new SimpleTableSoftDelete(); final SimpleTableSoftDelete test = new SimpleTableSoftDelete();

View File

@ -11,7 +11,12 @@ class InitializationCurrent extends MigrationSqlStep {
return "Initialization"; return "Initialization";
} }
public InitializationCurrent() throws Exception { public InitializationCurrent() {
}
@Override
public void generateStep() throws Exception {
addClass(TypesMigrationInitialisationCurrent.class); addClass(TypesMigrationInitialisationCurrent.class);
addAction(""" addAction("""
ALTER TABLE `TestTableMigration` AUTO_INCREMENT = 1000; ALTER TABLE `TestTableMigration` AUTO_INCREMENT = 1000;

View File

@ -11,7 +11,12 @@ class InitializationFirst extends MigrationSqlStep {
return "Initialization"; return "Initialization";
} }
public InitializationFirst() throws Exception { public InitializationFirst() {
}
@Override
public void generateStep() throws Exception {
addClass(TypesMigrationInitialisationFirst.class); addClass(TypesMigrationInitialisationFirst.class);
addAction(""" addAction("""
ALTER TABLE `TestTableMigration` AUTO_INCREMENT = 1000; ALTER TABLE `TestTableMigration` AUTO_INCREMENT = 1000;

View File

@ -9,7 +9,12 @@ class Migration1 extends MigrationSqlStep {
return "first migratiion"; return "first migratiion";
} }
public Migration1() throws Exception { public Migration1() {
}
@Override
public void generateStep() throws Exception {
addAction(""" addAction("""
ALTER TABLE `TestTableMigration` ALTER TABLE `TestTableMigration`

View File

@ -9,7 +9,12 @@ class Migration2 extends MigrationSqlStep {
return "Second migration"; return "Second migration";
} }
public Migration2() throws Exception { public Migration2() {
}
@Override
public void generateStep() throws Exception {
addAction(""" addAction("""
ALTER TABLE `TestTableMigration` ALTER TABLE `TestTableMigration`

View File

@ -9,7 +9,12 @@ class MigrationFail extends MigrationSqlStep {
return "Fail migration Test"; return "Fail migration Test";
} }
public MigrationFail() throws Exception { public MigrationFail() {
}
@Override
public void generateStep() throws Exception {
addAction(""" addAction("""
ALTER TABLE `TestTableMigrationqs` ALTER TABLE `TestTableMigrationqs`