From c8f7642f6e867f95214f0b1de37cf3623b9d94e8 Mon Sep 17 00:00:00 2001 From: Madhu Dollu Date: Thu, 18 May 2023 11:50:49 +0530 Subject: [PATCH 1/4] fix global index deletion of projection INCLUDE type --- .../lib/utils/dynamoose/index_changes.ts | 12 ++- .../test/utils/dynamoose/index_changes.js | 83 ++++++++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/packages/dynamoose/lib/utils/dynamoose/index_changes.ts b/packages/dynamoose/lib/utils/dynamoose/index_changes.ts index 419492d70..0e9b4b427 100644 --- a/packages/dynamoose/lib/utils/dynamoose/index_changes.ts +++ b/packages/dynamoose/lib/utils/dynamoose/index_changes.ts @@ -34,6 +34,13 @@ const index_changes = async (table: Table, existingIndexes = []): Promise<(Model identicalProperties.pop(); } + const sanitizeIndex = (index: IndexItem) => { + if (Array.isArray(index.Projection.NonKeyAttributes)) { + index.Projection.NonKeyAttributes.sort(); + } + return index; + } + const deleteIndexes: ModelIndexDeleteChange[] = existingIndexes.filter((index) => { const cleanedIndex = deep_copy(index); obj.entries(cleanedIndex).forEach(([key, value]) => { @@ -42,7 +49,10 @@ const index_changes = async (table: Table, existingIndexes = []): Promise<(Model } }); - return !(expectedIndexes.GlobalSecondaryIndexes || []).find((searchIndex) => obj.equals(obj.pick(cleanedIndex, identicalProperties), obj.pick(searchIndex as any, identicalProperties))); + return !(expectedIndexes.GlobalSecondaryIndexes || []).find((searchIndex) => obj.equals( + sanitizeIndex(obj.pick(cleanedIndex, identicalProperties) as unknown as IndexItem), + sanitizeIndex(obj.pick(searchIndex as any, identicalProperties) as unknown as IndexItem) + )); }).map((index) => ({"name": index.IndexName as string, "type": TableIndexChangeType.delete})); output.push(...deleteIndexes); diff --git a/packages/dynamoose/test/utils/dynamoose/index_changes.js b/packages/dynamoose/test/utils/dynamoose/index_changes.js index b23004b7c..8095c618d 100644 --- a/packages/dynamoose/test/utils/dynamoose/index_changes.js +++ b/packages/dynamoose/test/utils/dynamoose/index_changes.js @@ -215,7 +215,88 @@ describe("utils.dynamoose.index_changes", () => { ], "schema": {"id": String, "data": {"type": String, "index": {"name": "data-index-1", "type": "global", "project": true}}}, "output": [] - } + }, + { + "input": [], + "schema": {"id": String, "data1": {"type": String, "index": {"type": "global", "project": ["data2"]}}, "data2": String, "data3": String}, + "output": [{ + "spec": { + "IndexName": "data1GlobalIndex", + "KeySchema": [ + { + "AttributeName": "data1", + "KeyType": "HASH" + } + ], + "Projection": { + "NonKeyAttributes": [ "data2" ], + "ProjectionType": "INCLUDE", + }, + "ProvisionedThroughput": { + "ReadCapacityUnits": 1, + "WriteCapacityUnits": 1 + } + }, + "type": "add" + }] + }, + { + "input": [ + { + "IndexName": "data-index-1", + "KeySchema": [ + { + "AttributeName": "data1", + "KeyType": "HASH" + } + ], + "Projection": { + "NonKeyAttributes": [ "data2", "data3" ], // order not same as schema definition + "ProjectionType": "INCLUDE", + }, + "IndexStatus": "ACTIVE", + "ProvisionedThroughput": { + "ReadCapacityUnits": 1, + "WriteCapacityUnits": 1 + }, + "IndexSizeBytes": 0, + "ItemCount": 0, + "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/User/index/data-index-1" + } + ], + "schema": {"id": String, "data1": {"type": String, "index": {"name": "data-index-1", "type": "global", "project": ["data3", "data2"]}}, "data2": String, "data3": String}, + "output": [] + }, + { + "input": [ + { + "IndexName": "data-index-1", + "KeySchema": [ + { + "AttributeName": "data1", + "KeyType": "HASH" + } + ], + "Projection": { + "NonKeyAttributes": [ "data2" ], + "ProjectionType": "INCLUDE", + }, + "IndexStatus": "ACTIVE", + "ProvisionedThroughput": { + "ReadCapacityUnits": 1, + "WriteCapacityUnits": 1 + }, + "IndexSizeBytes": 0, + "ItemCount": 0, + "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/User/index/data-index-1" + } + ], + "schema": {"id": String, "data1": {"type": String, "index": {"name": "data-index-1", "type": "global", "project": ["data3", "data2"]}}, "data2": String, "data3": String}, + "output": [{ + "name": "data-index-1", + "type": "delete" + }] + }, ]; tests.forEach((test) => { From 96c0020831cfe20c6d0e7fefeae3f36ffd5c3599 Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Thu, 28 Dec 2023 09:01:38 -0700 Subject: [PATCH 2/4] Removing unnecessary code --- packages/dynamoose/lib/utils/dynamoose/index_changes.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/dynamoose/lib/utils/dynamoose/index_changes.ts b/packages/dynamoose/lib/utils/dynamoose/index_changes.ts index 0e9b4b427..81519840d 100644 --- a/packages/dynamoose/lib/utils/dynamoose/index_changes.ts +++ b/packages/dynamoose/lib/utils/dynamoose/index_changes.ts @@ -50,8 +50,8 @@ const index_changes = async (table: Table, existingIndexes = []): Promise<(Model }); return !(expectedIndexes.GlobalSecondaryIndexes || []).find((searchIndex) => obj.equals( - sanitizeIndex(obj.pick(cleanedIndex, identicalProperties) as unknown as IndexItem), - sanitizeIndex(obj.pick(searchIndex as any, identicalProperties) as unknown as IndexItem) + sanitizeIndex(obj.pick(cleanedIndex, identicalProperties) as IndexItem), + sanitizeIndex(obj.pick(searchIndex as any, identicalProperties) as IndexItem) )); }).map((index) => ({"name": index.IndexName as string, "type": TableIndexChangeType.delete})); output.push(...deleteIndexes); From 2c01fba0dec2f90ba86020859dd4b13781cac421 Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Thu, 28 Dec 2023 09:30:14 -0700 Subject: [PATCH 3/4] Fixing CI Signed-off-by: Charlie Fish --- packages/dynamoose/lib/utils/dynamoose/index_changes.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/dynamoose/lib/utils/dynamoose/index_changes.ts b/packages/dynamoose/lib/utils/dynamoose/index_changes.ts index 81519840d..b9ce4a3be 100644 --- a/packages/dynamoose/lib/utils/dynamoose/index_changes.ts +++ b/packages/dynamoose/lib/utils/dynamoose/index_changes.ts @@ -35,11 +35,11 @@ const index_changes = async (table: Table, existingIndexes = []): Promise<(Model } const sanitizeIndex = (index: IndexItem) => { - if (Array.isArray(index.Projection.NonKeyAttributes)) { + if (Array.isArray(index.Projection.NonKeyAttributes)) { index.Projection.NonKeyAttributes.sort(); } return index; - } + }; const deleteIndexes: ModelIndexDeleteChange[] = existingIndexes.filter((index) => { const cleanedIndex = deep_copy(index); @@ -50,8 +50,8 @@ const index_changes = async (table: Table, existingIndexes = []): Promise<(Model }); return !(expectedIndexes.GlobalSecondaryIndexes || []).find((searchIndex) => obj.equals( - sanitizeIndex(obj.pick(cleanedIndex, identicalProperties) as IndexItem), - sanitizeIndex(obj.pick(searchIndex as any, identicalProperties) as IndexItem) + sanitizeIndex(obj.pick(cleanedIndex, identicalProperties) as any), + sanitizeIndex(obj.pick(searchIndex as any, identicalProperties) as any) )); }).map((index) => ({"name": index.IndexName as string, "type": TableIndexChangeType.delete})); output.push(...deleteIndexes); From 73618c939c1118dc63ce69a27ebefa58dc21bd23 Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Thu, 28 Dec 2023 09:32:19 -0700 Subject: [PATCH 4/4] Fixing lint issues Signed-off-by: Charlie Fish --- .../test/utils/dynamoose/index_changes.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/dynamoose/test/utils/dynamoose/index_changes.js b/packages/dynamoose/test/utils/dynamoose/index_changes.js index 8095c618d..03ec24642 100644 --- a/packages/dynamoose/test/utils/dynamoose/index_changes.js +++ b/packages/dynamoose/test/utils/dynamoose/index_changes.js @@ -217,8 +217,8 @@ describe("utils.dynamoose.index_changes", () => { "output": [] }, { - "input": [], - "schema": {"id": String, "data1": {"type": String, "index": {"type": "global", "project": ["data2"]}}, "data2": String, "data3": String}, + "input": [], + "schema": {"id": String, "data1": {"type": String, "index": {"type": "global", "project": ["data2"]}}, "data2": String, "data3": String}, "output": [{ "spec": { "IndexName": "data1GlobalIndex", @@ -229,8 +229,8 @@ describe("utils.dynamoose.index_changes", () => { } ], "Projection": { - "NonKeyAttributes": [ "data2" ], - "ProjectionType": "INCLUDE", + "NonKeyAttributes": ["data2"], + "ProjectionType": "INCLUDE" }, "ProvisionedThroughput": { "ReadCapacityUnits": 1, @@ -251,8 +251,8 @@ describe("utils.dynamoose.index_changes", () => { } ], "Projection": { - "NonKeyAttributes": [ "data2", "data3" ], // order not same as schema definition - "ProjectionType": "INCLUDE", + "NonKeyAttributes": ["data2", "data3"], // order not same as schema definition + "ProjectionType": "INCLUDE" }, "IndexStatus": "ACTIVE", "ProvisionedThroughput": { @@ -278,8 +278,8 @@ describe("utils.dynamoose.index_changes", () => { } ], "Projection": { - "NonKeyAttributes": [ "data2" ], - "ProjectionType": "INCLUDE", + "NonKeyAttributes": ["data2"], + "ProjectionType": "INCLUDE" }, "IndexStatus": "ACTIVE", "ProvisionedThroughput": { @@ -296,7 +296,7 @@ describe("utils.dynamoose.index_changes", () => { "name": "data-index-1", "type": "delete" }] - }, + } ]; tests.forEach((test) => {