Skip to content

Commit

Permalink
#20267 Setting autocommit=false before creating additional_info column (
Browse files Browse the repository at this point in the history
#20271)

* #20267 Setting TRANSACTION ISOLATION LEVEL READ COMMITTED to avoid exception when the additional_info column is created

* run-all

* #20267 Improving fix, just setting autocommit=false

* run-all

Co-authored-by: Nollymar Longa <>
  • Loading branch information
nollymar committed Apr 19, 2021
1 parent 87199b1 commit e190ace
Showing 1 changed file with 44 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.dotmarketing.util.Logger;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
Expand All @@ -22,22 +23,57 @@ public class Task210218MigrateUserProxyTable implements StartupTask {
private static final ObjectMapper mapper = DotObjectMapperProvider.getInstance()
.getDefaultObjectMapper();

private static final String POSTGRES_SCRIPT = "alter table user_ add additional_info JSONB NULL;";

private static final String MYSQL_SCRIPT = "alter table user_ add additional_info text NULL;";

private static final String ORACLE_SCRIPT = "alter table user_ add additional_info NCLOB NULL;";

private static final String MSSQL_SCRIPT = "alter table user_ add additional_info NVARCHAR(MAX) NULL;";

@Override
public void executeUpgrade() throws DotDataException, DotRuntimeException {

createAdditionalInfoColumn();
migrateDataFromUserProxyToUser();
try{
migrateDataFromUserProxyToUser();
} catch(Exception e){
Logger.error(this, "Unable to execute SQL upgrade", e);
throw e;
}
}

/**
* Creates the `additional_info` column in the `user_` table and migrates existing data from `user_proxy`
* to `user_`. The migrated data will be stored in the new column
* @throws DotDataException
*/
private void migrateDataFromUserProxyToUser() throws DotDataException {

DotConnect dotConnect = new DotConnect();
// retrieves existing additional info from user_proxy table
final List<Map<String, Object>> additionalInfoMaps = dotConnect.setSQL(
"select * from user_proxy").loadObjectResults();

if (null != additionalInfoMaps) {
dotConnect = new DotConnect();

Connection connection;
try {

DbConnectionFactory.getConnection().setAutoCommit(false);
connection = DbConnectionFactory.getDataSource().getConnection();

if (DbConnectionFactory.isPostgres()){
dotConnect.executeStatement(POSTGRES_SCRIPT, connection);
}else if (DbConnectionFactory.isMySql()){
dotConnect.executeStatement(MYSQL_SCRIPT, connection);
}else if (DbConnectionFactory.isOracle()){
dotConnect.executeStatement(ORACLE_SCRIPT, connection);
}else{
dotConnect.executeStatement(MSSQL_SCRIPT, connection);
}

if (null != additionalInfoMaps) {

try {
//migrates info from proxy_user table to user_
for (final Map<String, Object> additionalInfo : additionalInfoMaps) {
dotConnect = new DotConnect();
Expand All @@ -49,54 +85,15 @@ private void migrateDataFromUserProxyToUser() throws DotDataException {
DbConnectionFactory.isPostgres() ? "? ::jsonb" : "?")
+ " where userid = ? ")
.addParam(mapper.writeValueAsString(additionalInfo)).addParam(userId)
.loadResult();
.loadResult(connection);
}

} catch (JsonProcessingException e) {
throw new DotRuntimeException(e);
}
}
}

private void createAdditionalInfoColumn() {
DotConnect dotConnect = new DotConnect();
final String sql;

if (DbConnectionFactory.isPostgres()){
sql = getPostgresScript();
}else if (DbConnectionFactory.isMySql()){
sql = getMySQLScript();
}else if (DbConnectionFactory.isOracle()){
sql = getOracleScript();
}else{
sql = getMSSQLScript();
}

try {
dotConnect.executeStatement(sql);
} catch (SQLException exception) {
throw new DotRuntimeException(exception);
} catch (JsonProcessingException | SQLException exception) {
throw new DotDataException(exception);
}

Logger.info(this, "additional_info column created");
}

private String getPostgresScript() {
return "alter table user_ add additional_info JSONB NULL;";

}

private String getMySQLScript() {
return "alter table user_ add additional_info text NULL;";

}

private String getOracleScript() {
return "alter table user_ add additional_info NCLOB NULL;";
}

private String getMSSQLScript() {
return "alter table user_ add additional_info NVARCHAR(MAX) NULL;";
Logger.info(this, "additional_info column created and user_proxy data migrated");
}

@Override
Expand Down

0 comments on commit e190ace

Please sign in to comment.