Skip to content

Commit

Permalink
Merge pull request #1554 from sromic/main
Browse files Browse the repository at this point in the history
FIX - fixed recreating indexes when table throughput is set ON_DEMAND
  • Loading branch information
fishcharlie committed Apr 15, 2023
2 parents 6b059f9 + 7d129be commit 1d5b595
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/dynamoose/lib/utils/dynamoose/index_changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ export interface ModelIndexDeleteChange {
const index_changes = async (table: Table, existingIndexes = []): Promise<(ModelIndexAddChange | ModelIndexDeleteChange)[]> => {
const output: (ModelIndexAddChange | ModelIndexDeleteChange)[] = [];
const expectedIndexes = await table.getInternalProperties(internalProperties).getIndexes();
const tableThroughput = table.getInternalProperties(internalProperties).options.throughput;

// Indexes to delete
const identicalProperties: string[] = ["IndexName", "KeySchema", "Projection", "ProvisionedThroughput"]; // This array represents the properties in the indexes that should match between existingIndexes (from DynamoDB) and expectedIndexes. This array will not include things like `IndexArn`, `ItemCount`, etc, since those properties do not exist in expectedIndexes

if (tableThroughput === "ON_DEMAND") {
// remove `ProvisionedThroughput` property from properties to compare against
// because `ProvisionedThroughput` is not set on index schema in case of `ON_DEMAND` throughput
// meaning `ProvisionedThroughput` is implicitly inherited from the table
identicalProperties.pop();
}

const deleteIndexes: ModelIndexDeleteChange[] = existingIndexes.filter((index) => {
const cleanedIndex = deep_copy(index);
obj.entries(cleanedIndex).forEach(([key, value]) => {
Expand Down
34 changes: 34 additions & 0 deletions packages/dynamoose/test/utils/dynamoose/index_changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,38 @@ describe("utils.dynamoose.index_changes", () => {
expect(await utils.dynamoose.index_changes(table, test.input)).toEqual(test.output);
});
});

it("Should not recreate indexes when Table throughput is set `ON_DEMAND`", async () => {
const test = {
"input": [
{
"IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/User/index/data-index-2",
"IndexName": "data-index-2",
"IndexSizeBytes": 0,
"IndexStatus": "ACTIVE",
"ItemCount": 0,
"KeySchema": [
{
"AttributeName": "data",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 0,
"WriteCapacityUnits": 0
}
}
],
"schema": {"id": String, "data": {"type": String, "index": {"name": "data-index-2", "type": "global", "project": true}}},
"output": []
};

const Model = dynamoose.model("Model", test.schema);
const table = new dynamoose.Table("Table", [Model], {"create": false, "waitForActive": false, "update": false, "throughput": "ON_DEMAND"});
expect(await utils.dynamoose.index_changes(table, test.input)).toEqual(test.output);
});
});

0 comments on commit 1d5b595

Please sign in to comment.