Skip to content

Commit

Permalink
Fix disappearance issue of renamed schema in File metastore
Browse files Browse the repository at this point in the history
Additionally, verify renamed schema existence in BaseConnectorTest.testRenameSchema.
  • Loading branch information
ebyhr committed Aug 3, 2022
1 parent 09ae0b9 commit 362f691
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,12 @@ public synchronized void renameDatabase(String databaseName, String newDatabaseN
getRequiredDatabase(databaseName);
verifyDatabaseNotExists(newDatabaseName);

Path oldDatabaseMetadataDirectory = getDatabaseMetadataDirectory(databaseName);
Path newDatabaseMetadataDirectory = getDatabaseMetadataDirectory(newDatabaseName);
try {
if (!metadataFileSystem.rename(getDatabaseMetadataDirectory(databaseName), getDatabaseMetadataDirectory(newDatabaseName))) {
renameSchemaFile(DATABASE, oldDatabaseMetadataDirectory, newDatabaseMetadataDirectory);

if (!metadataFileSystem.rename(oldDatabaseMetadataDirectory, newDatabaseMetadataDirectory)) {
throw new TrinoException(HIVE_METASTORE_ERROR, "Could not rename database metadata directory");
}
}
Expand Down Expand Up @@ -1331,6 +1335,18 @@ private <T> void writeFile(String type, Path path, JsonCodec<T> codec, T value,
}
}

private void renameSchemaFile(SchemaType type, Path oldMetadataDirectory, Path newMetadataDirectory)
{
try {
if (!metadataFileSystem.rename(getSchemaPath(type, oldMetadataDirectory), getSchemaPath(type, newMetadataDirectory))) {
throw new TrinoException(HIVE_METASTORE_ERROR, "Could not rename " + type + " schema");
}
}
catch (IOException e) {
throw new TrinoException(HIVE_METASTORE_ERROR, "Could not rename " + type + " schema", e);
}
}

private void deleteSchemaFile(SchemaType type, Path metadataDirectory)
{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,7 @@ public void testRenameSchema()
try {
assertUpdate("CREATE SCHEMA " + schemaName);
assertUpdate("ALTER SCHEMA " + schemaName + " RENAME TO " + schemaName + "_renamed");
assertThat(computeActual("SHOW SCHEMAS").getOnlyColumnAsSet()).contains(schemaName + "_renamed");
}
finally {
assertUpdate("DROP SCHEMA IF EXISTS " + schemaName);
Expand Down

0 comments on commit 362f691

Please sign in to comment.