/* (non-Javadoc)
* @see com.dotmarketing.startup.StartupTask#executeUpgrade()
*/
public void executeUpgrade() throws DotDataException, DotRuntimeException{
DotConnect dc = new DotConnect();
Connection conn = null;
List<PrimaryKey> primaryKeys=null;
List<ForeignKey> foreignKeys=null;
List<Index> indexes=null;
try {
conn = DbConnectionFactory.getDataSource().getConnection();
conn.setAutoCommit(true);
List<String> tables = getTablesToDropConstraints();
if(tables!=null){
foreignKeys=getForeingKeys(conn, tables,true);
//conn.commit();
primaryKeys=getPrimaryKey(conn, tables, true);
//conn.commit();
indexes=getIndexes(conn, tables,true);
//conn.commit();
if(DbConnectionFactory.isMsSql())
// for mssql we pass again as we might have index dependencies
getPrimaryKey(conn, tables, true);
}
//conn.commit();
} catch (Exception e) {
/*try {
conn.rollback();
}
catch(SQLException ex) {}*/
throw new DotDataException(e.getMessage(), e);
}
finally {
try {
conn.close();
}
catch(SQLException ex) {
throw new DotDataException(ex.getMessage(), ex);
}
}
List<String> schemaList = new ArrayList<String>();
//Execute the SQL Script in accordance with the database type
if(DbConnectionFactory.isPostgres()){
schemaList = SQLUtil.tokenize(getPostgresScript());
}else if(DbConnectionFactory.isMySql()){
schemaList = SQLUtil.tokenize(getMySQLScript());
}else if(DbConnectionFactory.isOracle()){
schemaList = SQLUtil.tokenize(getOracleScript());
}else if(DbConnectionFactory.isMsSql()) {
schemaList = SQLUtil.tokenize(getMSSQLScript());
}else {
schemaList = SQLUtil.tokenize(getH2Script());
}
try {
conn = DbConnectionFactory.getDataSource().getConnection();
conn.setAutoCommit(false);
if(DbConnectionFactory.isMySql()){
dc.executeStatement("SET storage_engine=INNODB", conn);
}else if(DbConnectionFactory.isMsSql()){
dc.executeStatement("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;", conn);
}
for (String query : schemaList) {
if(!runInSingleTransaction){
try {
HibernateUtil.startTransaction();
dc.executeStatement(query);
} catch (Exception e) {
Logger.error(this, "Unable to execute query : " + query);
HibernateUtil.rollbackTransaction();
continue;
}
HibernateUtil.commitTransaction();
} else {
try {
dc.executeStatement(query, conn);
} catch (SQLException e) {
Logger.fatal(this, "Unable to execute SQL upgrade", e);
throw new DotDataException(e.getMessage(), e);
}
}