Skip to content

Commit 62b9b81

Browse files
committed
Fix to handle an issue where Db2 does not support renaming non-primary or index key columns
1 parent 67a8ed6 commit 62b9b81

File tree

10 files changed

+121
-53
lines changed

10 files changed

+121
-53
lines changed

core/src/integration-test/java/com/scalar/db/storage/jdbc/JdbcAdminIntegrationTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import com.scalar.db.api.DistributedStorageAdminIntegrationTestBase;
44
import com.scalar.db.config.DatabaseConfig;
5+
import com.scalar.db.exception.storage.ExecutionException;
56
import com.scalar.db.util.AdminTestUtils;
67
import java.util.Properties;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.condition.DisabledIf;
710

811
public class JdbcAdminIntegrationTest extends DistributedStorageAdminIntegrationTestBase {
912
private RdbEngineStrategy rdbEngine;
@@ -27,4 +30,25 @@ protected boolean isCreateIndexOnTextAndBlobColumnsEnabled() {
2730
// So we disable these tests until the issue is resolved.
2831
return !JdbcTestUtils.isDb2(rdbEngine);
2932
}
33+
34+
@SuppressWarnings("unused")
35+
private boolean isDb2() {
36+
return JdbcEnv.isDb2();
37+
}
38+
39+
@Test
40+
@Override
41+
@DisabledIf("isDb2")
42+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly()
43+
throws ExecutionException {
44+
super.renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly();
45+
}
46+
47+
@Test
48+
@Override
49+
@DisabledIf("isDb2")
50+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly()
51+
throws ExecutionException {
52+
super.renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly();
53+
}
3054
}

core/src/integration-test/java/com/scalar/db/storage/jdbc/JdbcEnv.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ public static Properties getPropertiesForNormalUser(String testName) {
5454
public static boolean isSqlite() {
5555
return System.getProperty(PROP_JDBC_URL, DEFAULT_JDBC_URL).startsWith("jdbc:sqlite:");
5656
}
57+
58+
public static boolean isDb2() {
59+
return System.getProperty(PROP_JDBC_URL, DEFAULT_JDBC_URL).startsWith("jdbc:db2:");
60+
}
5761
}

