Search in sources :

Example 46 with CustomChangeException

use of liquibase.exception.CustomChangeException in project webanno by webanno.

the class RemoveDanglingFeatures method execute.

@Override
public void execute(Database aDatabase) throws CustomChangeException {
    JdbcConnection dbConn = (JdbcConnection) aDatabase.getConnection();
    try {
        PreparedStatement selection = dbConn.prepareStatement(// 
        String.join(// 
        "\n", // 
        "SELECT f.id", // 
        "FROM annotation_feature f", // 
        "LEFT JOIN annotation_type l ON f.annotation_type = l.id \n", "WHERE l.id IS NULL"));
        PreparedStatement deletion = dbConn.prepareStatement("DELETE FROM annotation_feature WHERE id = ?");
        try (ResultSet danglingFeatures = selection.executeQuery()) {
            while (danglingFeatures.next()) {
                long danglingFeatureId = danglingFeatures.getLong(1);
                deletion.setLong(1, danglingFeatureId);
                deletion.execute();
                changed++;
            }
        }
        if (changed > 0) {
            log.info("DATABASE UPGRADE PERFORMED: [{}] dangling features were removed.", changed);
        }
    } catch (Exception e) {
    // swallow the exception !
    }
}
Also used : ResultSet(java.sql.ResultSet) JdbcConnection(liquibase.database.jvm.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) CustomChangeException(liquibase.exception.CustomChangeException) SetupException(liquibase.exception.SetupException)

Aggregations

CustomChangeException (liquibase.exception.CustomChangeException)46 PreparedStatement (java.sql.PreparedStatement)33 ResultSet (java.sql.ResultSet)26 DatabaseException (liquibase.exception.DatabaseException)26 SQLException (java.sql.SQLException)25 JdbcConnection (liquibase.database.jvm.JdbcConnection)21 SetupException (liquibase.exception.SetupException)16 Statement (java.sql.Statement)8 ArrayList (java.util.ArrayList)6 InsertStatement (liquibase.statement.core.InsertStatement)6 Table (liquibase.structure.core.Table)6 Date (java.sql.Date)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 BatchUpdateException (java.sql.BatchUpdateException)4 Connection (java.sql.Connection)4 HashSet (java.util.HashSet)4 List (java.util.List)4 UpdateStatement (liquibase.statement.core.UpdateStatement)4 SqlStatement (liquibase.statement.SqlStatement)3