Fixed migrations and transactions
This commit is contained in:
@@ -14,6 +14,7 @@ import it.integry.ems.utility.UtilityDebug;
|
||||
import it.integry.ems_model.config.EmsRestConstants;
|
||||
import it.integry.ems_model.entity.Azienda;
|
||||
import it.integry.ems_model.entity.StbMigrationStatus;
|
||||
import it.integry.ems_model.exception.DataConverterNotFoundException;
|
||||
import it.integry.ems_model.rulescompleting.DroolsDataCompleting;
|
||||
import it.integry.ems_model.types.OperationType;
|
||||
import it.integry.ems_model.utility.UtilityDB;
|
||||
@@ -25,9 +26,9 @@ import org.reflections.Reflections;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.Statement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -85,37 +86,18 @@ public class MigrationService {
|
||||
}
|
||||
}
|
||||
|
||||
private void initLastVersionField(AdvancedDataSource advancedDataSource) throws Exception {
|
||||
|
||||
String sql = "SELECT CAST(COUNT(*) AS BIT) as exist " +
|
||||
"FROM sys.columns " +
|
||||
"WHERE Name = N'last_migration' " +
|
||||
" AND Object_ID = Object_ID(N'dbo.azienda')";
|
||||
|
||||
boolean fieldExists = UtilityDB.executeSimpleQueryOnlyFirstRowFirstColumn(advancedDataSource.getConnection(), sql);
|
||||
|
||||
if (!fieldExists) {
|
||||
String alterTableSql = "ALTER TABLE azienda ADD last_migration VARCHAR(14)";
|
||||
Statement statement = advancedDataSource.getConnection().createStatement();
|
||||
statement.execute(alterTableSql);
|
||||
statement.close();
|
||||
|
||||
advancedDataSource.getConnection().commit();
|
||||
}
|
||||
}
|
||||
|
||||
public void executeMigrationGroup(AdvancedDataSource advancedDataSource) {
|
||||
try {
|
||||
initLastVersionField(advancedDataSource);
|
||||
migrateStatus(advancedDataSource);
|
||||
List<StbMigrationStatus> migrationStatuses = retrieveAllMigrationsStatus(advancedDataSource);
|
||||
|
||||
for (Class<? extends MigrationModelInterface> migrationClass : allMigrationsList) {
|
||||
long migrationNumber = Long.parseLong(migrationClass.getSimpleName().replace("Migration_", ""));
|
||||
|
||||
if (migrationStatuses.stream().anyMatch(x -> x.getMigrationCode() == migrationNumber && (x.isCompleted() || x.isSkipped())))
|
||||
continue;
|
||||
|
||||
try {
|
||||
long lastMigration = getLastMigrationFromDB(advancedDataSource);
|
||||
|
||||
if (migrationNumber <= lastMigration) continue;
|
||||
|
||||
executeMigration(migrationClass, advancedDataSource, settingsController, settingsModel, droolsDataCompleting);
|
||||
updateLastMigrationIntoDB(advancedDataSource, migrationNumber);
|
||||
|
||||
@@ -143,7 +125,6 @@ public class MigrationService {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void executeMigration(Class<? extends MigrationModelInterface> migrationClass,
|
||||
AdvancedDataSource advancedDataSource,
|
||||
SettingsController settingsController,
|
||||
@@ -157,20 +138,7 @@ public class MigrationService {
|
||||
migrationInstance.up();
|
||||
}
|
||||
|
||||
private Long getLastMigrationFromDB(AdvancedDataSource advancedDataSource) throws Exception {
|
||||
String sql = "SELECT last_migration FROM azienda";
|
||||
String lastMigration = UtilityDB.executeSimpleQueryOnlyFirstRowFirstColumn(advancedDataSource.getConnection(), sql);
|
||||
lastMigration = UtilityString.isNullOrEmpty(lastMigration) ? "-1" : lastMigration;
|
||||
|
||||
return Long.parseLong(lastMigration);
|
||||
}
|
||||
|
||||
public void updateLastMigrationIntoDB(AdvancedDataSource advancedDataSource, long lastMigraton) throws Exception {
|
||||
String updateSql = "UPDATE azienda SET last_migration = " + UtilityDB.valueToString(String.valueOf(lastMigraton));
|
||||
PreparedStatement ps = advancedDataSource.getConnection().prepareStatement(updateSql);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
|
||||
String sql = "SELECT * FROM " + StbMigrationStatus.ENTITY + " WHERE migration_code = " + lastMigraton;
|
||||
StbMigrationStatus stbMigrationStatus = UtilityDB.executeSimpleQueryOnlyFirstRowDTO(advancedDataSource.getConnection(), sql, StbMigrationStatus.class);
|
||||
|
||||
@@ -200,6 +168,49 @@ public class MigrationService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
private void migrateStatus(AdvancedDataSource advancedDataSource) throws Exception {
|
||||
final long lastMigrationNumber = getLastMigrationFromDB(advancedDataSource);
|
||||
|
||||
final List<StbMigrationStatus> migrationStatusesToMigrate = allMigrationsList.stream()
|
||||
.map(x -> Long.parseLong(x.getSimpleName().replace("Migration_", "")))
|
||||
.filter(x -> x <= lastMigrationNumber)
|
||||
.map(x -> {
|
||||
final StbMigrationStatus stbMigrationStatus = new StbMigrationStatus()
|
||||
.setMigrationCode(x)
|
||||
.setCompleted(true);
|
||||
stbMigrationStatus.setOperation(OperationType.INSERT);
|
||||
return stbMigrationStatus;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (StbMigrationStatus stbMigrationStatus : migrationStatusesToMigrate)
|
||||
stbMigrationStatus.manageWithParentConnection(advancedDataSource.getConnection());
|
||||
|
||||
}
|
||||
|
||||
private Long getLastMigrationFromDB(AdvancedDataSource advancedDataSource) throws Exception {
|
||||
String sql = "SELECT last_migration FROM azienda";
|
||||
String lastMigration = UtilityDB.executeSimpleQueryOnlyFirstRowFirstColumn(advancedDataSource.getConnection(), sql);
|
||||
lastMigration = UtilityString.isNullOrEmpty(lastMigration) ? "-1" : lastMigration;
|
||||
|
||||
return Long.parseLong(lastMigration);
|
||||
}
|
||||
|
||||
|
||||
private List<StbMigrationStatus> retrieveAllMigrationsStatus(AdvancedDataSource advancedDataSource) throws SQLException, IOException, DataConverterNotFoundException, InstantiationException, IllegalAccessException {
|
||||
String sql = "SELECT * FROM " + StbMigrationStatus.ENTITY;
|
||||
List<StbMigrationStatus> stbMigrationStatuses = UtilityDB.executeSimpleQueryDTO(advancedDataSource.getConnection(), sql, StbMigrationStatus.class);
|
||||
|
||||
if (stbMigrationStatuses == null) return new ArrayList<>();
|
||||
|
||||
stbMigrationStatuses = stbMigrationStatuses.stream()
|
||||
.sorted(Comparator.comparing(StbMigrationStatus::getMigrationCode))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return stbMigrationStatuses;
|
||||
}
|
||||
|
||||
private void trackMigrationError(AdvancedDataSource advancedDataSource, long migrationCode, Exception e) {
|
||||
migrationsErrorMapByProfiles.putIfAbsent(advancedDataSource.getProfileName(), true);
|
||||
migrationsErrorMapByProfiles.replace(advancedDataSource.getProfileName(), true);
|
||||
@@ -243,7 +254,7 @@ public class MigrationService {
|
||||
|
||||
public boolean isAnyMigrationFailed() {
|
||||
for (String profileDb : migrationsErrorMapByProfiles.keySet())
|
||||
if(migrationsErrorMapByProfiles.get(profileDb)) return true;
|
||||
if (migrationsErrorMapByProfiles.get(profileDb)) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package it.integry.ems.migration.model._base;
|
||||
package it.integry.ems.migration._base;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import it.integry.ems.settings.Model.SettingsModel;
|
||||
@@ -88,6 +88,9 @@ public abstract class BaseMigration implements MigrationModelInterface {
|
||||
return generatedId;
|
||||
}
|
||||
|
||||
public void updateFunction(Connection connection, String sql) throws SQLException {
|
||||
executeStatement(connection, sql);
|
||||
}
|
||||
|
||||
protected void dropTable(Connection connection, String tableName) throws SQLException {
|
||||
String dropSql = "DROP TABLE " + tableName;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package it.integry.ems.migration.model;
|
||||
|
||||
import it.integry.ems.migration.model._base.BaseMigration;
|
||||
import it.integry.ems.migration.model._base.MigrationModelInterface;
|
||||
import it.integry.ems.migration._base.BaseMigration;
|
||||
import it.integry.ems.migration._base.MigrationModelInterface;
|
||||
import it.integry.ems_model.utility.Query;
|
||||
import it.integry.ems_model.utility.UtilityDB;
|
||||
import it.integry.ems_model.utility.UtilityHashMap;
|
||||
@@ -12,7 +12,7 @@ import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class Migration_20240124171059 extends BaseMigration implements MigrationModelInterface {
|
||||
public class Migration_20240212000000 extends BaseMigration implements MigrationModelInterface {
|
||||
|
||||
@Override
|
||||
public void up() throws Exception {
|
||||
@@ -1,9 +1,9 @@
|
||||
package it.integry.ems.migration.model;
|
||||
|
||||
import it.integry.ems.migration.model._base.BaseMigration;
|
||||
import it.integry.ems.migration.model._base.MigrationModelInterface;
|
||||
import it.integry.ems.migration._base.BaseMigration;
|
||||
import it.integry.ems.migration._base.MigrationModelInterface;
|
||||
|
||||
public class Migration_20240130180106 extends BaseMigration implements MigrationModelInterface {
|
||||
public class Migration_20240212000001 extends BaseMigration implements MigrationModelInterface {
|
||||
|
||||
@Override
|
||||
public void up() throws Exception {
|
||||
@@ -1,9 +1,9 @@
|
||||
package it.integry.ems.migration.model;
|
||||
|
||||
import it.integry.ems.migration.model._base.BaseMigration;
|
||||
import it.integry.ems.migration.model._base.MigrationModelInterface;
|
||||
import it.integry.ems.migration._base.BaseMigration;
|
||||
import it.integry.ems.migration._base.MigrationModelInterface;
|
||||
|
||||
public class Migration_20240131172623 extends BaseMigration implements MigrationModelInterface {
|
||||
public class Migration_20240212000002 extends BaseMigration implements MigrationModelInterface {
|
||||
|
||||
@Override
|
||||
public void up() throws Exception {
|
||||
@@ -0,0 +1,15 @@
|
||||
package it.integry.ems.system.dto.syncronization;
|
||||
|
||||
public class InsertPublicationGroupRequestDTO {
|
||||
|
||||
private String description;
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public InsertPublicationGroupRequestDTO setDescription(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package it.integry.ems.system.dto.syncronization;
|
||||
|
||||
public class InsertPublicationGroupResponseDTO {
|
||||
|
||||
private long id;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public InsertPublicationGroupResponseDTO setId(long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package it.integry.ems.system.dto.syncronization;
|
||||
|
||||
public class PublicationDTO {
|
||||
|
||||
private long id;
|
||||
private String entityName;
|
||||
private String whereCondSql;
|
||||
private String recalcColumns;
|
||||
private boolean syncronize;
|
||||
private String whereCond;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public PublicationDTO setId(long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getEntityName() {
|
||||
return entityName;
|
||||
}
|
||||
|
||||
public PublicationDTO setEntityName(String entityName) {
|
||||
this.entityName = entityName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getWhereCondSql() {
|
||||
return whereCondSql;
|
||||
}
|
||||
|
||||
public PublicationDTO setWhereCondSql(String whereCondSql) {
|
||||
this.whereCondSql = whereCondSql;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getRecalcColumns() {
|
||||
return recalcColumns;
|
||||
}
|
||||
|
||||
public PublicationDTO setRecalcColumns(String recalcColumns) {
|
||||
this.recalcColumns = recalcColumns;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isSyncronize() {
|
||||
return syncronize;
|
||||
}
|
||||
|
||||
public PublicationDTO setSyncronize(boolean syncronize) {
|
||||
this.syncronize = syncronize;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getWhereCond() {
|
||||
return whereCond;
|
||||
}
|
||||
|
||||
public PublicationDTO setWhereCond(String whereCond) {
|
||||
this.whereCond = whereCond;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package it.integry.ems.system.dto.syncronization;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PublicationGroupDTO {
|
||||
|
||||
private long id;
|
||||
private String description;
|
||||
|
||||
private List<PublicationDTO> publications = new ArrayList<>();
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public PublicationGroupDTO setId(long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public PublicationGroupDTO setDescription(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<PublicationDTO> getPublications() {
|
||||
return publications;
|
||||
}
|
||||
|
||||
public PublicationGroupDTO setPublications(List<PublicationDTO> publications) {
|
||||
this.publications = publications;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user