use of com.serotonin.m2m2.db.upgrade.DatabaseSchemaUpgrader in project ma-core-public by infiniteautomation.
the class AbstractDatabaseProxy method initialize.
@PostConstruct
@Override
public void initialize() {
initializeImpl();
DataSource dataSource = getDataSource();
this.jdbcTemplate = new ExtendedJdbcTemplate(dataSource);
this.context = DSL.using(getConfig());
this.transactionManager = new DataSourceTransactionManager(dataSource);
SystemSettingsAccessor systemSettingsAccessor = () -> context;
DatabaseSchemaUpgrader upgrader = new DatabaseSchemaUpgrader(this, systemSettingsAccessor, classLoader);
// VO objects stored in blobs
try {
systemSettingsAccessor.getSystemSetting(SystemSettingsDao.LANGUAGE).ifPresent(Common::setSystemLanguage);
} catch (DataAccessException e) {
// that's ok, table probably doesn't exist yet
}
// First confirm that if we are MySQL we have JSON Support
if (getType().name().equals(DatabaseType.MYSQL.name())) {
try {
runScript(new String[] { "CREATE TABLE mangoUpgrade28 (test JSON)engine=InnoDB;", "DROP TABLE mangoUpgrade28;" }, null);
} catch (BadSqlGrammarException e) {
String version = "?";
try {
DatabaseMetaData dmd = getDataSource().getConnection().getMetaData();
version = dmd.getDatabaseProductVersion();
} catch (Exception ex) {
log.error("Failed to create test table for JSON compatibility" + ex);
}
throw new ShouldNeverHappenException("Unable to start Mango, MySQL version must be at least 5.7.8 to support JSON columns. Your version is " + version);
}
}
try {
boolean newDatabase = false;
String convertTypeStr = env.getProperty(propertyPrefix + "convert.db.type");
boolean willConvert = false;
if (!databaseExists()) {
if (!restoreTables()) {
createTables();
// Check if we should convert from another database.
if (!StringUtils.isBlank(convertTypeStr)) {
willConvert = true;
} else {
newDatabase = true;
}
}
}
if (newDatabase) {
doInTransaction(txStatus -> {
initializeCoreDatabase(context);
});
} else if (!willConvert) {
// Make sure the core schema version matches the application version. If we are running a conversion
// then the responsibility is on the User to be converting from a compatible version
upgrader.checkCoreUpgrade();
}
// Ensure the modules are installed after the core schema is updated
for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) {
try {
def.setDatabaseProxy(this);
def.newInstallationCheck();
} catch (Exception e) {
log.error("Module " + def.getModule().getName() + " new installation check failed", e);
}
}
if (!willConvert) {
// Allow modules to upgrade their schemas
for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) {
upgrader.checkModuleUpgrade(def);
}
}
if (willConvert) {
// Found a database type from which to convert.
DatabaseType convertType = DatabaseType.valueOf(convertTypeStr.toUpperCase());
// TODO check that the convert source has the current DB version, or upgrade it if not.
AbstractDatabaseProxy sourceProxy = (AbstractDatabaseProxy) factory.createDatabaseProxy(convertType, propertyPrefix + "convert.");
try {
sourceProxy.initialize();
DBConvert convert = new DBConvert();
convert.setSource(sourceProxy);
convert.setTarget(this);
try {
convert.execute();
} catch (SQLException e) {
throw new ShouldNeverHappenException(e);
}
} finally {
sourceProxy.terminate();
}
}
listeners.forEach(l -> l.onInitialize(this));
} catch (CannotGetJdbcConnectionException e) {
log.error("Unable to connect to database of type " + getType().name(), e);
throw e;
} catch (IOException e) {
log.error("Exception initializing database proxy: " + e.getMessage(), e);
throw new UncheckedIOException(e);
} catch (Exception e) {
log.error("Exception initializing database proxy: " + e.getMessage(), e);
throw e;
}
}
Aggregations