[Minor] Code cleanup
This commit is contained in:
parent
20bcef2b3a
commit
405704968e
|
@ -69,9 +69,8 @@ public abstract class PostgreSqlInitializer extends SystemAction {
|
||||||
logger.info(MessageFormat.format(msg, realmName, migrationType));
|
logger.info(MessageFormat.format(msg, realmName, migrationType));
|
||||||
|
|
||||||
ModelStatistics statistics;
|
ModelStatistics statistics;
|
||||||
try (StrolchTransaction tx = this.persistenceHandler
|
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.agent.getContainer().getRealm(realmName),
|
||||||
.openTx(this.agent.getContainer().getRealm(realmName), getCertificate(), getClass().getSimpleName(),
|
getCertificate(), getClass().getSimpleName(), false)) {
|
||||||
false)) {
|
|
||||||
File dataStoreF = getDataStoreFile(this.runtimeConfig, this.realmConfig, realmName);
|
File dataStoreF = getDataStoreFile(this.runtimeConfig, this.realmConfig, realmName);
|
||||||
|
|
||||||
StoreToDaoElementListener listener = new StoreToDaoElementListener(tx);
|
StoreToDaoElementListener listener = new StoreToDaoElementListener(tx);
|
||||||
|
@ -84,19 +83,10 @@ public abstract class PostgreSqlInitializer extends SystemAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean checkNeedsDbInit(DbMigrationState migrationType) {
|
protected boolean checkNeedsDbInit(DbMigrationState migrationType) {
|
||||||
boolean needsDbInit;
|
return switch (migrationType) {
|
||||||
switch (migrationType) {
|
case CREATED, DROPPED_CREATED -> true;
|
||||||
case CREATED:
|
case MIGRATED, NOTHING -> false;
|
||||||
case DROPPED_CREATED:
|
};
|
||||||
needsDbInit = true;
|
|
||||||
break;
|
|
||||||
case MIGRATED:
|
|
||||||
case NOTHING:
|
|
||||||
default:
|
|
||||||
needsDbInit = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return needsDbInit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected File getDataStoreFile(RuntimeConfiguration runtimeConfiguration,
|
protected File getDataStoreFile(RuntimeConfiguration runtimeConfiguration,
|
||||||
|
|
|
@ -94,12 +94,11 @@ public class PostgreSqlPersistenceHandler extends StrolchComponent implements Pe
|
||||||
@Override
|
@Override
|
||||||
public void start() throws Exception {
|
public void start() throws Exception {
|
||||||
|
|
||||||
ComponentConfiguration configuration = getConfiguration();
|
ComponentConfiguration config = getConfiguration();
|
||||||
boolean allowSchemaCreation = configuration.getBoolean(PROP_ALLOW_SCHEMA_CREATION, Boolean.FALSE);
|
boolean allowSchemaCreation = config.getBoolean(PROP_ALLOW_SCHEMA_CREATION, false);
|
||||||
boolean allowSchemaMigration = configuration.getBoolean(PROP_ALLOW_SCHEMA_MIGRATION, Boolean.FALSE);
|
boolean allowSchemaMigration = config.getBoolean(PROP_ALLOW_SCHEMA_MIGRATION, false);
|
||||||
boolean allowSchemaDrop = configuration.getBoolean(PROP_ALLOW_SCHEMA_DROP, Boolean.FALSE);
|
boolean allowSchemaDrop = config.getBoolean(PROP_ALLOW_SCHEMA_DROP, false);
|
||||||
boolean allowDataInitOnSchemaCreate = configuration.getBoolean(PROP_ALLOW_DATA_INIT_ON_SCHEMA_CREATE,
|
boolean allowDataInitOnSchemaCreate = config.getBoolean(PROP_ALLOW_DATA_INIT_ON_SCHEMA_CREATE, false);
|
||||||
Boolean.FALSE);
|
|
||||||
|
|
||||||
DbSchemaVersionCheck schemaVersionCheck = new DbSchemaVersionCheck(SCRIPT_PREFIX_STROLCH, this.getClass(),
|
DbSchemaVersionCheck schemaVersionCheck = new DbSchemaVersionCheck(SCRIPT_PREFIX_STROLCH, this.getClass(),
|
||||||
allowSchemaCreation, allowSchemaMigration, allowSchemaDrop);
|
allowSchemaCreation, allowSchemaMigration, allowSchemaDrop);
|
||||||
|
|
|
@ -28,7 +28,7 @@ import li.strolch.privilege.model.PrivilegeContext;
|
||||||
*/
|
*/
|
||||||
public class PostgreSqlSchemaInitializer extends PostgreSqlInitializer {
|
public class PostgreSqlSchemaInitializer extends PostgreSqlInitializer {
|
||||||
|
|
||||||
private Map<String, DbMigrationState> dbMigrationStates;
|
private final Map<String, DbMigrationState> dbMigrationStates;
|
||||||
|
|
||||||
public PostgreSqlSchemaInitializer(StrolchAgent agent, PostgreSqlPersistenceHandler persistenceHandler,
|
public PostgreSqlSchemaInitializer(StrolchAgent agent, PostgreSqlPersistenceHandler persistenceHandler,
|
||||||
Map<String, DbMigrationState> dbMigrationStates) {
|
Map<String, DbMigrationState> dbMigrationStates) {
|
||||||
|
|
|
@ -15,7 +15,6 @@
|
||||||
*/
|
*/
|
||||||
package li.strolch.persistence.postgresql.dao.test;
|
package li.strolch.persistence.postgresql.dao.test;
|
||||||
|
|
||||||
import static li.strolch.db.DbConstants.PROP_DB_ALLOW_HOST_OVERRIDE_ENV;
|
|
||||||
import static li.strolch.db.DbConstants.PROP_DB_HOST_OVERRIDE;
|
import static li.strolch.db.DbConstants.PROP_DB_HOST_OVERRIDE;
|
||||||
import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_ARCHIVE;
|
import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_ARCHIVE;
|
||||||
import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_STROLCH;
|
import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_STROLCH;
|
||||||
|
@ -43,13 +42,13 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class CachedDaoTest extends AbstractModelTest {
|
public class CachedDaoTest extends AbstractModelTest {
|
||||||
|
|
||||||
public static final String RUNTIME_PATH = "target/cachedRuntime/"; //$NON-NLS-1$
|
public static final String RUNTIME_PATH = "target/cachedRuntime/";
|
||||||
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
|
public static final String DB_STORE_PATH_DIR = "dbStore";
|
||||||
public static final String CONFIG_SRC = "src/test/resources/cachedRuntime"; //$NON-NLS-1$
|
public static final String CONFIG_SRC = "src/test/resources/cachedRuntime";
|
||||||
|
|
||||||
public static final String DB_URL = "jdbc:postgresql://localhost/testdb"; //$NON-NLS-1$
|
public static final String DB_URL = "jdbc:postgresql://localhost/testdb";
|
||||||
public static final String DB_USERNAME = "testuser"; //$NON-NLS-1$
|
public static final String DB_USERNAME = "testuser";
|
||||||
public static final String DB_PASSWORD = "test"; //$NON-NLS-1$
|
public static final String DB_PASSWORD = "test";
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(CachedDaoTest.class);
|
private static final Logger logger = LoggerFactory.getLogger(CachedDaoTest.class);
|
||||||
|
|
||||||
|
@ -70,7 +69,9 @@ public class CachedDaoTest extends AbstractModelTest {
|
||||||
File configSrc = new File(CONFIG_SRC);
|
File configSrc = new File(CONFIG_SRC);
|
||||||
runtimeMock = new RuntimeMock();
|
runtimeMock = new RuntimeMock();
|
||||||
runtimeMock.mockRuntime(rootPath, configSrc);
|
runtimeMock.mockRuntime(rootPath, configSrc);
|
||||||
new File(rootPath, DB_STORE_PATH_DIR).mkdir();
|
File dbStorePath = new File(rootPath, DB_STORE_PATH_DIR);
|
||||||
|
if (!dbStorePath.mkdir())
|
||||||
|
throw new IllegalStateException("Failed to created db store path " + dbStorePath);
|
||||||
runtimeMock.startContainer();
|
runtimeMock.startContainer();
|
||||||
|
|
||||||
PostgreSqlPersistenceHandler persistenceHandler = (PostgreSqlPersistenceHandler) runtimeMock.getContainer()
|
PostgreSqlPersistenceHandler persistenceHandler = (PostgreSqlPersistenceHandler) runtimeMock.getContainer()
|
||||||
|
@ -89,8 +90,7 @@ public class CachedDaoTest extends AbstractModelTest {
|
||||||
|
|
||||||
Version dbVersion = DbSchemaVersionCheck.getExpectedDbVersion(scriptPrefix, PostgreSqlPersistenceHandler.class);
|
Version dbVersion = DbSchemaVersionCheck.getExpectedDbVersion(scriptPrefix, PostgreSqlPersistenceHandler.class);
|
||||||
logger.info(MessageFormat.format("Dropping schema for expected version {0}", dbVersion));
|
logger.info(MessageFormat.format("Dropping schema for expected version {0}", dbVersion));
|
||||||
String sql = DbSchemaVersionCheck.getSql(scriptPrefix, PostgreSqlPersistenceHandler.class, dbVersion,
|
String sql = DbSchemaVersionCheck.getSql(scriptPrefix, PostgreSqlPersistenceHandler.class, dbVersion, "drop");
|
||||||
"drop"); //$NON-NLS-1$
|
|
||||||
logger.info(StringHelper.NEW_LINE + sql);
|
logger.info(StringHelper.NEW_LINE + sql);
|
||||||
try (Connection connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword)) {
|
try (Connection connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword)) {
|
||||||
connection.prepareStatement(sql).execute();
|
connection.prepareStatement(sql).execute();
|
||||||
|
|
|
@ -15,8 +15,6 @@
|
||||||
*/
|
*/
|
||||||
package li.strolch.command;
|
package li.strolch.command;
|
||||||
|
|
||||||
import static li.strolch.db.DbConstants.PROP_DB_HOST_OVERRIDE;
|
|
||||||
import static li.strolch.runtime.configuration.DbConnectionBuilder.overridePostgresqlHost;
|
|
||||||
import static li.strolch.service.test.AbstractRealmServiceTest.*;
|
import static li.strolch.service.test.AbstractRealmServiceTest.*;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package li.strolch.command;
|
package li.strolch.command;
|
||||||
|
|
||||||
import static li.strolch.db.DbConstants.PROP_DB_HOST_OVERRIDE;
|
|
||||||
import static li.strolch.runtime.configuration.DbConnectionBuilder.overridePostgresqlHost;
|
|
||||||
import static li.strolch.service.test.AbstractRealmServiceTest.*;
|
import static li.strolch.service.test.AbstractRealmServiceTest.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ import static li.strolch.runtime.configuration.DbConnectionBuilder.overridePostg
|
||||||
import static li.strolch.testbase.runtime.RuntimeMock.assertServiceResult;
|
import static li.strolch.testbase.runtime.RuntimeMock.assertServiceResult;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
|
|
||||||
|
@ -49,8 +50,8 @@ public abstract class AbstractRealmServiceTest<T extends ServiceArgument, U exte
|
||||||
public static final String REALM_CACHED = "svcCached";
|
public static final String REALM_CACHED = "svcCached";
|
||||||
public static final String REALM_CACHED_AUDITS_VERSIONING = "svcCachedAuditsVersioning";
|
public static final String REALM_CACHED_AUDITS_VERSIONING = "svcCachedAuditsVersioning";
|
||||||
public static final String REALM_TRANSIENT = "svcTransient";
|
public static final String REALM_TRANSIENT = "svcTransient";
|
||||||
public static final String RUNTIME_PATH = "target/svcTestRuntime/"; //$NON-NLS-1$
|
public static final String RUNTIME_PATH = "target/svcTestRuntime/";
|
||||||
public static final String CONFIG_SRC = "src/test/resources/svctest"; //$NON-NLS-1$
|
public static final String CONFIG_SRC = "src/test/resources/svctest";
|
||||||
|
|
||||||
protected static final Logger logger = LoggerFactory.getLogger(AbstractRealmServiceTest.class);
|
protected static final Logger logger = LoggerFactory.getLogger(AbstractRealmServiceTest.class);
|
||||||
|
|
||||||
|
@ -97,7 +98,7 @@ public abstract class AbstractRealmServiceTest<T extends ServiceArgument, U exte
|
||||||
Version dbVersion = DbSchemaVersionCheck.getExpectedDbVersion(
|
Version dbVersion = DbSchemaVersionCheck.getExpectedDbVersion(
|
||||||
PostgreSqlPersistenceHandler.SCRIPT_PREFIX_STROLCH, PostgreSqlPersistenceHandler.class);
|
PostgreSqlPersistenceHandler.SCRIPT_PREFIX_STROLCH, PostgreSqlPersistenceHandler.class);
|
||||||
String sql = DbSchemaVersionCheck.getSql(PostgreSqlPersistenceHandler.SCRIPT_PREFIX_STROLCH,
|
String sql = DbSchemaVersionCheck.getSql(PostgreSqlPersistenceHandler.SCRIPT_PREFIX_STROLCH,
|
||||||
PostgreSqlPersistenceHandler.class, dbVersion, "drop"); //$NON-NLS-1$
|
PostgreSqlPersistenceHandler.class, dbVersion, "drop");
|
||||||
try (Connection connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword)) {
|
try (Connection connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword)) {
|
||||||
connection.prepareStatement(sql).execute();
|
connection.prepareStatement(sql).execute();
|
||||||
}
|
}
|
||||||
|
@ -114,9 +115,10 @@ public abstract class AbstractRealmServiceTest<T extends ServiceArgument, U exte
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doService(String realm, ServiceResultState expectedState, Class<?> expectedServiceResultType,
|
private void doService(String realm, ServiceResultState expectedState, Class<?> expectedServiceResultType,
|
||||||
Runner before, Runner validator, Runner after) throws IllegalAccessException, InstantiationException {
|
Runner before, Runner validator, Runner after)
|
||||||
|
throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
|
||||||
|
|
||||||
Service<T, U> svc = getSvcClass().newInstance();
|
Service<T, U> svc = getSvcClass().getConstructor().newInstance();
|
||||||
T arg = getArgInstance();
|
T arg = getArgInstance();
|
||||||
|
|
||||||
if (before != null)
|
if (before != null)
|
||||||
|
@ -165,34 +167,37 @@ public abstract class AbstractRealmServiceTest<T extends ServiceArgument, U exte
|
||||||
runTransient(expectedServiceResultType, before, validator, after);
|
runTransient(expectedServiceResultType, before, validator, after);
|
||||||
runCached(expectedServiceResultType, before, validator, after);
|
runCached(expectedServiceResultType, before, validator, after);
|
||||||
runCachedWithAuditsAndVersioning(expectedServiceResultType, before, validator, after);
|
runCachedWithAuditsAndVersioning(expectedServiceResultType, before, validator, after);
|
||||||
} catch (InstantiationException | IllegalAccessException e) {
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
|
||||||
|
NoSuchMethodException e) {
|
||||||
throw new RuntimeException("Failed to instantiate class " + getSvcClass().getName() + ": " + e.getMessage(),
|
throw new RuntimeException("Failed to instantiate class " + getSvcClass().getName() + ": " + e.getMessage(),
|
||||||
e);
|
e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runCached(Class<?> expectedServiceResultType, Runner before, Runner validator, Runner after)
|
private void runCached(Class<?> expectedServiceResultType, Runner before, Runner validator, Runner after)
|
||||||
throws InstantiationException, IllegalAccessException {
|
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||||
doService(REALM_CACHED, ServiceResultState.SUCCESS, expectedServiceResultType, before, validator, after);
|
doService(REALM_CACHED, ServiceResultState.SUCCESS, expectedServiceResultType, before, validator, after);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runCachedWithAuditsAndVersioning(Class<?> expectedServiceResultType, Runner before, Runner validator,
|
private void runCachedWithAuditsAndVersioning(Class<?> expectedServiceResultType, Runner before, Runner validator,
|
||||||
Runner after) throws InstantiationException, IllegalAccessException {
|
Runner after)
|
||||||
|
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||||
doService(REALM_CACHED_AUDITS_VERSIONING, ServiceResultState.SUCCESS, expectedServiceResultType, before,
|
doService(REALM_CACHED_AUDITS_VERSIONING, ServiceResultState.SUCCESS, expectedServiceResultType, before,
|
||||||
validator, after);
|
validator, after);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void runTransient() throws IllegalAccessException, InstantiationException {
|
protected void runTransient()
|
||||||
|
throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
|
||||||
runTransient(null);
|
runTransient(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void runTransient(Class<?> expectedServiceResultType)
|
protected void runTransient(Class<?> expectedServiceResultType)
|
||||||
throws IllegalAccessException, InstantiationException {
|
throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
|
||||||
runTransient(expectedServiceResultType, null, null, null);
|
runTransient(expectedServiceResultType, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runTransient(Class<?> expectedServiceResultType, Runner before, Runner validator, Runner after)
|
private void runTransient(Class<?> expectedServiceResultType, Runner before, Runner validator, Runner after)
|
||||||
throws InstantiationException, IllegalAccessException {
|
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||||
doService(REALM_TRANSIENT, ServiceResultState.SUCCESS, expectedServiceResultType, before, validator, after);
|
doService(REALM_TRANSIENT, ServiceResultState.SUCCESS, expectedServiceResultType, before, validator, after);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,7 +122,7 @@ public abstract class PerformanceTest {
|
||||||
logger.info("Task " + i + " executed " + results.get(i) + " TXs");
|
logger.info("Task " + i + " executed " + results.get(i) + " TXs");
|
||||||
}
|
}
|
||||||
|
|
||||||
long avg = (long) results.stream().mapToLong(l -> l).average().getAsDouble();
|
long avg = (long) results.stream().mapToLong(l -> l).average().orElse(0.0D);
|
||||||
long took = System.currentTimeMillis() - start;
|
long took = System.currentTimeMillis() - start;
|
||||||
long txPerSec = avg / (took / 1000);
|
long txPerSec = avg / (took / 1000);
|
||||||
logger.info("Average TXs was " + avg + " with " + txPerSec + " TXs/s");
|
logger.info("Average TXs was " + avg + " with " + txPerSec + " TXs/s");
|
||||||
|
@ -130,8 +130,8 @@ public abstract class PerformanceTest {
|
||||||
|
|
||||||
public class PerformanceTask extends ForkJoinTask<Long> {
|
public class PerformanceTask extends ForkJoinTask<Long> {
|
||||||
|
|
||||||
private String username;
|
private final String username;
|
||||||
private int nrOfElements;
|
private final int nrOfElements;
|
||||||
private PerformanceTestResult svcResult;
|
private PerformanceTestResult svcResult;
|
||||||
|
|
||||||
public PerformanceTask(String username, int nrOfElements) {
|
public PerformanceTask(String username, int nrOfElements) {
|
||||||
|
|
|
@ -20,13 +20,14 @@ package li.strolch.db;
|
||||||
*/
|
*/
|
||||||
public class DbConstants {
|
public class DbConstants {
|
||||||
|
|
||||||
public static final String PROP_DB_URL = "db.url"; //$NON-NLS-1$
|
public static final String PROP_USE_ENV = "db.useEnv";
|
||||||
public static final String PROP_DB_IGNORE_REALM = "db.ignore.realm"; //$NON-NLS-1$
|
public static final String PROP_DB_URL = "db.url";
|
||||||
public static final String PROP_DB_USERNAME = "db.username"; //$NON-NLS-1$
|
public static final String PROP_DB_IGNORE_REALM = "db.ignore.realm";
|
||||||
public static final String PROP_DB_PASSWORD = "db.password"; //$NON-NLS-1$
|
public static final String PROP_DB_USERNAME = "db.username";
|
||||||
public static final String PROP_DB_VERBOSE = "db.verbose"; //$NON-NLS-1$
|
public static final String PROP_DB_PASSWORD = "db.password";
|
||||||
public static final String PROP_DB_ALLOW_HOST_OVERRIDE_ENV = "db.allowHostOverrideEnv"; //$NON-NLS-1$
|
public static final String PROP_DB_VERBOSE = "db.verbose";
|
||||||
public static final String PROP_DB_HOST_OVERRIDE = "db.hostOverride"; //$NON-NLS-1$
|
public static final String PROP_DB_ALLOW_HOST_OVERRIDE_ENV = "db.allowHostOverrideEnv";
|
||||||
|
public static final String PROP_DB_HOST_OVERRIDE = "db.hostOverride";
|
||||||
public static final String PROP_ALLOW_SCHEMA_CREATION = "allowSchemaCreation";
|
public static final String PROP_ALLOW_SCHEMA_CREATION = "allowSchemaCreation";
|
||||||
public static final String PROP_ALLOW_SCHEMA_MIGRATION = "allowSchemaMigration";
|
public static final String PROP_ALLOW_SCHEMA_MIGRATION = "allowSchemaMigration";
|
||||||
public static final String PROP_ALLOW_SCHEMA_DROP = "allowSchemaDrop";
|
public static final String PROP_ALLOW_SCHEMA_DROP = "allowSchemaDrop";
|
||||||
|
|
|
@ -42,16 +42,15 @@ import org.slf4j.LoggerFactory;
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings(value = "nls")
|
|
||||||
public class DbSchemaVersionCheck {
|
public class DbSchemaVersionCheck {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(DbSchemaVersionCheck.class);
|
private static final Logger logger = LoggerFactory.getLogger(DbSchemaVersionCheck.class);
|
||||||
private String app;
|
private final String app;
|
||||||
private Class<?> ctxClass;
|
private final Class<?> ctxClass;
|
||||||
private boolean allowSchemaCreation;
|
private final boolean allowSchemaCreation;
|
||||||
private boolean allowSchemaMigration;
|
private final boolean allowSchemaMigration;
|
||||||
private boolean allowSchemaDrop;
|
private final boolean allowSchemaDrop;
|
||||||
private Map<String, DbMigrationState> dbMigrationStates;
|
private final Map<String, DbMigrationState> dbMigrationStates;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param app
|
* @param app
|
||||||
|
@ -127,18 +126,13 @@ public class DbSchemaVersionCheck {
|
||||||
DbMigrationState migrationType = detectMigrationState(realm, expectedDbVersion, currentVersion);
|
DbMigrationState migrationType = detectMigrationState(realm, expectedDbVersion, currentVersion);
|
||||||
|
|
||||||
switch (migrationType) {
|
switch (migrationType) {
|
||||||
case CREATED:
|
case CREATED -> createSchema(con, realm, expectedDbVersion);
|
||||||
createSchema(con, realm, expectedDbVersion);
|
case MIGRATED -> migrateSchema(con, realm, currentVersion, expectedDbVersion);
|
||||||
break;
|
case DROPPED_CREATED -> throw new DbException("Migration type " + migrationType + " not handled!");
|
||||||
case MIGRATED:
|
|
||||||
migrateSchema(con, realm, currentVersion, expectedDbVersion);
|
// do nothing
|
||||||
break;
|
case NOTHING -> {
|
||||||
case DROPPED_CREATED:
|
}
|
||||||
throw new DbException("Migration type " + migrationType + " not handled!");
|
|
||||||
case NOTHING:
|
|
||||||
// do nothing
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
con.commit();
|
con.commit();
|
||||||
|
@ -215,8 +209,8 @@ public class DbSchemaVersionCheck {
|
||||||
return DbMigrationState.MIGRATED;
|
return DbMigrationState.MIGRATED;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new DbException(MessageFormat
|
throw new DbException(
|
||||||
.format("[{0}:{1}]Current version {2} is later than expected version {3}", this.app, realm,
|
MessageFormat.format("[{0}:{1}]Current version {2} is later than expected version {3}", this.app, realm,
|
||||||
currentVersion, expectedDbVersion));
|
currentVersion, expectedDbVersion));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,8 +306,8 @@ public class DbSchemaVersionCheck {
|
||||||
throw new DbException("Failed to execute schema generation SQL: " + e.getMessage(), e);
|
throw new DbException("Failed to execute schema generation SQL: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(MessageFormat
|
logger.info(MessageFormat.format("[{0}:{1}] Successfully created schema with version {2}", this.app, realm,
|
||||||
.format("[{0}:{1}] Successfully created schema with version {2}", this.app, realm, version));
|
version));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -345,8 +339,8 @@ public class DbSchemaVersionCheck {
|
||||||
"Expected version " + expectedVersion + " is weirdly before current version" + currentVersion
|
"Expected version " + expectedVersion + " is weirdly before current version" + currentVersion
|
||||||
+ " for " + this.app);
|
+ " for " + this.app);
|
||||||
|
|
||||||
logger.info(MessageFormat
|
logger.info(
|
||||||
.format("[{0}:{1}] Migrating schema from {2} to {3}...", this.app, realm, currentVersion,
|
MessageFormat.format("[{0}:{1}] Migrating schema from {2} to {3}...", this.app, realm, currentVersion,
|
||||||
expectedVersion));
|
expectedVersion));
|
||||||
|
|
||||||
// first get all possible migration scripts
|
// first get all possible migration scripts
|
||||||
|
@ -383,8 +377,8 @@ public class DbSchemaVersionCheck {
|
||||||
throw new IllegalStateException("Failed to read current version", e);
|
throw new IllegalStateException("Failed to read current version", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(MessageFormat
|
logger.info(MessageFormat.format("[{0}:{1}] Successfully migrated schema to version {2}", this.app, realm,
|
||||||
.format("[{0}:{1}] Successfully migrated schema to version {2}", this.app, realm, expectedVersion));
|
expectedVersion));
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Version> parseMigrationVersions() {
|
public List<Version> parseMigrationVersions() {
|
||||||
|
@ -401,8 +395,8 @@ public class DbSchemaVersionCheck {
|
||||||
ZipEntry ze;
|
ZipEntry ze;
|
||||||
while ((ze = zip.getNextEntry()) != null) {
|
while ((ze = zip.getNextEntry()) != null) {
|
||||||
String entryName = ze.getName();
|
String entryName = ze.getName();
|
||||||
if (entryName.endsWith(".sql") && entryName.startsWith(this.app) && entryName
|
if (entryName.endsWith(".sql") && entryName.startsWith(this.app) && entryName.contains(
|
||||||
.contains("migration"))
|
"migration"))
|
||||||
versions.add(parseVersion(entryName));
|
versions.add(parseVersion(entryName));
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -463,7 +457,7 @@ public class DbSchemaVersionCheck {
|
||||||
throw new DbException("Failed to execute schema drop SQL: " + e.getMessage(), e);
|
throw new DbException("Failed to execute schema drop SQL: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(MessageFormat
|
logger.info(MessageFormat.format("[{0}:{1}] Successfully dropped schema with version {2}", this.app, realm,
|
||||||
.format("[{0}:{1}] Successfully dropped schema with version {2}", this.app, realm, version));
|
version));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue