use of liquibase.exception.DatabaseException in project liquibase by liquibase.
the class ExampleExecutor method execute.
/**
* Execute the SQL from the <a href="#{@link}">{@link SqlStatement}</a> parameter
*
* @param action This is the SqlStatement object which contains
* the SQL to execute
* @param sqlVisitors List of <a href="#{@link}">{@link SqlVisitor}</a> to apply to the generated SQL
* @throws DatabaseException Exception type thrown if an error occurs during execution
*/
@Override
public void execute(SqlStatement action, List<SqlVisitor> sqlVisitors) throws DatabaseException {
Scope.getCurrentScope().getLog(getClass()).info("Executing with the '" + getName() + "' executor");
Sql[] sqls = SqlGeneratorFactory.getInstance().generateSql(action, database);
try {
for (Sql sql : sqls) {
String actualSqlString = sql.toSql();
for (SqlVisitor visitor : sqlVisitors) {
visitor.modifySql(actualSqlString, database);
}
Scope.getCurrentScope().getLog(getClass()).info("Generated SQL for change is " + actualSqlString);
super.execute(action, sqlVisitors);
}
} catch (Exception e) {
throw new DatabaseException(e);
}
}
use of liquibase.exception.DatabaseException in project liquibase by liquibase.
the class AbstractSQLChange method generateStatements.
/**
* Generates one or more SqlStatements depending on how the SQL should be parsed.
* If split statements is set to true then the SQL is split and each command is made into a separate SqlStatement.
* <p></p>
* If stripping comments is true then any comments are removed before the splitting is executed.
* The set SQL is passed through the {@link java.sql.Connection#nativeSQL} method if a connection is available.
*/
@Override
public SqlStatement[] generateStatements(Database database) {
List<SqlStatement> returnStatements = new ArrayList<>();
String sql = StringUtil.trimToNull(getSql());
if (sql == null) {
return new SqlStatement[0];
}
String processedSQL = normalizeLineEndings(sql);
if (this instanceof RawSQLChange && ((RawSQLChange) this).isRerunnable()) {
returnStatements.add(new RawSqlStatement(processedSQL, getEndDelimiter()));
return returnStatements.toArray(new SqlStatement[returnStatements.size()]);
}
for (String statement : StringUtil.processMultiLineSQL(processedSQL, isStripComments(), isSplitStatements(), getEndDelimiter())) {
if (database instanceof MSSQLDatabase) {
statement = statement.replaceAll("\\n", "\r\n");
}
String escapedStatement = statement;
try {
if (database.getConnection() != null) {
escapedStatement = database.getConnection().nativeSQL(statement);
}
} catch (DatabaseException e) {
escapedStatement = statement;
}
returnStatements.add(new RawSqlStatement(escapedStatement, getEndDelimiter()));
}
return returnStatements.toArray(new SqlStatement[returnStatements.size()]);
}
use of liquibase.exception.DatabaseException in project liquibase by liquibase.
the class AddAutoIncrementChange method checkStatus.
@Override
public ChangeStatus checkStatus(Database database) {
ChangeStatus result = new ChangeStatus();
Column example = new Column(Table.class, getCatalogName(), getSchemaName(), getTableName(), getColumnName());
try {
Column column = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database);
if (column == null) {
return result.unknown("Column does not exist");
}
result.assertComplete(column.isAutoIncrement(), "Column is not auto-increment");
if ((getStartWith() != null) && (column.getAutoIncrementInformation().getStartWith() != null)) {
result.assertCorrect(getStartWith().equals(column.getAutoIncrementInformation().getStartWith()), "startsWith incorrect");
}
if ((getIncrementBy() != null) && (column.getAutoIncrementInformation().getIncrementBy() != null)) {
result.assertCorrect(getIncrementBy().equals(column.getAutoIncrementInformation().getIncrementBy()), "Increment by incorrect");
}
if (getGenerationType() != null && column.getAutoIncrementInformation().getGenerationType() != null) {
result.assertCorrect(getGenerationType().equals(column.getAutoIncrementInformation().getGenerationType()), "Generation type is incorrect");
}
if (getDefaultOnNull() != null && column.getAutoIncrementInformation().getDefaultOnNull() != null) {
result.assertCorrect(getDefaultOnNull().equals(column.getAutoIncrementInformation().getDefaultOnNull()), "Default on null is incorrect");
}
return result;
} catch (DatabaseException | InvalidExampleException e) {
return result.unknown(e);
}
}
use of liquibase.exception.DatabaseException in project liquibase by liquibase.
the class StandardLockServiceTest method before.
@Before
public void before() throws Exception {
Executor executor = Mockito.mock(Executor.class);
Mockito.when(executor.queryForList(Mockito.any())).thenReturn(sampleLockData());
ExecutorService executorService = Mockito.mock(ExecutorService.class);
Mockito.when(executorService.getExecutor(Mockito.any(), Mockito.any())).thenReturn(executor);
Map<String, Object> scopeValues = new TreeMap<>();
scopeValues.put(ExecutorService.class.getName(), executorService);
mockedScope = Mockito.mock(Scope.class);
Mockito.when(mockedScope.getSingleton(ExecutorService.class)).thenReturn(executorService);
lockService = new StandardLockService() {
@Override
public boolean hasDatabaseChangeLogLockTable() throws DatabaseException {
return true;
}
};
}
use of liquibase.exception.DatabaseException in project liquibase by liquibase.
the class OfflineDatabaseTest method canOutputSQLFromOfflineOracleDB.
/**
* Check if it it's possible to output SQL from an OfflineConnection
* set to Oracle (offline:oracle).
*
* @see <a href="https://liquibase.jira.com/browse/CORE-2192">CORE-2192</a>
*/
@Test
public void canOutputSQLFromOfflineOracleDB() throws Exception {
AddColumnChange change = new AddColumnChange();
AddColumnConfig column1 = new AddColumnConfig();
column1.setName("column1");
column1.setType("INT");
change.addColumn(column1);
AddColumnConfig column2 = new AddColumnConfig();
column2.setName("column2");
column2.setType("INT");
change.addColumn(column2);
SqlStatement[] statements = new SqlStatement[0];
try {
statements = change.generateStatements(createOfflineDatabase("offline:oracle"));
} catch (DatabaseException e) {
Assert.fail("Can't generate statements from an Offline Oracle database.");
}
Assert.assertEquals(1, statements.length);
Assert.assertTrue(statements[0] instanceof AddColumnStatement);
AddColumnStatement stmt = (AddColumnStatement) statements[0];
Assert.assertTrue(stmt.isMultiple());
Assert.assertEquals(2, stmt.getColumns().size());
}
Aggregations