Search in sources :

Example 1 with DataException

use of com.autentia.tnt.manager.data.exception.DataException in project TNTConcept by autentia.

the class ConsoleBean method migrateDB.

public String migrateDB() {
    MigrationManager mig = MigrationManager.getDefault();
    try {
        // Migrate database
        Version oldVersion = mig.upgradeDatabase();
        // Tell user
        FacesUtils.addInfoMessage(null, "msg.migrationSuccess", oldVersion, Version.getApplicationVersion());
        // Refresh lock filter state
        ApplicationLock.refresh();
    } catch (DataException e) {
        FacesUtils.addErrorMessage(null, "error.migration");
        log.error("migrateDB - exception", e);
    }
    return null;
}
Also used : DataException(com.autentia.tnt.manager.data.exception.DataException) Version(com.autentia.tnt.version.Version) MigrationManager(com.autentia.tnt.manager.data.MigrationManager)

Example 2 with DataException

use of com.autentia.tnt.manager.data.exception.DataException in project TNTConcept by autentia.

the class MigrationManager method upgradeDatabase.

/**
 * @return the original database version
 * @throws com.autentia.tnt.manager.data.exception.DataException
 */
public Version upgradeDatabase() throws DataException {
    Version ret = null;
    Version db = null;
    Version code = Version.getApplicationVersion();
    Session ses = HibernateUtil.getSessionFactory().openSession();
    Connection con = ses.connection();
    Statement stmt = null;
    LineNumberReader file = null;
    String delimiter = ";";
    try {
        db = Version.getDatabaseVersion();
        ret = db.clone();
        log.info("upgradeDatabase - >>>> STARTING MIGRATION FROM " + db + " TO " + code + " <<<<");
        // Disable auto-commit (just in case...)
        log.info("upgradeDatabase - disabling auto commit");
        con.setAutoCommit(false);
        // Create statement
        stmt = con.createStatement();
        // While necessary, upgrade database
        while (db.compareTo(code, Version.MINOR) < 0) {
            log.info("upgradeDatabase - " + db);
            // Compose script name and open it
            String script = SCRIPT_PREFIX + db.toString(Version.MINOR) + SCRIPT_SUFFIX;
            log.info("upgradeDatabase - loading script " + script);
            InputStream sqlScript = Thread.currentThread().getContextClassLoader().getResourceAsStream(script);
            if (sqlScript == null) {
                throw FileNotFoundException(script);
            }
            file = new LineNumberReader(new InputStreamReader(new BufferedInputStream(sqlScript), "UTF-8"));
            int _c;
            // Add batched SQL sentences to statement
            StringBuilder sentence = new StringBuilder();
            String line;
            while ((line = file.readLine()) != null) {
                line = line.trim();
                if (!line.startsWith("--")) {
                    // Interpret "DELIMITER" sentences
                    if (line.trim().toUpperCase(Locale.ENGLISH).startsWith("DELIMITER")) {
                        delimiter = line.trim().substring("DELIMITER".length()).trim();
                    } else {
                        // Add line to sentence
                        if (line.endsWith(delimiter)) {
                            // Remove delimiter
                            String lastLine = line.substring(0, line.length() - delimiter.length());
                            // Append line to sentence
                            sentence.append(lastLine);
                            // Execute sentence
                            log.info(" " + sentence.toString());
                            stmt.addBatch(sentence.toString());
                            // Prepare new sentence
                            sentence = new StringBuilder();
                        } else {
                            // Append line to sentence
                            sentence.append(line);
                            // Append separator for next line
                            sentence.append(" ");
                        }
                    }
                }
            }
            // Execute batch
            log.info("upgradeDatabase - executing batch of commands");
            stmt.executeBatch();
            // Re-read database version
            Version old = db;
            db = Version.getDatabaseVersion(con);
            if (old.equals(db)) {
                throw new DataException("Script was applied but did not upgrade database: " + script);
            }
            log.info("upgradeDatabase - database upgraded to version " + db);
        }
        // Commit transaction
        log.info("upgradeDatabase - commiting changes to database");
        con.commit();
        // Report end of migration
        log.info("upgradeDatabase - >>>> MIGRATION SUCCESSFULLY FINISHED <<<<");
    } catch (Exception e) {
        log.error("upgradeDatabase - >>>> MIGRATION FAILED: WILL BE ROLLED BACK <<<<", e);
        try {
            con.rollback();
        } catch (SQLException e2) {
            log.error("upgradeDatabase - Error al realizar el rollback");
        }
        throw new DataException("Script was applied but did not upgrade database: ", e);
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (IOException e2) {
                log.error("upgradeDatabase - Error al cerrar el fichero de script ", e2);
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e2) {
                log.error("upgradeDatabase - Error al cerrar el statement");
            }
        }
        ses.close();
    }
    return ret;
}
Also used : InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) Statement(java.sql.Statement) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) IOException(java.io.IOException) IOException(java.io.IOException) DataException(com.autentia.tnt.manager.data.exception.DataException) SQLException(java.sql.SQLException) LineNumberReader(java.io.LineNumberReader) DataException(com.autentia.tnt.manager.data.exception.DataException) Version(com.autentia.tnt.version.Version) BufferedInputStream(java.io.BufferedInputStream) Session(org.hibernate.Session)

Aggregations

DataException (com.autentia.tnt.manager.data.exception.DataException)2 Version (com.autentia.tnt.version.Version)2 MigrationManager (com.autentia.tnt.manager.data.MigrationManager)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 LineNumberReader (java.io.LineNumberReader)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Session (org.hibernate.Session)1