feat: add GlobalSecondaryIndexUpdates support in DynamoDB UpdateTable (#222)

Support Create and Delete actions in GlobalSecondaryIndexUpdates
parameter for the DynamoDB UpdateTable operation. The Update action
(throughput modification) is accepted and ignored since the emulator
does not enforce throughput limits.

Also handles AttributeDefinitions updates required when adding new
GSI key attributes.

Closes #221
This commit is contained in:
yusuke-uranaka authored and GitHub committed 2026-04-06 00:08:14 -05:00
1 parent 10edd2bc38
commit 4e6e9533b5
3 files changed
+256 -1

No files matched your search

@@ -396,7 +396,41 @@ public class DynamoDbJsonHandler {
writeCapacity = pt.has("WriteCapacityUnits") ? pt.get("WriteCapacityUnits").asLong() : null;
}
TableDefinition table = dynamoDbService.updateTable(tableName, readCapacity, writeCapacity, region);
List<GlobalSecondaryIndex> gsiCreates = new ArrayList<>();
List<String> gsiDeletes = new ArrayList<>();
JsonNode gsiUpdates = request.path("GlobalSecondaryIndexUpdates");
if (!gsiUpdates.isMissingNode() && gsiUpdates.isArray()) {
for (JsonNode update : gsiUpdates) {
JsonNode createNode = update.path("Create");
if (!createNode.isMissingNode()) {
String indexName = createNode.path("IndexName").asText();
List<KeySchemaElement> gsiKeySchema = new ArrayList<>();
createNode.path("KeySchema").forEach(ks ->
gsiKeySchema.add(new KeySchemaElement(
ks.path("AttributeName").asText(),
ks.path("KeyType").asText())));
String projectionType = createNode.path("Projection").path("ProjectionType").asText("ALL");
gsiCreates.add(new GlobalSecondaryIndex(indexName, gsiKeySchema, null, projectionType));
}
JsonNode deleteNode = update.path("Delete");
if (!deleteNode.isMissingNode()) {
gsiDeletes.add(deleteNode.path("IndexName").asText());
}
}
}
List<AttributeDefinition> newAttrDefs = new ArrayList<>();
JsonNode attrDefsNode = request.path("AttributeDefinitions");
if (!attrDefsNode.isMissingNode() && attrDefsNode.isArray()) {
for (JsonNode ad : attrDefsNode) {
newAttrDefs.add(new AttributeDefinition(
ad.path("AttributeName").asText(),
ad.path("AttributeType").asText()));
}
}
TableDefinition table = dynamoDbService.updateTable(tableName, readCapacity, writeCapacity,
gsiCreates, gsiDeletes, newAttrDefs, region);
String billingMode = request.has("BillingMode")
? request.get("BillingMode").asText() : null;
@@ -726,6 +726,12 @@ public class DynamoDbService {
// --- UpdateTable ---
public TableDefinition updateTable(String tableName, Long readCapacity, Long writeCapacity, String region) {
return updateTable(tableName, readCapacity, writeCapacity, List.of(), List.of(), List.of(), region);
}
public TableDefinition updateTable(String tableName, Long readCapacity, Long writeCapacity,
List<GlobalSecondaryIndex> gsiCreates, List<String> gsiDeletes,
List<AttributeDefinition> newAttrDefs, String region) {
String storageKey = regionKey(region, tableName);
TableDefinition table = tableStore.get(storageKey)
.orElseThrow(() -> resourceNotFoundException(tableName));
@@ -736,6 +742,27 @@ public class DynamoDbService {
if (writeCapacity != null) {
table.getProvisionedThroughput().setWriteCapacityUnits(writeCapacity);
}
for (String indexName : gsiDeletes) {
table.getGlobalSecondaryIndexes().removeIf(g -> indexName.equals(g.getIndexName()));
}
for (GlobalSecondaryIndex gsi : gsiCreates) {
gsi.setIndexArn(table.getTableArn() + "/index/" + gsi.getIndexName());
table.getGlobalSecondaryIndexes().add(gsi);
}
if (newAttrDefs != null && !newAttrDefs.isEmpty()) {
List<AttributeDefinition> existing = table.getAttributeDefinitions();
for (AttributeDefinition newDef : newAttrDefs) {
boolean found = existing.stream()
.anyMatch(e -> e.getAttributeName().equals(newDef.getAttributeName()));
if (!found) {
existing.add(newDef);
}
}
}
tableStore.put(storageKey, table);
LOG.infov("Updated table: {0} in region {1}", tableName, region);
return table;