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);
}
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");
}
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;
}
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);
}
Aggregations