Skip to content

Commit bf367a6

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

17 files changed

+226
-57
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.scalar.db.storage.jdbc;
22

33
import com.scalar.db.config.DatabaseConfig;
4+
import com.scalar.db.exception.storage.ExecutionException;
45
import com.scalar.db.transaction.consensuscommit.ConsensusCommitAdminIntegrationTestBase;
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 ConsensusCommitAdminIntegrationTestWithJdbcDatabase
912
extends ConsensusCommitAdminIntegrationTestBase {
@@ -29,4 +32,25 @@ protected boolean isCreateIndexOnTextAndBlobColumnsEnabled() {
2932
// So we disable these tests until the issue with the Db2 community edition is resolved.
3033
return !JdbcTestUtils.isDb2(rdbEngine);
3134
}
35+
36+
@SuppressWarnings("unused")
37+
private boolean isDb2() {
38+
return JdbcEnv.isDb2();
39+
}
40+
41+
@Test
42+
@Override
43+
@DisabledIf("isDb2")
44+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly()
45+
throws ExecutionException {
46+
super.renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly();
47+
}
48+
49+
@Test
50+
@Override
51+
@DisabledIf("isDb2")
52+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly()
53+
throws ExecutionException {
54+
super.renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly();
55+
}
3256
}

core/src/integration-test/java/com/scalar/db/storage/jdbc/JdbcAdminCaseSensitivityIntegrationTest.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.DistributedStorageAdminCaseSensitivityIntegrationTestBase;
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 JdbcAdminCaseSensitivityIntegrationTest
912
extends DistributedStorageAdminCaseSensitivityIntegrationTestBase {
@@ -28,4 +31,25 @@ protected boolean isCreateIndexOnTextAndBlobColumnsEnabled() {
2831
// So we disable these tests until the issue is resolved.
2932
return !JdbcTestUtils.isDb2(rdbEngine);
3033
}
34+
35+
@SuppressWarnings("unused")
36+
private boolean isDb2() {
37+
return JdbcEnv.isDb2();
38+
}
39+
40+
@Test
41+
@Override
42+
@DisabledIf("isDb2")
43+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly()
44+
throws ExecutionException {
45+
super.renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly();
46+
}
47+
48+
@Test
49+
@Override
50+
@DisabledIf("isDb2")
51+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly()
52+
throws ExecutionException {
53+
super.renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly();
54+
}
3155
}

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/integration-test/java/com/scalar/db/storage/jdbc/SingleCrudOperationTransactionAdminIntegrationTestWithJdbcDatabase.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.scalar.db.storage.jdbc;
22

33
import com.scalar.db.config.DatabaseConfig;
4+
import com.scalar.db.exception.storage.ExecutionException;
45
import com.scalar.db.transaction.singlecrudoperation.SingleCrudOperationTransactionAdminIntegrationTestBase;
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 SingleCrudOperationTransactionAdminIntegrationTestWithJdbcDatabase
912
extends SingleCrudOperationTransactionAdminIntegrationTestBase {
@@ -29,4 +32,25 @@ protected boolean isCreateIndexOnTextAndBlobColumnsEnabled() {
2932
// So we disable these tests until the issue with the Db2 community edition is resolved.
3033
return !JdbcTestUtils.isDb2(rdbEngine);
3134
}
35+
36+
@SuppressWarnings("unused")
37+
private boolean isDb2() {
38+
return JdbcEnv.isDb2();
39+
}
40+
41+
@Test
42+
@Override
43+
@DisabledIf("isDb2")
44+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly()
45+
throws ExecutionException {
46+
super.renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly();
47+
}
48+
49+
@Test
50+
@Override
51+
@DisabledIf("isDb2")
52+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly()
53+
throws ExecutionException {
54+
super.renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly();
55+
}
3256
}

core/src/integration-test/java/com/scalar/db/transaction/jdbc/JdbcTransactionAdminIntegrationTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Properties;
1313
import org.junit.jupiter.api.Disabled;
1414
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.condition.DisabledIf;
1516

