Skip to content

Commit

Permalink
0005436: Sybase ASE issues when upgrading SymmetricDS from an older
Browse files Browse the repository at this point in the history
version to 3.14.*
  • Loading branch information
Philip Marzullo committed Sep 12, 2022
1 parent 4a8741c commit 5fcc801
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ public boolean createOrAlterTablesIfNecessary(String... tableNames) {
script.setListener(resultsListener);
script.execute(platform.getDatabaseInfo().isRequiresAutoCommitForDdl());
}
// The beforeUpgrade() methods may have made changes to the current database, should read again
modelFromDatabase = readSymmetricSchemaFromDatabase();
String alterSql = builder.alterDatabase(modelFromDatabase, modelFromXml, interceptors);
if (isNotBlank(alterSql)) {
log.info("There are SymmetricDS tables that needed altered");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ public String beforeUpgrade(ISymmetricDialect symmetricDialect, String tablePref
engine.getSqlTemplate().update("delete from " + tablePrefix + "_" + TableConstants.SYM_NODE_COMMUNICATION);
}
}
if (isUpgradeFromPre310(tablePrefix, currentModel, desiredModel)) {
String name = engine.getDatabasePlatform().getName();
if (name.equals(DatabaseNamesConstants.ASE)) {
log.info("Before upgrade, dropping foreign key constraints to node table");
try {
engine.getSqlTemplate().update("alter table " + tablePrefix + "_" + TableConstants.SYM_NODE_IDENTITY
+ " drop constraint " + tablePrefix + "_fk_ident_2_node");
} catch (Exception e) {
log.info("Unable to drop FK constraint " + tablePrefix + "_fk_ident_2_node to node table", e);
}
try {
engine.getSqlTemplate().update("alter table " + tablePrefix + "_" + TableConstants.SYM_NODE_SECURITY
+ " drop constraint " + tablePrefix + "_fk_sec_2_node");
} catch (Exception e) {
log.info("Unable to drop FK constraint " + tablePrefix + "_fk_sec_2_node to node table", e);
}
}
}
if (isUpgradeFromPre311(tablePrefix, currentModel, desiredModel) && shouldFixDataEvent311(tablePrefix)) {
fixDataEvent311(tablePrefix);
}
Expand Down Expand Up @@ -114,16 +132,20 @@ public String beforeUpgrade(ISymmetricDialect symmetricDialect, String tablePref
engine.getSqlTemplate().update("drop index " + tablePrefix + "_" + TableConstants.SYM_DATA + "."
+ tablePrefix + "_idx_d_channel_id");
} catch (Exception e) {
log.info("Unable to drop FK constraints to router table: {}", e.getMessage());
log.info("Unable to drop index " + tablePrefix + "_idx_d_channel_id on data table: {}", e.getMessage());
}
log.info("Before upgrade, dropping FK constraints to router table");
try {
engine.getSqlTemplate().update("alter table " + tablePrefix + "_" + TableConstants.SYM_TRIGGER_ROUTER
+ " drop constraint " + tablePrefix + "_fk_tr_2_rtr");
} catch (Exception e) {
log.info("Unable to drop FK constraint to router table: {}", e.getMessage());
}
try {
engine.getSqlTemplate().update("alter table " + tablePrefix + "_" + TableConstants.SYM_FILE_TRIGGER_ROUTER
+ " drop constraint " + tablePrefix + "_fk_ftr_2_rtr");
} catch (Exception e) {
log.info("Unable to drop FK constraints to router table: {}", e.getMessage());
log.info("Unable to drop FK constraint to router table: {}", e.getMessage());
}
}
}
Expand Down Expand Up @@ -235,6 +257,18 @@ protected boolean isUpgradeFromPre38(String tablePrefix, Database currentModel,
return false;
}
}

protected boolean isUpgradeFromPre310(String tablePrefix, Database currentModel, Database desiredModel) {
String nodeTableName = tablePrefix + "_" + TableConstants.SYM_NODE;
Table nodeTable = currentModel.findTable(nodeTableName);
if (nodeTable != null) {
Column heartbeatTime = nodeTable.getColumnWithName("heartbeat_time");
if (heartbeatTime != null) {
return true;
}
}
return false;
}

protected boolean isUpgradeFromPre311(String tablePrefix, Database currentModel, Database desiredModel) {
Table eventTable = currentModel.findTable(tablePrefix + "_" + TableConstants.SYM_DATA_EVENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ protected void writeCastExpression(Column sourceColumn, Column targetColumn, Str
protected void processChange(Database currentModel, Database desiredModel, AddPrimaryKeyChange change,
StringBuilder ddl) {
writeExternalPrimaryKeysCreateStmt(change.getChangedTable(), change.getPrimaryKeyColumns(), ddl);
change.apply(currentModel, delimitedIdentifierModeOn);
change.apply(desiredModel, delimitedIdentifierModeOn);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,19 @@ protected void createTable(Table table, StringBuilder ddl, boolean temporary, bo
protected void writeColumn(Table table, Column column, StringBuilder ddl) {
printIdentifier(getColumnName(column), ddl);
ddl.append(" ");
ddl.append(getSqlType(column));
String sqlType = getSqlType(column);
ddl.append(sqlType);
writeColumnDefaultValueStmt(table, column, ddl);
if (column.isUnique() && databaseInfo.isUniqueEmbedded()) {
writeColumnUniqueStmt(ddl);
}
// Sybase does not like NULL/NOT NULL and IDENTITY together
if (column.isAutoIncrement()) {
ddl.append(" ");
writeColumnAutoIncrementStmt(table, column, ddl);
// getSqlType() is returning "numeric identity" sometimes
if (! sqlType.toLowerCase().contains(" identity")) {
ddl.append(" ");
writeColumnAutoIncrementStmt(table, column, ddl);
}
} else {
ddl.append(" ");
if (column.isRequired()) {
Expand Down Expand Up @@ -200,7 +204,7 @@ protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKe
@Override
public void writeExternalIndexDropStmt(Table table, IIndex index, StringBuilder ddl) {
ddl.append("DROP INDEX ");
ddl.append(getFullyQualifiedTableNameShorten(table));
printIdentifier(getTableName(table.getName()), ddl);
ddl.append(".");
printIdentifier(getIndexName(index), ddl);
printEndOfStatement(ddl);
Expand Down

0 comments on commit 5fcc801

Please sign in to comment.