LifeMate — Personal, Family & Learning Companion

feat/private-planner-series-api
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
sql 123 lines 7.83 KB
Raw
1 begin;
2
3 alter table life_context add column version integer not null default 1 check(version>0);
4 alter table life_context add column updated_at timestamptz not null default now();
5 alter table academic_year add column version integer not null default 1 check(version>0);
6 alter table academic_year add column updated_at timestamptz not null default now();
7 alter table academic_term add column active boolean not null default true;
8 alter table academic_term add column version integer not null default 1 check(version>0);
9 alter table academic_term add column updated_at timestamptz not null default now();
10
11 create table school_lifecycle_mutation (
12 owner_user_id uuid not null references app_user(id) on delete cascade,
13 mutation_id uuid not null,
14 entity_type text not null check(entity_type in('contexts','years','terms')),
15 entity_id uuid not null,
16 operation text not null check(operation in('create','update','archive')),
17 request_hash text not null check(char_length(request_hash)=64),
18 applied_version integer not null check(applied_version>0),
19 accepted_at timestamptz not null default now(),
20 primary key(owner_user_id,mutation_id)
21 );
22
23 -- The same owner-row lock is used by the new router and these guards. It
24 -- serializes ancestor archival/date edits against the old create APIs too.
25 create function school_lifecycle_context_guard() returns trigger language plpgsql set search_path=public,pg_catalog as $$
26 begin
27 perform 1 from app_user where id=new.user_id for update;
28 if tg_op='UPDATE' then
29 if new.user_id<>old.user_id then raise exception 'school_owner_immutable' using errcode='23514'; end if;
30 if new.kind<>old.kind and exists(select 1 from academic_year where life_context_id=old.id) then
31 raise exception 'school_context_kind_in_use' using errcode='23514';
32 end if;
33 new.version=old.version+1; new.updated_at=clock_timestamp();
34 end if;
35 return new;
36 end $$;
37 create trigger school_lifecycle_context_guard before insert or update on life_context for each row execute function school_lifecycle_context_guard();
38
39 create function school_lifecycle_year_guard() returns trigger language plpgsql set search_path=public,pg_catalog as $$
40 declare context_row life_context;
41 begin
42 perform 1 from app_user where id=new.student_user_id for update;
43 if tg_op='UPDATE' and (new.student_user_id<>old.student_user_id or new.life_context_id<>old.life_context_id) then
44 raise exception 'school_parent_immutable' using errcode='23514';
45 end if;
46 select * into context_row from life_context where id=new.life_context_id for share;
47 if context_row.id is null or context_row.user_id<>new.student_user_id or context_row.kind<>'student' then
48 raise exception 'school_parent_forbidden' using errcode='23514';
49 end if;
50 if new.active and not context_row.active then raise exception 'school_parent_inactive' using errcode='23514'; end if;
51 if char_length(trim(new.title)) not between 1 and 120 then raise exception 'school_invalid_title' using errcode='23514'; end if;
52 if tg_op='UPDATE' then
53 if exists(select 1 from academic_term where academic_year_id=old.id and (starts_on<new.starts_on or ends_on>new.ends_on)) then
54 raise exception 'school_dates_in_use' using errcode='23514';
55 end if;
56 new.version=old.version+1; new.updated_at=clock_timestamp();
57 end if;
58 return new;
59 end $$;
60 create trigger school_lifecycle_year_guard before insert or update on academic_year for each row execute function school_lifecycle_year_guard();
61
62 create function school_lifecycle_term_guard() returns trigger language plpgsql set search_path=public,pg_catalog as $$
63 declare year_row academic_year; context_row life_context;
64 begin
65 select * into year_row from academic_year where id=new.academic_year_id;
66 perform 1 from app_user where id=year_row.student_user_id for update;
67 select * into year_row from academic_year where id=new.academic_year_id for share;
68 select * into context_row from life_context where id=year_row.life_context_id for share;
69 if tg_op='UPDATE' and new.academic_year_id<>old.academic_year_id then raise exception 'school_parent_immutable' using errcode='23514'; end if;
70 if year_row.id is null or context_row.id is null or context_row.user_id<>year_row.student_user_id or context_row.kind<>'student' then
71 raise exception 'school_parent_forbidden' using errcode='23514';
72 end if;
73 if new.active and (not year_row.active or not context_row.active) then raise exception 'school_parent_inactive' using errcode='23514'; end if;
74 if new.starts_on<year_row.starts_on or new.ends_on>year_row.ends_on then raise exception 'school_dates_outside_parent' using errcode='23514'; end if;
75 if char_length(trim(new.title)) not between 1 and 120 then raise exception 'school_invalid_title' using errcode='23514'; end if;
76 if tg_op='UPDATE' then
77 if exists(select 1 from class_session c join subject s on s.id=c.subject_id where s.academic_term_id=old.id and c.recurrence_until is not null and (c.recurrence_until<new.starts_on or c.recurrence_until>new.ends_on)) then
78 raise exception 'school_dates_in_use' using errcode='23514';
79 end if;
80 new.version=old.version+1; new.updated_at=clock_timestamp();
81 end if;
82 return new;
83 end $$;
84 create trigger school_lifecycle_term_guard before insert or update on academic_term for each row execute function school_lifecycle_term_guard();
85
86 create function school_lifecycle_subject_guard() returns trigger language plpgsql set search_path=public,pg_catalog as $$
87 declare parent_row record;
88 begin
89 perform 1 from app_user where id=new.student_user_id for update;
90 if tg_op='UPDATE' and new.student_user_id<>old.student_user_id then raise exception 'school_owner_immutable' using errcode='23514'; end if;
91 select t.active as term_active,y.active as year_active,l.active as context_active,y.student_user_id,l.user_id,l.kind
92 into parent_row from academic_term t join academic_year y on y.id=t.academic_year_id join life_context l on l.id=y.life_context_id where t.id=new.academic_term_id for share of t,y,l;
93 if not found or parent_row.student_user_id<>new.student_user_id or parent_row.user_id<>new.student_user_id then
94 raise exception 'school_parent_forbidden' using errcode='23514';
95 end if;
96 if new.archived_at is null and (not parent_row.term_active or not parent_row.year_active or not parent_row.context_active or parent_row.kind<>'student') then
97 raise exception 'school_parent_inactive' using errcode='23514';
98 end if;
99 return new;
100 end $$;
101 create trigger school_lifecycle_subject_guard before insert or update on subject for each row execute function school_lifecycle_subject_guard();
102
103 create function school_lifecycle_class_guard() returns trigger language plpgsql set search_path=public,pg_catalog as $$
104 declare parent_row record; owner_id uuid;
105 begin
106 select student_user_id into owner_id from subject where id=new.subject_id;
107 perform 1 from app_user where id=owner_id for update;
108 select s.archived_at,t.active as term_active,y.active as year_active,l.active as context_active,t.starts_on,t.ends_on
109 into parent_row from subject s join academic_term t on t.id=s.academic_term_id join academic_year y on y.id=t.academic_year_id join life_context l on l.id=y.life_context_id where s.id=new.subject_id for share of s,t,y,l;
110 if not found then raise exception 'school_parent_forbidden' using errcode='23514'; end if;
111 if new.archived_at is null then
112 if parent_row.archived_at is not null or not parent_row.term_active or not parent_row.year_active or not parent_row.context_active then
113 raise exception 'school_parent_inactive' using errcode='23514';
114 end if;
115 if new.recurrence_until is not null and (new.recurrence_until<parent_row.starts_on or new.recurrence_until>parent_row.ends_on) then
116 raise exception 'school_dates_outside_parent' using errcode='23514';
117 end if;
118 end if;
119 return new;
120 end $$;
121 create trigger school_lifecycle_class_guard before insert or update on class_session for each row execute function school_lifecycle_class_guard();
122
123 commit;