Skip to content

Commit 6441ab0

Browse files
committed
Fix not to drop metadata table instead of recreating it
1 parent 149d5c2 commit 6441ab0

File tree

2 files changed

+8
-75
lines changed

2 files changed

+8
-75
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ private String computeBooleanValue(boolean value) {
409409
public void dropTable(String namespace, String table) throws ExecutionException {
410410
try (Connection connection = dataSource.getConnection()) {
411411
dropTableInternal(connection, namespace, table);
412-
deleteTableMetadata(connection, namespace, table);
412+
deleteTableMetadata(connection, namespace, table, true);
413413
deleteNamespacesTableAndMetadataSchemaIfEmpty(connection);
414414
} catch (SQLException e) {
415415
throw new ExecutionException(
@@ -423,11 +423,14 @@ private void dropTableInternal(Connection connection, String schema, String tabl
423423
execute(connection, dropTableStatement);
424424
}
425425

426-
private void deleteTableMetadata(Connection connection, String namespace, String table)
426+
private void deleteTableMetadata(
427+
Connection connection, String namespace, String table, boolean deleteMetadataTableIfEmpty)
427428
throws SQLException {
428429
try {
429430
execute(connection, getDeleteTableMetadataStatement(namespace, table));
430-
deleteMetadataTableIfEmpty(connection);
431+
if (deleteMetadataTableIfEmpty) {
432+
deleteMetadataTableIfEmpty(connection);
433+
}
431434
} catch (SQLException e) {
432435
if (e.getMessage().contains("Unknown table") || e.getMessage().contains("does not exist")) {
433436
return;
@@ -1015,14 +1018,14 @@ public void renameTable(String namespace, String oldTableName, String newTableNa
10151018
String renameTableStatement = rdbEngine.renameTableSql(namespace, oldTableName, newTableName);
10161019
try (Connection connection = dataSource.getConnection()) {
10171020
execute(connection, renameTableStatement);
1018-
deleteTableMetadata(connection, namespace, oldTableName);
1021+
deleteTableMetadata(connection, namespace, oldTableName, false);
10191022
for (String indexedColumnName : tableMetadata.getSecondaryIndexNames()) {
10201023
String oldIndexName = getIndexName(namespace, oldTableName, indexedColumnName);
10211024
String newIndexName = getIndexName(namespace, newTableName, indexedColumnName);
10221025
renameIndexInternal(
10231026
connection, namespace, newTableName, indexedColumnName, oldIndexName, newIndexName);
10241027
}
1025-
addTableMetadata(connection, namespace, newTableName, tableMetadata, true, false);
1028+
addTableMetadata(connection, namespace, newTableName, tableMetadata, false, false);
10261029
}
10271030
} catch (SQLException e) {
10281031
throw new ExecutionException(

core/src/test/java/com/scalar/db/storage/jdbc/JdbcAdminTest.java

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,19 +3425,6 @@ public void renameTable_ForMysql_ShouldWorkProperly() throws SQLException, Execu
34253425
+ "`.`metadata` WHERE `full_table_name`=? ORDER BY `ordinal_position` ASC",
34263426
"ALTER TABLE `ns`.`table` RENAME TO `ns`.`table_new`",
34273427
"DELETE FROM `" + METADATA_SCHEMA + "`.`metadata` WHERE `full_table_name` = 'ns.table'",
3428-
"SELECT DISTINCT `full_table_name` FROM `" + METADATA_SCHEMA + "`.`metadata`",
3429-
"CREATE SCHEMA IF NOT EXISTS `" + METADATA_SCHEMA + "`",
3430-
"CREATE TABLE IF NOT EXISTS `"
3431-
+ METADATA_SCHEMA
3432-
+ "`.`metadata`("
3433-
+ "`full_table_name` VARCHAR(128),"
3434-
+ "`column_name` VARCHAR(128),"
3435-
+ "`data_type` VARCHAR(20) NOT NULL,"
3436-
+ "`key_type` VARCHAR(20),"
3437-
+ "`clustering_order` VARCHAR(10),"
3438-
+ "`indexed` BOOLEAN NOT NULL,"
3439-
+ "`ordinal_position` INTEGER NOT NULL,"
3440-
+ "PRIMARY KEY (`full_table_name`, `column_name`))",
34413428
"INSERT INTO `"
34423429
+ METADATA_SCHEMA
34433430
+ "`.`metadata` VALUES ('ns.table_new','c1','TEXT','PARTITION',NULL,false,1)",
@@ -3457,12 +3444,6 @@ public void renameTable_ForOracle_ShouldWorkProperly() throws SQLException, Exec
34573444
"DELETE FROM \""
34583445
+ METADATA_SCHEMA
34593446
+ "\".\"metadata\" WHERE \"full_table_name\" = 'ns.table'",
3460-
"SELECT DISTINCT \"full_table_name\" FROM \"" + METADATA_SCHEMA + "\".\"metadata\"",
3461-
"CREATE USER \"" + METADATA_SCHEMA + "\" IDENTIFIED BY \"Oracle1234!@#$\"",
3462-
"ALTER USER \"" + METADATA_SCHEMA + "\" quota unlimited on USERS",
3463-
"CREATE TABLE \""
3464-
+ METADATA_SCHEMA
3465-
+ "\".\"metadata\"(\"full_table_name\" VARCHAR2(128),\"column_name\" VARCHAR2(128),\"data_type\" VARCHAR2(20) NOT NULL,\"key_type\" VARCHAR2(20),\"clustering_order\" VARCHAR2(10),\"indexed\" NUMBER(1) NOT NULL,\"ordinal_position\" INTEGER NOT NULL,PRIMARY KEY (\"full_table_name\", \"column_name\"))",
34663447
"INSERT INTO \""
34673448
+ METADATA_SCHEMA
34683449
+ "\".\"metadata\" VALUES ('ns.table_new','c1','TEXT','PARTITION',NULL,0,1)",
@@ -3483,19 +3464,6 @@ public void renameTable_ForPostgresql_ShouldWorkProperly()
34833464
"DELETE FROM \""
34843465
+ METADATA_SCHEMA
34853466
+ "\".\"metadata\" WHERE \"full_table_name\" = 'ns.table'",
3486-
"SELECT DISTINCT \"full_table_name\" FROM \"" + METADATA_SCHEMA + "\".\"metadata\"",
3487-
"CREATE SCHEMA IF NOT EXISTS \"" + METADATA_SCHEMA + "\"",
3488-
"CREATE TABLE IF NOT EXISTS \""
3489-
+ METADATA_SCHEMA
3490-
+ "\".\"metadata\"("
3491-
+ "\"full_table_name\" VARCHAR(128),"
3492-
+ "\"column_name\" VARCHAR(128),"
3493-
+ "\"data_type\" VARCHAR(20) NOT NULL,"
3494-
+ "\"key_type\" VARCHAR(20),"
3495-
+ "\"clustering_order\" VARCHAR(10),"
3496-
+ "\"indexed\" BOOLEAN NOT NULL,"
3497-
+ "\"ordinal_position\" INTEGER NOT NULL,"
3498-
+ "PRIMARY KEY (\"full_table_name\", \"column_name\"))",
34993467
"INSERT INTO \""
35003468
+ METADATA_SCHEMA
35013469
+ "\".\"metadata\" VALUES ('ns.table_new','c1','TEXT','PARTITION',NULL,false,1)",
@@ -3514,19 +3482,6 @@ public void renameTable_ForSqlServer_ShouldWorkProperly()
35143482
+ "].[metadata] WHERE [full_table_name]=? ORDER BY [ordinal_position] ASC",
35153483
"EXEC sp_rename '[ns].[table]', 'table_new'",
35163484
"DELETE FROM [" + METADATA_SCHEMA + "].[metadata] WHERE [full_table_name] = 'ns.table'",
3517-
"SELECT DISTINCT [full_table_name] FROM [" + METADATA_SCHEMA + "].[metadata]",
3518-
"CREATE SCHEMA [" + METADATA_SCHEMA + "]",
3519-
"CREATE TABLE ["
3520-
+ METADATA_SCHEMA
3521-
+ "].[metadata]("
3522-
+ "[full_table_name] VARCHAR(128),"
3523-
+ "[column_name] VARCHAR(128),"
3524-
+ "[data_type] VARCHAR(20) NOT NULL,"
3525-
+ "[key_type] VARCHAR(20),"
3526-
+ "[clustering_order] VARCHAR(10),"
3527-
+ "[indexed] BIT NOT NULL,"
3528-
+ "[ordinal_position] INTEGER NOT NULL,"
3529-
+ "PRIMARY KEY ([full_table_name], [column_name]))",
35303485
"INSERT INTO ["
35313486
+ METADATA_SCHEMA
35323487
+ "].[metadata] VALUES ('ns.table_new','c1','TEXT','PARTITION',NULL,0,1)",
@@ -3544,18 +3499,6 @@ public void renameTable_ForSqlite_ShouldWorkProperly() throws SQLException, Exec
35443499
+ "$metadata\" WHERE \"full_table_name\"=? ORDER BY \"ordinal_position\" ASC",
35453500
"ALTER TABLE \"ns$table\" RENAME TO \"ns$table_new\"",
35463501
"DELETE FROM \"" + METADATA_SCHEMA + "$metadata\" WHERE \"full_table_name\" = 'ns.table'",
3547-
"SELECT DISTINCT \"full_table_name\" FROM \"" + METADATA_SCHEMA + "$metadata\"",
3548-
"CREATE TABLE IF NOT EXISTS \""
3549-
+ METADATA_SCHEMA
3550-
+ "$metadata\"("
3551-
+ "\"full_table_name\" TEXT,"
3552-
+ "\"column_name\" TEXT,"
3553-
+ "\"data_type\" TEXT NOT NULL,"
3554-
+ "\"key_type\" TEXT,"
3555-
+ "\"clustering_order\" TEXT,"
3556-
+ "\"indexed\" BOOLEAN NOT NULL,"
3557-
+ "\"ordinal_position\" INTEGER NOT NULL,"
3558-
+ "PRIMARY KEY (\"full_table_name\", \"column_name\"))",
35593502
"INSERT INTO \""
35603503
+ METADATA_SCHEMA
35613504
+ "$metadata\" VALUES ('ns.table_new','c1','TEXT','PARTITION',NULL,FALSE,1)",
@@ -3575,19 +3518,6 @@ public void renameTable_ForDb2_ShouldWorkProperly() throws SQLException, Executi
35753518
"DELETE FROM \""
35763519
+ METADATA_SCHEMA
35773520
+ "\".\"metadata\" WHERE \"full_table_name\" = 'ns.table'",
3578-
"SELECT DISTINCT \"full_table_name\" FROM \"" + METADATA_SCHEMA + "\".\"metadata\"",
3579-
"CREATE SCHEMA \"" + METADATA_SCHEMA + "\"",
3580-
"CREATE TABLE IF NOT EXISTS \""
3581-
+ METADATA_SCHEMA
3582-
+ "\".\"metadata\"("
3583-
+ "\"full_table_name\" VARCHAR(128) NOT NULL,"
3584-
+ "\"column_name\" VARCHAR(128) NOT NULL,"
3585-
+ "\"data_type\" VARCHAR(20) NOT NULL,"
3586-
+ "\"key_type\" VARCHAR(20),"
3587-
+ "\"clustering_order\" VARCHAR(10),"
3588-
+ "\"indexed\" BOOLEAN NOT NULL,"
3589-
+ "\"ordinal_position\" INTEGER NOT NULL,"
3590-
+ "PRIMARY KEY (\"full_table_name\", \"column_name\"))",
35913521
"INSERT INTO \""
35923522
+ METADATA_SCHEMA
35933523
+ "\".\"metadata\" VALUES ('ns.table_new','c1','TEXT','PARTITION',NULL,false,1)",

0 commit comments

Comments
 (0)