1617
public class JdbcTransactionAdminIntegrationTest
1718
extends DistributedTransactionAdminIntegrationTestBase {
@@ -102,4 +103,25 @@ protected boolean isCreateIndexOnTextAndBlobColumnsEnabled() {
102103
// So we disable these tests until the issue with the Db2 community edition is resolved.
103104
return !JdbcTestUtils.isDb2(rdbEngine);
104105
}
106+
107+
@SuppressWarnings("unused")
108+
private boolean isDb2() {
109+
return JdbcEnv.isDb2();
110+
}
111+
112+
@Test
113+
@Override
114+
@DisabledIf("isDb2")
115+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly()
116+
throws ExecutionException {
117+
super.renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly();
118+
}
119+
120+
@Test
121+
@Override
122+
@DisabledIf("isDb2")
123+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly()
124+
throws ExecutionException {
125+
super.renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly();
126+
}
105127
}

core/src/main/java/com/scalar/db/api/Admin.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,8 @@ default void dropColumnFromTable(
527527
* @param table the table name
528528
* @param oldColumnName the current name of the column to rename
529529
* @param newColumnName the new name of the column
530-
* @throws IllegalArgumentException if the table or the old column does not exist, the new column
531-
* already exists, or the column is a partition key column, clustering key column, or is
532-
* indexed
530+
* @throws IllegalArgumentException if the table or the old column does not exist or the new
531+
* column already exists
533532
* @throws ExecutionException if the operation fails
534533
*/
535534
void renameColumn(String namespace, String table, String oldColumnName, String newColumnName)

core/src/main/java/com/scalar/db/common/CoreError.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ public enum CoreError implements ScalarDbError {
709709
CASSANDRA_RENAME_NON_PRIMARY_KEY_COLUMN_NOT_SUPPORTED(
710710
Category.USER_ERROR,
711711
"0222",
712-
"Cassandra does not support renaming non-primary key columns",
712+
"Cassandra does not support renaming the non-primary key column feature",
713713
"",
714714
""),
715715

core/src/main/java/com/scalar/db/storage/cassandra/CassandraAdmin.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,13 @@ public void renameColumn(
441441
String namespace, String table, String oldColumnName, String newColumnName)
442442
throws ExecutionException {
443443
try {
444+
TableMetadata tableMetadata = getTableMetadata(namespace, table);
445+
assert tableMetadata != null;
446+
if (!tableMetadata.getPartitionKeyNames().contains(oldColumnName)
447+
|| !tableMetadata.getClusteringKeyNames().contains(oldColumnName)) {
448+
throw new IllegalArgumentException(
449+
CoreError.CASSANDRA_RENAME_NON_PRIMARY_KEY_COLUMN_NOT_SUPPORTED.buildMessage());
450+
}
444451
String alterTableQuery =
445452
SchemaBuilder.alterTable(quoteIfNecessary(namespace), quoteIfNecessary(table))
446453
.renameColumn(quoteIfNecessary(oldColumnName))

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,7 @@ public void renameColumn(
890890
throws ExecutionException {
891891
try {
892892
TableMetadata currentTableMetadata = getTableMetadata(namespace, table);
893+
assert currentTableMetadata != null;
893894
DataType columnType = currentTableMetadata.getColumnDataType(oldColumnName);
894895
TableMetadata.Builder tableMetadataBuilder =
895896
TableMetadata.newBuilder(currentTableMetadata)
@@ -917,12 +918,7 @@ public void renameColumn(
917918
try (Connection connection = dataSource.getConnection()) {
918919
execute(connection, renameColumnStatement);
919920
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-
}
921+
renameIndex(connection, namespace, table, oldColumnName, newColumnName);
926922
}
927923
addTableMetadata(connection, namespace, table, updatedTableMetadata, false, true);
928924
}
@@ -1001,8 +997,11 @@ private void renameIndex(
1001997
throws SQLException {
1002998
String oldIndexName = getIndexName(schema, table, oldIndexedColumn);
1003999
String newIndexName = getIndexName(schema, table, newIndexedColumn);
1004-
String sql = rdbEngine.renameIndexSql(schema, table, oldIndexName, newIndexName);
1005-
execute(connection, sql);
1000+
String[] sqls =
1001+
rdbEngine.renameIndexSqls(schema, table, oldIndexName, newIndexName, newIndexedColumn);
1002+
for (String sql : sqls) {
1003+
execute(connection, sql);
1004+
}
10061005
}
10071006

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

0 commit comments

Comments
 (0)