Search in sources :

Example 61 with DatabaseException

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);
    }
}
Also used : DatabaseException(liquibase.exception.DatabaseException) SqlVisitor(liquibase.sql.visitor.SqlVisitor) DatabaseException(liquibase.exception.DatabaseException) Sql(liquibase.sql.Sql)

Example 62 with DatabaseException

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()]);
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) RawSQLChange(liquibase.change.core.RawSQLChange) ArrayList(java.util.ArrayList) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) DatabaseException(liquibase.exception.DatabaseException)

Example 63 with DatabaseException

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);
    }
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) Column(liquibase.structure.core.Column) DatabaseException(liquibase.exception.DatabaseException)

Example 64 with DatabaseException

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;
        }
    };
}
Also used : Executor(liquibase.executor.Executor) Scope(liquibase.Scope) ExecutorService(liquibase.executor.ExecutorService) TreeMap(java.util.TreeMap) DatabaseException(liquibase.exception.DatabaseException) Before(org.junit.Before)

Example 65 with DatabaseException

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());
}
Also used : SqlStatement(liquibase.statement.SqlStatement) AddColumnConfig(liquibase.change.AddColumnConfig) AddColumnChange(liquibase.change.core.AddColumnChange) DatabaseException(liquibase.exception.DatabaseException) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Aggregations

DatabaseException (liquibase.exception.DatabaseException)139 SQLException (java.sql.SQLException)65 JdbcConnection (liquibase.database.jvm.JdbcConnection)34 PreparedStatement (java.sql.PreparedStatement)33 ResultSet (java.sql.ResultSet)28 Statement (java.sql.Statement)24 Database (liquibase.database.Database)24 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)24 CustomChangeException (liquibase.exception.CustomChangeException)22 CatalogAndSchema (liquibase.CatalogAndSchema)17 LiquibaseException (liquibase.exception.LiquibaseException)16 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)14 InvalidExampleException (liquibase.snapshot.InvalidExampleException)14 RawSqlStatement (liquibase.statement.core.RawSqlStatement)14 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)13 CachedRow (liquibase.snapshot.CachedRow)13 SqlStatement (liquibase.statement.SqlStatement)13 DatabaseConnection (liquibase.database.DatabaseConnection)12 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)12 ArrayList (java.util.ArrayList)11