Search in sources :

Example 1 with DatabaseSchemaDefinition

use of com.serotonin.m2m2.module.DatabaseSchemaDefinition in project ma-core-public by infiniteautomation.

the class H2InMemoryDatabaseProxy method clean.

/**
 * @throws Exception
 */
public void clean() throws Exception {
    ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
    ejt.setDataSource(getDataSource());
    runScript(new String[] { "DROP ALL OBJECTS;" }, null);
    runScript(this.getClass().getResourceAsStream("/createTables-" + getType().name() + ".sql"), null);
    for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) def.newInstallationCheck(ejt);
    SystemSettingsDao.instance.setValue(SystemSettingsDao.DATABASE_SCHEMA_VERSION, Integer.toString(Common.getDatabaseSchemaVersion()));
    // Add the settings flag that this is a new instance. This flag is removed when an administrator
    // logs in.
    SystemSettingsDao.instance.setBooleanValue(SystemSettingsDao.NEW_INSTANCE, true);
    User user = new User();
    user.setId(Common.NEW_ID);
    user.setName("Administrator");
    user.setUsername("admin");
    user.setPassword(Common.encrypt("admin"));
    user.setEmail("admin@yourMangoDomain.com");
    user.setPhone("");
    user.setPermissions(SuperadminPermissionDefinition.GROUP_NAME);
    user.setDisabled(false);
    UserDao.instance.saveUser(user);
}
Also used : User(com.serotonin.m2m2.vo.User) DatabaseSchemaDefinition(com.serotonin.m2m2.module.DatabaseSchemaDefinition) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate)

Example 2 with DatabaseSchemaDefinition

use of com.serotonin.m2m2.module.DatabaseSchemaDefinition in project ma-core-public by infiniteautomation.

the class DBConvert method execute.

public void execute() throws SQLException {
    LOG.warn("Running database conversion from " + source.getType().name() + " to " + target.getType().name());
    // Create the connections
    Connection sourceConn = source.getDataSource().getConnection();
    sourceConn.setAutoCommit(true);
    Connection targetConn = target.getDataSource().getConnection();
    targetConn.setAutoCommit(false);
    List<String> tableNames = getCoreTableNames();
    for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) def.addConversionTableNames(tableNames);
    for (String tableName : tableNames) copyTable(sourceConn, targetConn, tableName);
    sourceConn.close();
    targetConn.close();
    LOG.warn("Completed database conversion");
}
Also used : DatabaseSchemaDefinition(com.serotonin.m2m2.module.DatabaseSchemaDefinition) Connection(java.sql.Connection)

Example 3 with DatabaseSchemaDefinition

use of com.serotonin.m2m2.module.DatabaseSchemaDefinition in project ma-core-public by infiniteautomation.

the class H2InMemoryDatabaseProxy method initialize.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.db.DatabaseProxy#initialize(java.lang.ClassLoader)
     */
