From 7eeffe2f958dc1789bcffe4ccd742819092ab857 Mon Sep 17 00:00:00 2001 From: Jun Nemoto Date: Fri, 17 Nov 2023 11:58:36 +0900 Subject: [PATCH] Fix to prevent importing existing storage table --- .../consensuscommit/ConsensusCommitAdmin.java | 2 +- .../ConsensusCommitAdminTestBase.java | 24 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdmin.java b/core/src/main/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdmin.java index 31b3d67b81..3f783d3284 100644 --- a/core/src/main/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdmin.java +++ b/core/src/main/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdmin.java @@ -209,7 +209,7 @@ public Set getNamespaceNames() throws ExecutionException { @Override public void importTable(String namespace, String table, Map options) throws ExecutionException { - TableMetadata tableMetadata = getTableMetadata(namespace, table); + TableMetadata tableMetadata = admin.getTableMetadata(namespace, table); if (tableMetadata != null) { throw new IllegalArgumentException( "Table already exists: " + ScalarDbUtils.getFullTableName(namespace, table)); diff --git a/core/src/test/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdminTestBase.java b/core/src/test/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdminTestBase.java index ee0fcdae69..fd937d8e83 100644 --- a/core/src/test/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdminTestBase.java +++ b/core/src/test/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdminTestBase.java @@ -633,7 +633,29 @@ public void importTable_ShouldCallStorageAdminProperly() throws ExecutionExcepti } @Test - public void importTable_WithTableAlreadyExists_ShouldThrowIllegalArgumentException() + public void importTable_WithStorageTableAlreadyExists_ShouldThrowIllegalArgumentException() + throws ExecutionException { + // Arrange + String primaryKeyColumn = "pk"; + String column = "col"; + TableMetadata metadata = + TableMetadata.newBuilder() + .addColumn(primaryKeyColumn, DataType.INT) + .addColumn(column, DataType.INT) + .addPartitionKey(primaryKeyColumn) + .build(); + when(distributedStorageAdmin.getTableMetadata(NAMESPACE, TABLE)).thenReturn(metadata); + + // Act + Throwable thrown = + catchThrowable(() -> admin.importTable(NAMESPACE, TABLE, Collections.emptyMap())); + + // Assert + assertThat(thrown).isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void importTable_WithTransactionTableAlreadyExists_ShouldThrowIllegalArgumentException() throws ExecutionException { // Arrange String primaryKeyColumn = "pk";