infra(gsi): gsi changes#19
Conversation
There was a problem hiding this comment.
Review Summary
This PR attempts to optimize DynamoDB design by consolidating GSIs, but contains critical blocking issues that will prevent deployment and cause runtime failures:
🚨 Critical Issues (Must Fix)
-
DynamoDB Configuration Error: The CDK configuration attempts to define multiple sort keys for a GSI, which is not supported by DynamoDB. This will cause deployment failure.
-
Query Pattern Mismatch: The database queries use 3-attribute key conditions (
Category + SkillName + ProficiencyLevel) but DynamoDB only supports partition key + sort key queries. -
Duplicate Code: There's a duplicate
PutItemcall in the CreateSkill method that will cause the operation to execute twice.
📋 Required Actions
- Fix CDK GSI Configuration: Update the GSI to use only one partition key and one sort key
- Align Query Patterns: Modify database queries to match DynamoDB's key condition limitations
- Remove Duplicate Code: Fix the duplicate PutItem call
💡 Recommendations
Consider using a composite sort key approach where you concatenate values (e.g., SkillName#ProficiencyLevel#YearsOfExperience) as a single sort key, or use FilterExpression for additional filtering on non-key attributes.
The optimization goal is sound, but the implementation needs significant corrections to work with DynamoDB's constraints.
You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.
Summary
Optimize DynamoDB single-table design by consolidating from 5 GSIs to 1 GSI, reducing infrastructure costs and complexity while maintaining query performance.
Changes
Infrastructure (deployments/app/cdk.go)
EntityTypeas partition key instead ofentity_idBySkill)SkillsByLevel,ByUser,SkillsByCategory,ByEntityType,BySkillIDSkillName(PK) +ProficiencyLevel(SK)Database Layer (cmd/app/internal/database/)
constants.gowith new GSI name and attribute constantsuser_skill_repository_dynamodb.goto use new query patterns:ListUsersBySkill: Now queriesBySkillGSI withSkillNamepartition keyListUsersBySkillAndLevel: QueriesBySkillGSI with skill name and proficiency levelListUserSkills: Uses main table withEntityTypepartition keyService Layer (cmd/app/internal/service/skill_service.go)
ListUsersBySkill(skillName): Find all users with a specific skillListUsersBySkillAndLevel(skillName, level): Find users by skill and proficiencyHandler Layer (cmd/app/internal/handler/)
ListUsersBySkillHandler: GET/skills/{skillName}/usersListUsersBySkillAndLevelHandler: GET/skills/{skillName}/users?level={level}Router (cmd/app/internal/router/router.go)
Benefits
Query Pattern Support
EntityType = "User"(main table)EntityType = "Skill"(main table)EntityType = "UserSkill"with entity_id prefix (main table)SkillName = "Python"(BySkill GSI)SkillName = "Python" AND ProficiencyLevel = "Expert"(BySkill GSI)