use of org.jumpmind.db.sql.SqlScript in project symmetric-ds by JumpMind.
the class ConfigDatabaseUpgrader method upgrade.
public boolean upgrade() {
try {
if (logOutput) {
log.info("Checking if config tables need created or altered");
}
Database modelFromXml = configDatabasePlatform.readDatabaseFromXml(schemaXml, true);
configDatabasePlatform.prefixDatabase(tablePrefix, modelFromXml);
Database modelFromDatabase = configDatabasePlatform.readFromDatabase(modelFromXml.getTables());
IDdlBuilder builder = configDatabasePlatform.getDdlBuilder();
if (builder.isAlterDatabase(modelFromDatabase, modelFromXml)) {
if (logOutput) {
log.info("There are config tables that needed altered");
}
String delimiter = configDatabasePlatform.getDatabaseInfo().getSqlCommandDelimiter();
String alterSql = builder.alterDatabase(modelFromDatabase, modelFromXml);
log.debug("Alter SQL generated: {}", alterSql);
SqlScript script = new SqlScript(alterSql, configDatabasePlatform.getSqlTemplate(), true, false, false, delimiter, null);
if (logOutput) {
script.setListener(new LogSqlResultsListener(log));
}
script.execute(configDatabasePlatform.getDatabaseInfo().isRequiresAutoCommitForDdl());
if (logOutput) {
log.info("Done with auto update of config tables");
}
return true;
} else {
return false;
}
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
use of org.jumpmind.db.sql.SqlScript in project symmetric-ds by JumpMind.
the class DatabasePlatformTest method testUpgradePrimaryKeyAutoIncrementFromIntToBigInt.
@Test
public void testUpgradePrimaryKeyAutoIncrementFromIntToBigInt() throws Exception {
boolean upgradeSupported = !platform.getName().equals(DatabaseNamesConstants.DERBY) && !platform.getName().equals(DatabaseNamesConstants.HSQLDB2) && !platform.getName().equals(DatabaseNamesConstants.INFORMIX) && !platform.getName().equals(DatabaseNamesConstants.DB2) && !platform.getName().equals(DatabaseNamesConstants.ASE) && !platform.getName().equals(DatabaseNamesConstants.MSSQL2000) && !platform.getName().equals(DatabaseNamesConstants.MSSQL2005) && !platform.getName().equals(DatabaseNamesConstants.MSSQL2008) && !platform.getName().equals(DatabaseNamesConstants.SQLANYWHERE);
if (upgradeSupported) {
Table table = new Table("TEST_UPGRADE");
table.addColumn(new Column("ID", true));
table.getColumnWithName("ID").setTypeCode(Types.INTEGER);
table.getColumnWithName("ID").setAutoIncrement(true);
table.getColumnWithName("ID").setRequired(true);
table.addColumn(new Column("NOTES"));
table.getColumnWithName("NOTES").setTypeCode(Types.VARCHAR);
table.getColumnWithName("NOTES").setSize("100");
Table tableFromDatabase = dropCreateAndThenReadTable(table);
assertNotNull(tableFromDatabase);
assertTrue(tableFromDatabase.getColumnWithName("ID").isPrimaryKey());
String insertSql = "insert into \"TEST_UPGRADE\" (\"ID\",\"NOTES\") values(null,?)";
insertSql = insertSql.replaceAll("\"", platform.getDatabaseInfo().getDelimiterToken());
long id1 = platform.getSqlTemplate().insertWithGeneratedKey(insertSql, "ID", getSequenceName(platform), new Object[] { "test" }, new int[] { Types.VARCHAR });
table.getColumnWithName("ID").setTypeCode(Types.BIGINT);
IDdlBuilder builder = platform.getDdlBuilder();
String alterSql = builder.alterTable(tableFromDatabase, table);
assertFalse(alterSql, alterSql.toLowerCase().contains("create table"));
new SqlScript(alterSql, platform.getSqlTemplate(), true, platform.getSqlScriptReplacementTokens()).execute(true);
tableFromDatabase = platform.getTableFromCache(table.getName(), true);
assertEquals(Types.BIGINT, table.getColumnWithName("ID").getMappedTypeCode());
assertTrue(tableFromDatabase.getColumnWithName("ID").isPrimaryKey());
long id2 = platform.getSqlTemplate().insertWithGeneratedKey(insertSql, "ID", getSequenceName(platform), new Object[] { "test" }, new int[] { Types.VARCHAR });
assertNotSame(id1, id2);
}
}
use of org.jumpmind.db.sql.SqlScript in project symmetric-ds by JumpMind.
the class AbstractSymmetricEngine method loadFromScriptIfProvided.
/**
* Give the end user the option to provide a script that will load a
* registration server with an initial SymmetricDS setup.
*
* Look first on the file system, then in the classpath for the SQL file.
*
* @return true if the script was executed
*/
protected boolean loadFromScriptIfProvided() {
boolean loaded = false;
String sqlScripts = parameterService.getString(ParameterConstants.AUTO_CONFIGURE_REG_SVR_SQL_SCRIPT);
if (!StringUtils.isBlank(sqlScripts)) {
String[] sqlScriptList = sqlScripts.split(",");
for (String sqlScript : sqlScriptList) {
sqlScript = sqlScript.trim();
if (StringUtils.isNotBlank(sqlScript)) {
File file = new File(sqlScript);
URL fileUrl = null;
if (file.isFile()) {
try {
fileUrl = file.toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
} else {
fileUrl = getClass().getResource(sqlScript);
if (fileUrl == null) {
fileUrl = Thread.currentThread().getContextClassLoader().getResource(sqlScript);
}
}
if (fileUrl != null) {
new SqlScript(fileUrl, symmetricDialect.getPlatform().getSqlTemplate(), true, SqlScriptReader.QUERY_ENDS, getSymmetricDialect().getPlatform().getSqlScriptReplacementTokens()).execute();
loaded = true;
} else {
log.info("Could not find the sql script: {} to execute. We would have run it if we had found it");
}
}
}
}
return loaded;
}
use of org.jumpmind.db.sql.SqlScript in project symmetric-ds by JumpMind.
the class AbstractSymmetricDialect method createOrAlterTablesIfNecessary.
/*
* @return true if SQL was executed.
*/
public boolean createOrAlterTablesIfNecessary(String... tableNames) {
try {
log.info("Checking if SymmetricDS tables need created or altered");
Database modelFromXml = readSymmetricSchemaFromXml();
Database modelFromDatabase = readSymmetricSchemaFromDatabase();
if (tableNames != null && tableNames.length > 0) {
tableNames = platform.alterCaseToMatchDatabaseDefaultCase(tableNames);
modelFromXml.removeAllTablesExcept(tableNames);
modelFromDatabase.removeAllTablesExcept(tableNames);
}
IDdlBuilder builder = platform.getDdlBuilder();
List<IAlterDatabaseInterceptor> alterDatabaseInterceptors = extensionService.getExtensionPointList(IAlterDatabaseInterceptor.class);
IAlterDatabaseInterceptor[] interceptors = alterDatabaseInterceptors.toArray(new IAlterDatabaseInterceptor[alterDatabaseInterceptors.size()]);
if (builder.isAlterDatabase(modelFromDatabase, modelFromXml, interceptors)) {
String delimiter = platform.getDatabaseInfo().getSqlCommandDelimiter();
ISqlResultsListener resultsListener = new LogSqlResultsListener(log);
List<IDatabaseUpgradeListener> databaseUpgradeListeners = extensionService.getExtensionPointList(IDatabaseUpgradeListener.class);
for (IDatabaseUpgradeListener listener : databaseUpgradeListeners) {
String sql = listener.beforeUpgrade(this, this.parameterService.getTablePrefix(), modelFromDatabase, modelFromXml);
SqlScript script = new SqlScript(sql, getPlatform().getSqlTemplate(), true, false, false, delimiter, null);
script.setListener(resultsListener);
script.execute(platform.getDatabaseInfo().isRequiresAutoCommitForDdl());
}
String alterSql = builder.alterDatabase(modelFromDatabase, modelFromXml, interceptors);
if (isNotBlank(alterSql)) {
log.info("There are SymmetricDS tables that needed altered");
log.debug("Alter SQL generated: {}", alterSql);
SqlScript script = new SqlScript(alterSql, getPlatform().getSqlTemplate(), true, false, false, delimiter, null);
script.setListener(resultsListener);
script.execute(platform.getDatabaseInfo().isRequiresAutoCommitForDdl());
for (IDatabaseUpgradeListener listener : databaseUpgradeListeners) {
String sql = listener.afterUpgrade(this, this.parameterService.getTablePrefix(), modelFromXml);
script = new SqlScript(sql, getPlatform().getSqlTemplate(), true, false, false, delimiter, null);
script.setListener(resultsListener);
script.execute(platform.getDatabaseInfo().isRequiresAutoCommitForDdl());
}
log.info("Done with auto update of SymmetricDS tables");
return true;
} else {
return false;
}
} else {
return false;
}
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
use of org.jumpmind.db.sql.SqlScript in project symmetric-ds by JumpMind.
the class TestSetupUtil method dropDatabaseTables.
public static IDatabasePlatform dropDatabaseTables(String databaseType, ISymmetricEngine engine) {
ISymmetricDialect dialect = engine.getSymmetricDialect();
AbstractJdbcDatabasePlatform platform = (AbstractJdbcDatabasePlatform) dialect.getPlatform();
engine.uninstall();
platform.resetDataSource();
IDdlBuilder builder = platform.getDdlBuilder();
Database db2drop = platform.readDatabase(platform.getDefaultCatalog(), platform.getDefaultSchema(), new String[] { "TABLE" });
platform.resetDataSource();
String sql = builder.dropTables(db2drop);
SqlScript dropScript = new SqlScript(sql, platform.getSqlTemplate(), false, platform.getSqlScriptReplacementTokens());
dropScript.execute(true);
platform.resetDataSource();
dialect.cleanDatabase();
platform.resetCachedTableModel();
return platform;
}
Aggregations