@Override
public void initialize(ClassLoader classLoader) {
    JdbcDataSource jds = new JdbcDataSource();
    String url = "jdbc:h2:mem:" + databaseName + ";DB_CLOSE_DELAY=-1";
    jds.setUrl(url);
    dataSource = JdbcConnectionPool.create(jds);
    transactionManager = new DataSourceTransactionManager(dataSource);
    if (initWebConsole) {
        String[] webArgs = new String[4];
        webArgs[0] = "-webPort";
        webArgs[1] = webPort.toString();
        webArgs[2] = "-ifExists";
        webArgs[3] = "-webAllowOthers";
        try {
            this.web = Server.createWebServer(webArgs);
            this.web.start();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
    ejt.setDataSource(getDataSource());
    // Create the empty database
    if (!tableExists(ejt, SchemaDefinition.USERS_TABLE)) {
        // The users table wasn't found, so assume that this is a new instance.
        // Create the tables
        runScript(this.getClass().getResourceAsStream("/createTables-" + getType().name() + ".sql"), null);
        for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) def.newInstallationCheck(ejt);
        SystemSettingsDao.instance.setValue(SystemSettingsDao.DATABASE_SCHEMA_VERSION, Integer.toString(Common.getDatabaseSchemaVersion()));
        SystemSettingsDao.instance.setValue(SystemSettingsDao.BACKUP_ENABLED, "false");
        SystemSettingsDao.instance.setValue(SystemSettingsDao.DATABASE_BACKUP_ENABLED, "false");
        // Add the settings flag that this is a new instance. This flag is removed when an administrator
        // logs in.
        SystemSettingsDao.instance.setBooleanValue(SystemSettingsDao.NEW_INSTANCE, true);
        Providers.get(IMangoLifecycle.class).addStartupTask(new Runnable() {

            @Override
            public void run() {
                // New database. Create a default user.
                User user = new User();
                user.setId(Common.NEW_ID);
                user.setName("Administrator");
                user.setUsername("admin");
                user.setPassword(Common.encrypt("admin"));
                user.setEmail("admin@yourMangoDomain.com");
                user.setPhone("");
                user.setPermissions(SuperadminPermissionDefinition.GROUP_NAME);
                user.setDisabled(false);
                UserDao.instance.saveUser(user);
                DefaultDataPointPropertiesTemplateFactory factory = new DefaultDataPointPropertiesTemplateFactory();
                factory.saveDefaultTemplates();
            }
        });
    }
    // Check if we are using NoSQL
    if (NoSQLProxyFactory.instance.getProxy() != null) {
        noSQLProxy = NoSQLProxyFactory.instance.getProxy();
        noSQLProxy.initialize();
    }
    initialized = true;
}
Also used : User(com.serotonin.m2m2.vo.User) SQLException(java.sql.SQLException) DatabaseSchemaDefinition(com.serotonin.m2m2.module.DatabaseSchemaDefinition) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) DefaultDataPointPropertiesTemplateFactory(com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Example 4 with DatabaseSchemaDefinition

use of com.serotonin.m2m2.module.DatabaseSchemaDefinition in project ma-core-public by infiniteautomation.

the class AbstractDatabaseProxy method initialize.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.db.DatabaseProxy#initialize(java.lang.ClassLoader)
     */
@Override
public void initialize(ClassLoader classLoader) {
    initializeImpl("");
    useMetrics = Common.envProps.getBoolean("db.useMetrics", false);
    ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
    ejt.setDataSource(getDataSource());
    transactionManager = new DataSourceTransactionManager(getDataSource());
    try {
        if (newDatabaseCheck(ejt)) {
            // Check if we should convert from another database.
            String convertTypeStr = null;
            try {
                convertTypeStr = Common.envProps.getString("convert.db.type");
            } catch (MissingResourceException e) {
                convertTypeStr = "";
            }
            if (!StringUtils.isBlank(convertTypeStr)) {
                // Found a database type from which to convert.
                DatabaseType convertType = DatabaseType.valueOf(convertTypeStr.toUpperCase());
                if (convertType == null)
                    throw new IllegalArgumentException("Unknown convert database type: " + convertType);
                // TODO check that the convert source has the current DB version, or upgrade it if not.
                AbstractDatabaseProxy sourceProxy = convertType.getImpl();
                sourceProxy.initializeImpl("convert.");
                DBConvert convert = new DBConvert();
                convert.setSource(sourceProxy);
                convert.setTarget(this);
                try {
                    convert.execute();
                } catch (SQLException e) {
                    throw new ShouldNeverHappenException(e);
                }
                sourceProxy.terminate(false);
            } else {
                // Record the current version.
                SystemSettingsDao.instance.setValue(SystemSettingsDao.DATABASE_SCHEMA_VERSION, Integer.toString(Common.getDatabaseSchemaVersion()));
                // Add the settings flag that this is a new instance. This flag is removed when an administrator
                // logs in.
                SystemSettingsDao.instance.setBooleanValue(SystemSettingsDao.NEW_INSTANCE, true);
                /**
                 * Add a startup task to run after the Audit system is ready
                 */
                Providers.get(IMangoLifecycle.class).addStartupTask(new Runnable() {

                    @Override
                    public void run() {
                        // New database. Create a default user.
                        User user = new User();
                        user.setId(Common.NEW_ID);
                        user.setName("Administrator");
                        user.setUsername("admin");
                        user.setPassword(Common.encrypt("admin"));
                        user.setEmail("admin@yourMangoDomain.com");
                        user.setPhone("");
                        user.setPermissions(SuperadminPermissionDefinition.GROUP_NAME);
                        user.setDisabled(false);
                        user.setHomeUrl("/ui/administration/home");
                        UserDao.instance.saveUser(user);
                        DefaultDataPointPropertiesTemplateFactory factory = new DefaultDataPointPropertiesTemplateFactory();
                        factory.saveDefaultTemplates();
                    }
                });
            }
        } else
            // The database exists, so let's make its schema version matches the application version.
            DBUpgrade.checkUpgrade();
        // Check if we are using NoSQL
        if (NoSQLProxyFactory.instance.getProxy() != null) {
            noSQLProxy = NoSQLProxyFactory.instance.getProxy();
            noSQLProxy.initialize();
        }
    } catch (CannotGetJdbcConnectionException e) {
        log.fatal("Unable to connect to database of type " + getType().name(), e);
        throw e;
    } catch (Exception e) {
        log.fatal("Exception initializing database proxy: " + e.getMessage(), e);
        throw e;
    }
    // Allow modules to upgrade themselves
    for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) DBUpgrade.checkUpgrade(def, classLoader);
    postInitialize(ejt);
}
Also used : User(com.serotonin.m2m2.vo.User) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) SQLException(java.sql.SQLException) DatabaseSchemaDefinition(com.serotonin.m2m2.module.DatabaseSchemaDefinition) MissingResourceException(java.util.MissingResourceException) DefaultDataPointPropertiesTemplateFactory(com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) SQLException(java.sql.SQLException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) MissingResourceException(java.util.MissingResourceException) FileNotFoundException(java.io.FileNotFoundException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Aggregations

DatabaseSchemaDefinition (com.serotonin.m2m2.module.DatabaseSchemaDefinition)4 ExtendedJdbcTemplate (com.serotonin.db.spring.ExtendedJdbcTemplate)3 User (com.serotonin.m2m2.vo.User)3 DefaultDataPointPropertiesTemplateFactory (com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory)2 SQLException (java.sql.SQLException)2 DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 IMangoLifecycle (com.serotonin.m2m2.IMangoLifecycle)1 FileNotFoundException (java.io.FileNotFoundException)1 Connection (java.sql.Connection)1 MissingResourceException (java.util.MissingResourceException)1 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)1 CannotGetJdbcConnectionException (org.springframework.jdbc.CannotGetJdbcConnectionException)1