Drop leftover Learn English one-practice unique and include nested lesson practices in course and module lists.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
3304d8818f
commit
82e895010b
|
|
@ -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);
|
||||
46
db/migrations/000106_lms_drop_one_practice_per_parent.up.sql
Normal file
46
db/migrations/000106_lms_drop_one_practice_per_parent.up.sql
Normal file
|
|
@ -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;
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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{}{}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user