From 82e895010be1ad16e679141673a39ac8e0266400 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Thu, 13 Aug 2026 15:07:37 +0300 Subject: [PATCH] Drop leftover Learn English one-practice unique and include nested lesson practices in course and module lists. Co-authored-by: Cursor --- ..._lms_drop_one_practice_per_parent.down.sql | 8 ++ ...06_lms_drop_one_practice_per_parent.up.sql | 46 ++++++++++++ db/query/lms_practices.sql | 54 ++++++++++++-- gen/db/lms_practices.sql.go | 74 ++++++++++++++----- internal/domain/practice.go | 6 +- internal/repository/lms_practices.go | 6 +- 6 files changed, 165 insertions(+), 29 deletions(-) create mode 100644 db/migrations/000106_lms_drop_one_practice_per_parent.down.sql create mode 100644 db/migrations/000106_lms_drop_one_practice_per_parent.up.sql diff --git a/db/migrations/000106_lms_drop_one_practice_per_parent.down.sql b/db/migrations/000106_lms_drop_one_practice_per_parent.down.sql new file mode 100644 index 00000000..444f2719 --- /dev/null +++ b/db/migrations/000106_lms_drop_one_practice_per_parent.down.sql @@ -0,0 +1,8 @@ +DELETE FROM lms_practice_parents a + USING lms_practice_parents b +WHERE a.id > b.id + AND a.parent_kind = b.parent_kind + AND a.parent_id = b.parent_id; + +ALTER TABLE lms_practice_parents + ADD CONSTRAINT lms_practice_parents_parent_kind_parent_id_key UNIQUE (parent_kind, parent_id); diff --git a/db/migrations/000106_lms_drop_one_practice_per_parent.up.sql b/db/migrations/000106_lms_drop_one_practice_per_parent.up.sql new file mode 100644 index 00000000..ac5e8fd4 --- /dev/null +++ b/db/migrations/000106_lms_drop_one_practice_per_parent.up.sql @@ -0,0 +1,46 @@ +-- 000105 dropped this unique by a single constraint name. Re-drop by definition so +-- Learn English can store multiple practices per course, module, or lesson even if +-- the unique was created under a different name or as an index. +DO $$ +DECLARE + r record; +BEGIN + FOR r IN + SELECT c.conname + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'lms_practice_parents' + AND c.contype = 'u' + AND pg_get_constraintdef(c.oid) ~* 'UNIQUE\s*\(\s*parent_kind\s*,\s*parent_id\s*\)' + LOOP + EXECUTE format('ALTER TABLE public.lms_practice_parents DROP CONSTRAINT IF EXISTS %I', r.conname); + END LOOP; +END $$; + +DO $$ +DECLARE + r record; +BEGIN + FOR r IN + SELECT i.relname AS idxname + FROM pg_index x + JOIN pg_class i ON i.oid = x.indexrelid + JOIN pg_class t ON t.oid = x.indrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'lms_practice_parents' + AND x.indisunique + AND NOT x.indisprimary + AND pg_get_indexdef(i.oid) LIKE '%(parent_kind, parent_id)%' + AND pg_get_indexdef(i.oid) NOT LIKE '%(practice_id, parent_kind, parent_id)%' + LOOP + EXECUTE format('DROP INDEX IF EXISTS public.%I', r.idxname); + END LOOP; +END $$; + +DROP INDEX IF EXISTS lms_practice_parents_parent_kind_parent_id_key; +DROP INDEX IF EXISTS idx_lms_practices_one_per_course; +DROP INDEX IF EXISTS idx_lms_practices_one_per_module; +DROP INDEX IF EXISTS idx_lms_practices_one_per_lesson; diff --git a/db/query/lms_practices.sql b/db/query/lms_practices.sql index fa39f4e4..b936d925 100644 --- a/db/query/lms_practices.sql +++ b/db/query/lms_practices.sql @@ -30,15 +30,39 @@ SELECT p.created_at, p.updated_at FROM lms_practices p - INNER JOIN lms_practice_parents lpp ON lpp.practice_id = p.id -WHERE lpp.parent_kind = 'COURSE' - AND lpp.parent_id = $1 +WHERE EXISTS ( + SELECT 1 + FROM lms_practice_parents lpp + WHERE lpp.practice_id = p.id + AND ( + (lpp.parent_kind = 'COURSE' AND lpp.parent_id = sqlc.arg('parent_id')) + OR ( + lpp.parent_kind = 'MODULE' + AND EXISTS ( + SELECT 1 + FROM modules m + WHERE m.id = lpp.parent_id + AND m.course_id = sqlc.arg('parent_id') + ) + ) + OR ( + lpp.parent_kind = 'LESSON' + AND EXISTS ( + SELECT 1 + FROM lessons l + INNER JOIN modules m ON m.id = l.module_id + WHERE l.id = lpp.parent_id + AND m.course_id = sqlc.arg('parent_id') + ) + ) + ) +) AND ( sqlc.arg('published_only')::boolean = FALSE OR p.publish_status = 'PUBLISHED'::TEXT ) ORDER BY p.created_at DESC -LIMIT $2 OFFSET $3; +LIMIT sqlc.arg('limit') OFFSET sqlc.arg('offset'); -- name: ListLmsPracticesByModuleID :many SELECT @@ -54,15 +78,29 @@ SELECT p.created_at, p.updated_at FROM lms_practices p - INNER JOIN lms_practice_parents lpp ON lpp.practice_id = p.id -WHERE lpp.parent_kind = 'MODULE' - AND lpp.parent_id = $1 +WHERE EXISTS ( + SELECT 1 + FROM lms_practice_parents lpp + WHERE lpp.practice_id = p.id + AND ( + (lpp.parent_kind = 'MODULE' AND lpp.parent_id = sqlc.arg('parent_id')) + OR ( + lpp.parent_kind = 'LESSON' + AND EXISTS ( + SELECT 1 + FROM lessons l + WHERE l.id = lpp.parent_id + AND l.module_id = sqlc.arg('parent_id') + ) + ) + ) +) AND ( sqlc.arg('published_only')::boolean = FALSE OR p.publish_status = 'PUBLISHED'::TEXT ) ORDER BY p.created_at DESC -LIMIT $2 OFFSET $3; +LIMIT sqlc.arg('limit') OFFSET sqlc.arg('offset'); -- name: ListLmsPracticesByLessonID :many SELECT diff --git a/gen/db/lms_practices.sql.go b/gen/db/lms_practices.sql.go index 8f796f5f..06572e65 100644 --- a/gen/db/lms_practices.sql.go +++ b/gen/db/lms_practices.sql.go @@ -261,22 +261,46 @@ SELECT p.created_at, p.updated_at FROM lms_practices p - INNER JOIN lms_practice_parents lpp ON lpp.practice_id = p.id -WHERE lpp.parent_kind = 'COURSE' - AND lpp.parent_id = $1 +WHERE EXISTS ( + SELECT 1 + FROM lms_practice_parents lpp + WHERE lpp.practice_id = p.id + AND ( + (lpp.parent_kind = 'COURSE' AND lpp.parent_id = $1) + OR ( + lpp.parent_kind = 'MODULE' + AND EXISTS ( + SELECT 1 + FROM modules m + WHERE m.id = lpp.parent_id + AND m.course_id = $1 + ) + ) + OR ( + lpp.parent_kind = 'LESSON' + AND EXISTS ( + SELECT 1 + FROM lessons l + INNER JOIN modules m ON m.id = l.module_id + WHERE l.id = lpp.parent_id + AND m.course_id = $1 + ) + ) + ) +) AND ( - $4::boolean = FALSE + $2::boolean = FALSE OR p.publish_status = 'PUBLISHED'::TEXT ) ORDER BY p.created_at DESC -LIMIT $2 OFFSET $3 +LIMIT $4 OFFSET $3 ` type ListLmsPracticesByCourseIDParams struct { ParentID int64 `json:"parent_id"` - Limit int32 `json:"limit"` - Offset int32 `json:"offset"` PublishedOnly bool `json:"published_only"` + Offset int32 `json:"offset"` + Limit int32 `json:"limit"` } type ListLmsPracticesByCourseIDRow struct { @@ -296,9 +320,9 @@ type ListLmsPracticesByCourseIDRow struct { func (q *Queries) ListLmsPracticesByCourseID(ctx context.Context, arg ListLmsPracticesByCourseIDParams) ([]ListLmsPracticesByCourseIDRow, error) { rows, err := q.db.Query(ctx, ListLmsPracticesByCourseID, arg.ParentID, - arg.Limit, - arg.Offset, arg.PublishedOnly, + arg.Offset, + arg.Limit, ) if err != nil { return nil, err @@ -427,22 +451,36 @@ SELECT p.created_at, p.updated_at FROM lms_practices p - INNER JOIN lms_practice_parents lpp ON lpp.practice_id = p.id -WHERE lpp.parent_kind = 'MODULE' - AND lpp.parent_id = $1 +WHERE EXISTS ( + SELECT 1 + FROM lms_practice_parents lpp + WHERE lpp.practice_id = p.id + AND ( + (lpp.parent_kind = 'MODULE' AND lpp.parent_id = $1) + OR ( + lpp.parent_kind = 'LESSON' + AND EXISTS ( + SELECT 1 + FROM lessons l + WHERE l.id = lpp.parent_id + AND l.module_id = $1 + ) + ) + ) +) AND ( - $4::boolean = FALSE + $2::boolean = FALSE OR p.publish_status = 'PUBLISHED'::TEXT ) ORDER BY p.created_at DESC -LIMIT $2 OFFSET $3 +LIMIT $4 OFFSET $3 ` type ListLmsPracticesByModuleIDParams struct { ParentID int64 `json:"parent_id"` - Limit int32 `json:"limit"` - Offset int32 `json:"offset"` PublishedOnly bool `json:"published_only"` + Offset int32 `json:"offset"` + Limit int32 `json:"limit"` } type ListLmsPracticesByModuleIDRow struct { @@ -462,9 +500,9 @@ type ListLmsPracticesByModuleIDRow struct { func (q *Queries) ListLmsPracticesByModuleID(ctx context.Context, arg ListLmsPracticesByModuleIDParams) ([]ListLmsPracticesByModuleIDRow, error) { rows, err := q.db.Query(ctx, ListLmsPracticesByModuleID, arg.ParentID, - arg.Limit, - arg.Offset, arg.PublishedOnly, + arg.Offset, + arg.Limit, ) if err != nil { return nil, err diff --git a/internal/domain/practice.go b/internal/domain/practice.go index c2b8c8d1..ac10407e 100644 --- a/internal/domain/practice.go +++ b/internal/domain/practice.go @@ -139,10 +139,12 @@ func DedupePracticeParents(parents []PracticeParent) []PracticeParent { seen := make(map[PracticeParent]struct{}, len(parents)) out := make([]PracticeParent, 0, len(parents)) for _, p := range parents { - if p.Kind == "" || p.ID <= 0 { + kind, ok := ParsePracticeParentKind(string(p.Kind)) + if !ok || p.ID <= 0 { continue } - if _, ok := seen[p]; ok { + p.Kind = kind + if _, exists := seen[p]; exists { continue } seen[p] = struct{}{} diff --git a/internal/repository/lms_practices.go b/internal/repository/lms_practices.go index efad240d..e2827549 100644 --- a/internal/repository/lms_practices.go +++ b/internal/repository/lms_practices.go @@ -109,9 +109,13 @@ func (s *Store) attachParentsToPractices(ctx context.Context, practices []domain func (s *Store) insertLmsPracticeParents(ctx context.Context, q *dbgen.Queries, practiceID int64, parents []domain.PracticeParent) error { for _, parent := range parents { + kind, ok := domain.ParsePracticeParentKind(string(parent.Kind)) + if !ok { + continue + } if _, err := q.InsertLmsPracticeParent(ctx, dbgen.InsertLmsPracticeParentParams{ PracticeID: practiceID, - ParentKind: string(parent.Kind), + ParentKind: string(kind), ParentID: parent.ID, }); err != nil { return err