Search in sources :

Example 1 with SystemSettingsAccessor

use of com.serotonin.m2m2.db.upgrade.SystemSettingsAccessor 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;
    }
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) SQLException(java.sql.SQLException) DatabaseSchemaDefinition(com.serotonin.m2m2.module.DatabaseSchemaDefinition) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) DataAccessException(org.jooq.exception.DataAccessException) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) IOException(java.io.IOException) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) UncheckedIOException(java.io.UncheckedIOException) DataSource(javax.sql.DataSource) Common(com.serotonin.m2m2.Common) SystemSettingsAccessor(com.serotonin.m2m2.db.upgrade.SystemSettingsAccessor) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) DataAccessException(org.jooq.exception.DataAccessException) DatabaseSchemaUpgrader(com.serotonin.m2m2.db.upgrade.DatabaseSchemaUpgrader) PostConstruct(javax.annotation.PostConstruct)

Aggregations

ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 ExtendedJdbcTemplate (com.serotonin.db.spring.ExtendedJdbcTemplate)1 Common (com.serotonin.m2m2.Common)1 DatabaseSchemaUpgrader (com.serotonin.m2m2.db.upgrade.DatabaseSchemaUpgrader)1 SystemSettingsAccessor (com.serotonin.m2m2.db.upgrade.SystemSettingsAccessor)1 DatabaseSchemaDefinition (com.serotonin.m2m2.module.DatabaseSchemaDefinition)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 PostConstruct (javax.annotation.PostConstruct)1 DataSource (javax.sql.DataSource)1 DataAccessException (org.jooq.exception.DataAccessException)1 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)1 CannotGetJdbcConnectionException (org.springframework.jdbc.CannotGetJdbcConnectionException)1 DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)1