Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/src/main/sphinx/connector/mongodb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,5 +389,6 @@ statements, the connector supports the following features:
ALTER TABLE
^^^^^^^^^^^

The connector does not support ``ALTER TABLE RENAME`` operations. Other uses of
``ALTER TABLE`` are supported.
The connector supports ``ALTER TABLE RENAME TO``, ``ALTER TABLE ADD COLUMN``
and ``ALTER TABLE DROP COLUMN`` operations.
Other uses of ``ALTER TABLE`` are not supported.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ public void setColumnComment(ConnectorSession session, ConnectorTableHandle tabl
mongoSession.setColumnComment(table.getSchemaTableName(), column.getName(), comment);
}

@Override
public void renameTable(ConnectorSession session, ConnectorTableHandle tableHandle, SchemaTableName newTableName)
{
MongoTableHandle table = (MongoTableHandle) tableHandle;
mongoSession.renameTable(table.getSchemaTableName(), newTableName);
}

@Override
public void addColumn(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnMetadata column)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.primitives.SignedBytes;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.mongodb.DBRef;
import com.mongodb.MongoNamespace;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
Expand Down Expand Up @@ -253,6 +254,29 @@ public void setColumnComment(SchemaTableName schemaTableName, String columnName,
tableCache.invalidate(schemaTableName);
}

public void renameTable(SchemaTableName oldName, SchemaTableName newName)
{
String oldSchemaName = toRemoteSchemaName(oldName.getSchemaName());
Comment thread
ebyhr marked this conversation as resolved.
Outdated
String oldTableName = toRemoteTableName(oldSchemaName, oldName.getTableName());
String newSchemaName = toRemoteSchemaName(newName.getSchemaName());

// Schema collection should always have the source table definition
MongoCollection<Document> oldSchema = client.getDatabase(oldSchemaName).getCollection(schemaCollection);
Comment thread
ebyhr marked this conversation as resolved.
Outdated
Document tableDefinition = oldSchema.findOneAndDelete(new Document(TABLE_NAME_KEY, oldTableName));
requireNonNull(tableDefinition, "Table definition not found in schema collection: " + oldTableName);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at some point we should consider introducing a mechanism to apply compensation actions in case of partial failure of methods like these so that the operations are "transactional".


MongoCollection<Document> newSchema = client.getDatabase(newSchemaName).getCollection(schemaCollection);
tableDefinition.append(TABLE_NAME_KEY, newName.getTableName());
newSchema.insertOne(tableDefinition);

// Need to check explicitly because the old collection may not exist when it doesn't have any data
if (collectionExists(client.getDatabase(oldSchemaName), oldTableName)) {
getCollection(oldName).renameCollection(new MongoNamespace(newSchemaName, newName.getTableName()));
}

tableCache.invalidate(oldName);
}

public void addColumn(SchemaTableName schemaTableName, ColumnMetadata columnMetadata)
{
String schemaName = toRemoteSchemaName(schemaTableName.getSchemaName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_RENAME_SCHEMA:
return false;

case SUPPORTS_RENAME_TABLE:
return false;

case SUPPORTS_NOT_NULL_CONSTRAINT:
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
switch (connectorBehavior) {
case SUPPORTS_RENAME_SCHEMA:
case SUPPORTS_NOT_NULL_CONSTRAINT:
case SUPPORTS_RENAME_TABLE:
case SUPPORTS_RENAME_COLUMN:
return false;
default:
Expand Down Expand Up @@ -470,6 +469,21 @@ public void testCaseInsensitive()
assertUpdate("DROP TABLE testcase.testinsensitive");
}

@Test
public void testCaseInsensitiveRenameTable()
{
MongoCollection<Document> collection = client.getDatabase("testCase_RenameTable").getCollection("testInsensitive_RenameTable");
collection.insertOne(new Document(ImmutableMap.of("value", 1)));
assertQuery("SHOW TABLES IN testcase_renametable", "SELECT 'testinsensitive_renametable'");
assertQuery("SELECT value FROM testcase_renametable.testinsensitive_renametable", "SELECT 1");

assertUpdate("ALTER TABLE testcase_renametable.testinsensitive_renametable RENAME TO testcase_renametable.testinsensitive_renamed_table");

assertQuery("SHOW TABLES IN testcase_renametable", "SELECT 'testinsensitive_renamed_table'");
assertQuery("SELECT value FROM testcase_renametable.testinsensitive_renamed_table", "SELECT 1");
assertUpdate("DROP TABLE testcase_renametable.testinsensitive_renamed_table");
}

@Test
public void testNonLowercaseViewName()
{
Expand Down