core/src/main/java/com/scalar/db/storage/jdbc/JdbcAdmin.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -917,12 +917,7 @@ public void renameColumn(
917917
try (Connection connection = dataSource.getConnection()) {
918918
execute(connection, renameColumnStatement);
919919
if (currentTableMetadata.getSecondaryIndexNames().contains(oldColumnName)) {
920-
if (rdbEngine instanceof RdbEngineSqlite) {
921-
dropIndex(namespace, table, oldColumnName);
922-
createIndex(connection, namespace, table, newColumnName, false);
923-
} else {
924-
renameIndex(connection, namespace, table, oldColumnName, newColumnName);
925-
}
920+
renameIndex(connection, namespace, table, oldColumnName, newColumnName);
926921
}
927922
addTableMetadata(connection, namespace, table, updatedTableMetadata, false, true);
928923
}
@@ -1001,8 +996,11 @@ private void renameIndex(
1001996
throws SQLException {
1002997
String oldIndexName = getIndexName(schema, table, oldIndexedColumn);
1003998
String newIndexName = getIndexName(schema, table, newIndexedColumn);
1004-
String sql = rdbEngine.renameIndexSql(schema, table, oldIndexName, newIndexName);
1005-
execute(connection, sql);
999+
String[] sqls =
1000+
rdbEngine.renameIndexSqls(schema, table, oldIndexName, newIndexName, newIndexedColumn);
1001+
for (String sql : sqls) {
1002+
execute(connection, sql);
1003+
}
10061004
}
10071005

10081006
private String getIndexName(String schema, String table, String indexedColumn) {

core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineDb2.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,20 @@ public String dropIndexSql(String schema, String table, String indexName) {
293293
}
294294

295295
@Override
296-
public String renameIndexSql(
297-
String schema, String table, String oldIndexName, String newIndexName) {
298-
return "RENAME INDEX "
299-
+ enclose(schema)
300-
+ "."
301-
+ enclose(oldIndexName)
302-
+ " TO "
303-
+ enclose(newIndexName);
296+
public String[] renameIndexSqls(
297+
String schema,
298+
String table,
299+
String oldIndexName,
300+
String newIndexName,
301+
String newIndexedColumn) {
302+
return new String[] {
303+
"RENAME INDEX "
304+
+ enclose(schema)
305+
+ "."
306+
+ enclose(oldIndexName)
307+
+ " TO "
308+
+ enclose(newIndexName)
309+
};
304310
}
305311

306312
@Override

core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineMysql.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,20 @@ public String dropIndexSql(String schema, String table, String indexName) {
153153
}
154154

155155
@Override
156-
public String renameIndexSql(
157-
String schema, String table, String oldIndexName, String newIndexName) {
158-
return "ALTER TABLE "
159-
+ encloseFullTableName(schema, table)
160-
+ " RENAME INDEX "
161-
+ enclose(oldIndexName)
162-
+ " TO "
163-
+ enclose(newIndexName);
156+
public String[] renameIndexSqls(
157+
String schema,
158+
String table,
159+
String oldIndexName,
160+
String newIndexName,
161+
String newIndexedColumn) {
162+
return new String[] {
163+
"ALTER TABLE "
164+
+ encloseFullTableName(schema, table)
165+
+ " RENAME INDEX "
166+
+ enclose(oldIndexName)
167+
+ " TO "
168+
+ enclose(newIndexName)
169+
};
164170
}
165171

166172
@Override

core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineOracle.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,20 @@ public String dropIndexSql(String schema, String table, String indexName) {
164164
}
165165

166166
@Override
167-
public String renameIndexSql(
168-
String schema, String table, String oldIndexName, String newIndexName) {
169-
return "ALTER INDEX "
170-
+ enclose(schema)
171-
+ "."
172-
+ enclose(oldIndexName)
173-
+ " RENAME TO "
174-
+ enclose(newIndexName);
167+
public String[] renameIndexSqls(
168+
String schema,
169+
String table,
170+
String oldIndexName,
171+
String newIndexName,
172+
String newIndexedColumn) {
173+
return new String[] {
174+
"ALTER INDEX "
175+
+ enclose(schema)
176+
+ "."
177+
+ enclose(oldIndexName)
178+
+ " RENAME TO "
179+
+ enclose(newIndexName)
180+
};
175181
}
176182

177183
@Override

core/src/main/java/com/scalar/db/storage/jdbc/RdbEnginePostgresql.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,20 @@ public String dropIndexSql(String schema, String table, String indexName) {
133133
}
134134

135135
@Override
136-
public String renameIndexSql(
137-
String schema, String table, String oldIndexName, String newIndexName) {
138-
return "ALTER INDEX "
139-
+ enclose(schema)
140-
+ "."
141-
+ enclose(oldIndexName)
142-
+ " RENAME TO "
143-
+ enclose(newIndexName);
136+
public String[] renameIndexSqls(
137+
String schema,
138+
String table,
139+
String oldIndexName,
140+
String newIndexName,
141+
String newIndexedColumn) {
142+
return new String[] {
143+
"ALTER INDEX "
144+
+ enclose(schema)
145+
+ "."
146+
+ enclose(oldIndexName)
147+
+ " RENAME TO "
148+
+ enclose(newIndexName)
149+
};
144150
}
145151

146152
@Override

core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineSqlServer.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,21 @@ public String dropIndexSql(String schema, String table, String indexName) {
135135
}
136136

137137
@Override
138-
public String renameIndexSql(
139-
String schema, String table, String oldIndexName, String newIndexName) {
140-
return "EXEC sp_rename '"
141-
+ encloseFullTableName(schema, table)
142-
+ "."
143-
+ enclose(oldIndexName)
144-
+ "', '"
145-
+ newIndexName
146-
+ "', 'INDEX'";
138+
public String[] renameIndexSqls(
139+
String schema,
140+
String table,
141+
String oldIndexName,
142+
String newIndexName,
143+
String newIndexedColumn) {
144+
return new String[] {
145+
"EXEC sp_rename '"
146+
+ encloseFullTableName(schema, table)
147+
+ "."
148+
+ enclose(oldIndexName)
149+
+ "', '"
150+
+ newIndexName
151+
+ "', 'INDEX'"
152+
};
147153
}
148154

149155
@Override

core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineSqlite.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,17 @@ public String dropIndexSql(String schema, String table, String indexName) {
267267
}
268268

269269
@Override
270-
public String renameIndexSql(
271-
String schema, String table, String oldIndexName, String newIndexName) {
270+
public String[] renameIndexSqls(
271+
String schema,
272+
String table,
273+
String oldIndexName,
274+
String newIndexName,
275+
String newIndexedColumn) {
272276
// SQLite does not support renaming an index
273-
return null;
277+
return new String[] {
278+
dropIndexSql(schema, table, oldIndexName),
279+
createIndexSql(schema, table, newIndexName, newIndexedColumn)
280+
};
274281
}
275282

276283
@Override

core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineStrategy.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ default String createIndexSql(
138138

139139
String dropIndexSql(String schema, String table, String indexName);
140140

141-
String renameIndexSql(String schema, String table, String oldIndexName, String newIndexName);
141+
String[] renameIndexSqls(
142+
String schema,
143+
String table,
144+
String oldIndexName,
145+
String newIndexName,
146+
String newIndexedColumn);
142147

143148
/**
144149
* Enclose the target (schema, table or column) to use reserved words and special characters.

0 commit comments

Comments
 (0)