From 1d7ffdfcea3c8840fca9693427a1e4ef5eceec5d Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 31 Dec 2020 12:05:13 -0800 Subject: [PATCH] Rename primary-key attribute from PK to hunt_id I was just about to add a separate attribut to every item in the database named hunt_id when I realized that that was totally redundant with our existing PK attribute (whose values were simply the hunt_id prefixed by "hunt-" in all cases). Since we're not using the PK field in any generalized sense, (that is, it's _always_ populated with the hunt_id value), it's much simpler and more efficient to just name it "hunt_id" and store the hunt_id valeu ther directly (without any prefix). --- turbot/events.py | 2 +- turbot/interaction.py | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/turbot/events.py b/turbot/events.py index 3b04036..267d939 100644 --- a/turbot/events.py +++ b/turbot/events.py @@ -60,7 +60,7 @@ def hunt_block(turb, hunt): response = turb.table.query( KeyConditionExpression=( - Key('PK').eq('hunt-{}'.format(hunt_id)) & + Key('hunt_id').eq(hunt_id) & Key('SK').begins_with('puzzle-') ) ) diff --git a/turbot/interaction.py b/turbot/interaction.py index 0618188..31d5549 100644 --- a/turbot/interaction.py +++ b/turbot/interaction.py @@ -108,11 +108,11 @@ def new_hunt_submission(turb, payload, metadata): turb.table = turb.db.create_table( TableName='turbot', KeySchema=[ - {'AttributeName': 'PK', 'KeyType': 'HASH'}, + {'AttributeName': 'hunt_id', 'KeyType': 'HASH'}, {'AttributeName': 'SK', 'KeyType': 'RANGE'}, ], AttributeDefinitions=[ - {'AttributeName': 'PK', 'AttributeType': 'S'}, + {'AttributeName': 'hunt_id', 'AttributeType': 'S'}, {'AttributeName': 'SK', 'AttributeType': 'S'}, {'AttributeName': 'channel_id', 'AttributeType': 'S'}, {'AttributeName': 'is_hunt', 'AttributeType': 'S'}, @@ -168,10 +168,9 @@ def new_hunt_submission(turb, payload, metadata): # (leaving it as non-active for now until the channel-created handler # finishes fixing it up with a sheet and a companion table) item={ - "PK": "hunt-{}".format(hunt_id), + "hunt_id": hunt_id, "SK": "hunt-{}".format(hunt_id), "is_hunt": hunt_id, - "hunt_id": hunt_id, "channel_id": channel_id, "active": False, "name": name, @@ -297,7 +296,7 @@ def find_hunt_for_hunt_id(turb, hunt_id): """ turbot_table = turb.db.Table("turbot") - response = turbot_table.get_item(Key={'PK': 'hunt-{}'.format(hunt_id)}) + response = turbot_table.get_item(Key={'hunt_id': hunt_id}) if 'Item' in response: return response['Item'] @@ -402,7 +401,7 @@ def puzzle_submission(turb, payload, metadata): # Insert the newly-created puzzle into the database item={ - "PK": "hunt-{}".format(hunt_id), + "hunt_id": hunt_id, "SK": "puzzle-{}".format(puzzle_id), "puzzle_id": puzzle_id, "channel_id": channel_id, -- 2.43.0