@davoodmehraban89 / LifeMate / commits / b324335
Fork 0

LifeMate — Personal, Family & Learning Companion

feat: add identity and family PostgreSQL schema

davoodmehraban89 committed Oct 5, 2026 at 07:31 UTC b3243358d00f3d9658ce2b1ed20edf32333422ab
1 file changed +100
backend/migrations/0001_identity_family.sql new
+100
@@ -0,0 +1,100 @@
1 +begin;
2 +
3 +create extension if not exists pgcrypto;
4 +
5 +create type member_role as enum ('owner_admin','parent_guardian','teen_minor','adult_member');
6 +create type invitation_status as enum ('pending','accepted','revoked','expired');
7 +create type visibility_scope as enum ('private','selected_members','parent_guardian','family','safety_controlled');
8 +
9 +create table app_user (
10 + id uuid primary key default gen_random_uuid(),
11 + identity_subject text not null unique,
12 + email_normalized text not null unique check (email_normalized = lower(email_normalized)),
13 + email_verified_at timestamptz,
14 + created_at timestamptz not null default now(),
15 + disabled_at timestamptz
16 +);
17 +
18 +create table profile (
19 + user_id uuid primary key references app_user(id) on delete cascade,
20 + display_name text not null check (char_length(display_name) between 1 and 100),
21 + birth_date date,
22 + theme_preference text check (theme_preference in ('girl_pink','boy_blue','adult_blue','custom')),
23 + updated_at timestamptz not null default now()
24 +);
25 +
26 +create table family_workspace (
27 + id uuid primary key default gen_random_uuid(),
28 + name text not null check (char_length(name) between 1 and 120),
29 + created_by uuid not null references app_user(id),
30 + created_at timestamptz not null default now(),
31 + archived_at timestamptz
32 +);
33 +
34 +create table family_membership (
35 + family_id uuid not null references family_workspace(id) on delete cascade,
36 + user_id uuid not null references app_user(id) on delete cascade,
37 + role member_role not null,
38 + joined_at timestamptz not null default now(),
39 + ended_at timestamptz,
40 + primary key (family_id, user_id)
41 +);
42 +
43 +create table guardian_relationship (
44 + family_id uuid not null,
45 + guardian_user_id uuid not null,
46 + minor_user_id uuid not null,
47 + active boolean not null default true,
48 + created_at timestamptz not null default now(),
49 + primary key (family_id, guardian_user_id, minor_user_id),
50 + foreign key (family_id, guardian_user_id) references family_membership(family_id, user_id) on delete cascade,
51 + foreign key (family_id, minor_user_id) references family_membership(family_id, user_id) on delete cascade,
52 + check (guardian_user_id <> minor_user_id)
53 +);
54 +
55 +create table family_invitation (
56 + id uuid primary key default gen_random_uuid(),
57 + family_id uuid not null references family_workspace(id) on delete cascade,
58 + invited_email_normalized text not null check (invited_email_normalized = lower(invited_email_normalized)),
59 + intended_role member_role not null,
60 + token_digest text not null unique,
61 + status invitation_status not null default 'pending',
62 + invited_by uuid not null references app_user(id),
63 + expires_at timestamptz not null,
64 + accepted_by uuid references app_user(id),
65 + created_at timestamptz not null default now(),
66 + accepted_at timestamptz,
67 + check (expires_at > created_at)
68 +);
69 +
70 +create table access_audit (
71 + id bigint generated always as identity primary key,
72 + actor_user_id uuid references app_user(id),
73 + family_id uuid references family_workspace(id),
74 + action text not null,
75 + target_type text not null,
76 + target_id text,
77 + occurred_at timestamptz not null default now(),
78 + metadata jsonb not null default '{}'::jsonb
79 +);
80 +
81 +create index family_membership_user_idx on family_membership(user_id) where ended_at is null;
82 +create index family_invitation_pending_idx on family_invitation(family_id, invited_email_normalized) where status = 'pending';
83 +create index access_audit_family_time_idx on access_audit(family_id, occurred_at desc);
84 +
85 +create function is_active_family_member(p_family uuid, p_user uuid) returns boolean
86 +language sql stable as $$
87 + select exists(select 1 from family_membership m where m.family_id=p_family and m.user_id=p_user and m.ended_at is null)
88 +$$;
89 +
90 +create function is_active_guardian(p_family uuid, p_guardian uuid, p_minor uuid) returns boolean
91 +language sql stable as $$
92 + select exists(
93 + select 1 from guardian_relationship g
94 + join family_membership gm on gm.family_id=g.family_id and gm.user_id=g.guardian_user_id and gm.ended_at is null
95 + join family_membership mm on mm.family_id=g.family_id and mm.user_id=g.minor_user_id and mm.ended_at is null
96 + where g.family_id=p_family and g.guardian_user_id=p_guardian and g.minor_user_id=p_minor and g.active
97 + )
98 +$$;
99 +
100 +commit;