create-expo-updates-server 0.3.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/README.md +4 -2
  2. package/dist/template/Dockerfile +4 -6
  3. package/dist/template/README.md +5 -10
  4. package/dist/template/app/api/admin/device-favorites/route.test.ts +125 -0
  5. package/dist/template/app/api/admin/device-favorites/route.ts +91 -0
  6. package/dist/template/app/api/admin/distribution-history/restore/route.ts +39 -0
  7. package/dist/template/app/api/admin/distribution-history/route.test.ts +55 -0
  8. package/dist/template/app/api/admin/distribution-history/route.ts +24 -0
  9. package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +2 -1
  10. package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +2 -1
  11. package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +2 -1
  12. package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +2 -1
  13. package/dist/template/app/api/admin/updates/route.test.ts +21 -0
  14. package/dist/template/app/api/admin/updates/route.ts +12 -1
  15. package/dist/template/app/globals.css +65 -68
  16. package/dist/template/app/login/page.tsx +66 -42
  17. package/dist/template/app/updates/[encodedKey]/page.tsx +382 -248
  18. package/dist/template/app/updates/devices/page.tsx +187 -145
  19. package/dist/template/app/updates/embedded/page.tsx +80 -34
  20. package/dist/template/app/updates/emergency-redirects/page.tsx +138 -71
  21. package/dist/template/app/updates/layout.tsx +2 -1
  22. package/dist/template/app/updates/page.tsx +385 -274
  23. package/dist/template/app/updates/releases.test.tsx +156 -0
  24. package/dist/template/components/admin/admin-role.ts +5 -0
  25. package/dist/template/components/admin/admin-shell.tsx +166 -95
  26. package/dist/template/components/admin/confirm-action.tsx +123 -0
  27. package/dist/template/components/admin/language-switcher.tsx +27 -16
  28. package/dist/template/components/admin/release-badges.test.tsx +50 -0
  29. package/dist/template/components/admin/release-badges.tsx +101 -0
  30. package/dist/template/components/ui/alert.tsx +10 -3
  31. package/dist/template/components/ui/avatar.tsx +108 -0
  32. package/dist/template/components/ui/badge.tsx +2 -2
  33. package/dist/template/components/ui/breadcrumb.tsx +108 -0
  34. package/dist/template/components/ui/button.tsx +5 -5
  35. package/dist/template/components/ui/card.tsx +2 -2
  36. package/dist/template/components/ui/chart.tsx +1 -1
  37. package/dist/template/components/ui/collapsible.tsx +7 -0
  38. package/dist/template/components/ui/dialog.tsx +13 -4
  39. package/dist/template/components/ui/empty.tsx +103 -0
  40. package/dist/template/components/ui/field.tsx +248 -0
  41. package/dist/template/components/ui/input-group.tsx +170 -0
  42. package/dist/template/components/ui/input.tsx +1 -1
  43. package/dist/template/components/ui/native-select.tsx +1 -1
  44. package/dist/template/components/ui/radio-group.tsx +41 -0
  45. package/dist/template/components/ui/sheet.tsx +6 -2
  46. package/dist/template/components/ui/sidebar.tsx +62 -17
  47. package/dist/template/components/ui/status-tones.ts +15 -0
  48. package/dist/template/components/ui/table.tsx +1 -1
  49. package/dist/template/components/ui/textarea.tsx +1 -1
  50. package/dist/template/docs/admin-ui.md +22 -24
  51. package/dist/template/docs/device-targeting.md +6 -12
  52. package/dist/template/docs/doctor.md +2 -2
  53. package/dist/template/docs/expo-app-setup.md +9 -27
  54. package/dist/template/docs/getting-started.md +3 -4
  55. package/dist/template/docs/getting-started.ru.md +11 -544
  56. package/dist/template/docs/index.md +1 -1
  57. package/dist/template/docs/installation-telemetry.md +6 -12
  58. package/dist/template/docs/migrations/2026-10-16-manifest-identity.sql +5 -0
  59. package/dist/template/docs/migrations/2026-11-25-device-favorites.sql +53 -0
  60. package/dist/template/docs/migrations/2026-11-25-download-on-postpone.sql +5 -0
  61. package/dist/template/docs/migrations/2026-11-25-public-distribution-history.sql +283 -0
  62. package/dist/template/docs/migrations/schema.sql +349 -0
  63. package/dist/template/docs/npm-installer.md +3 -3
  64. package/dist/template/docs/operations-guide.md +9 -7
  65. package/dist/template/docs/ota-client-lifecycle.md +30 -68
  66. package/dist/template/docs/ota-emergency-recovery.md +34 -57
  67. package/dist/template/docs/ota-update-policy.md +9 -20
  68. package/dist/template/docs/ru/admin-ui.md +33 -0
  69. package/dist/template/docs/ru/device-targeting.md +59 -0
  70. package/dist/template/docs/ru/doctor.md +98 -0
  71. package/dist/template/docs/ru/expo-app-setup.md +497 -0
  72. package/dist/template/docs/ru/getting-started.md +391 -0
  73. package/dist/template/docs/ru/index.md +50 -0
  74. package/dist/template/docs/ru/installation-telemetry.md +125 -0
  75. package/dist/template/docs/ru/npm-installer.md +71 -0
  76. package/dist/template/docs/ru/operations-guide.md +322 -0
  77. package/dist/template/docs/ru/ota-client-lifecycle.md +162 -0
  78. package/dist/template/docs/ru/ota-emergency-recovery.md +108 -0
  79. package/dist/template/docs/ru/ota-update-policy.md +69 -0
  80. package/dist/template/docs/ru/publishing-npm-installer.md +49 -0
  81. package/dist/template/docs/ru/supabase-setup.md +120 -0
  82. package/dist/template/docs/uk/admin-ui.md +33 -0
  83. package/dist/template/docs/uk/device-targeting.md +59 -0
  84. package/dist/template/docs/uk/doctor.md +98 -0
  85. package/dist/template/docs/uk/expo-app-setup.md +497 -0
  86. package/dist/template/docs/uk/getting-started.md +391 -0
  87. package/dist/template/docs/uk/index.md +50 -0
  88. package/dist/template/docs/uk/installation-telemetry.md +125 -0
  89. package/dist/template/docs/uk/npm-installer.md +71 -0
  90. package/dist/template/docs/uk/operations-guide.md +322 -0
  91. package/dist/template/docs/uk/ota-client-lifecycle.md +162 -0
  92. package/dist/template/docs/uk/ota-emergency-recovery.md +108 -0
  93. package/dist/template/docs/uk/ota-update-policy.md +69 -0
  94. package/dist/template/docs/uk/publishing-npm-installer.md +49 -0
  95. package/dist/template/docs/uk/supabase-setup.md +120 -0
  96. package/dist/template/lib/audience-ids.ts +4 -0
  97. package/dist/template/package-lock.json +46 -0
  98. package/dist/template/package.json +4 -1
  99. package/dist/template/scripts/configure-expo-app.mjs +3 -3
  100. package/dist/template/scripts/create-access-token.mjs +1 -1
  101. package/dist/template/scripts/lib/doctor.mjs +10 -7
  102. package/dist/template/scripts/lib/doctor.test.mjs +7 -5
  103. package/dist/template/scripts/lib/embedded-update-registrar.test.ts +22 -7
  104. package/dist/template/scripts/lib/expo-app-configurator.mjs +25 -22
  105. package/dist/template/scripts/lib/expo-app-configurator.test.ts +37 -2
  106. package/dist/template/scripts/lib/ota-client-configurator.mjs +152 -55
  107. package/dist/template/scripts/lib/ota-client-configurator.test.ts +60 -20
  108. package/dist/template/scripts/lib/ota-client-package.mjs +25 -0
  109. package/dist/template/scripts/lib/ota-root-hook-patcher.mjs +90 -32
  110. package/dist/template/scripts/lib/ota-root-hook-patcher.test.ts +49 -6
  111. package/dist/template/scripts/tests/device-analytics-migrations.mjs +12 -1
  112. package/dist/template/scripts/tests/device-targeting-activation.mjs +2 -3
  113. package/dist/template/scripts/tests/e2e/client-lifecycle.mjs +161 -0
  114. package/dist/template/scripts/tests/e2e/client-lifecycle.scenario.jsx +465 -0
  115. package/dist/template/scripts/tests/e2e/client-vitest.mts +18 -0
  116. package/dist/template/scripts/tests/e2e/console.mjs +86 -0
  117. package/dist/template/scripts/tests/e2e/delivery.mjs +296 -0
  118. package/dist/template/scripts/tests/e2e/device-favorites.mjs +309 -0
  119. package/dist/template/scripts/tests/e2e/installations.mjs +132 -0
  120. package/dist/template/scripts/tests/e2e/policy.mjs +253 -0
  121. package/dist/template/scripts/tests/e2e/registrar-token.mjs +59 -0
  122. package/dist/template/scripts/tests/e2e/release-matrix.mjs +307 -0
  123. package/dist/template/scripts/tests/e2e/releases.mjs +162 -0
  124. package/dist/template/scripts/tests/e2e/run.mjs +224 -0
  125. package/dist/template/scripts/tests/manifest-identity.sql +7 -5
  126. package/dist/template/scripts/tests/public-distribution-history.mjs +102 -0
  127. package/dist/template/src/client/admin-api.ts +45 -10
  128. package/dist/template/src/client/admin-types.ts +35 -1
  129. package/dist/template/src/client/devices/analytics.tsx +169 -219
  130. package/dist/template/src/client/devices/cohorts.tsx +181 -0
  131. package/dist/template/src/client/devices/device-name.tsx +37 -21
  132. package/dist/template/src/client/devices/favorite-targets.tsx +381 -0
  133. package/dist/template/src/client/devices/registry.tsx +265 -233
  134. package/dist/template/src/client/devices/use-devices-report.test.tsx +98 -0
  135. package/dist/template/src/client/devices/use-devices-report.ts +53 -82
  136. package/dist/template/src/client/devices/version-adoption-chart.tsx +48 -38
  137. package/dist/template/src/client/distribution-control.tsx +98 -53
  138. package/dist/template/src/client/distribution-history.tsx +353 -0
  139. package/dist/template/src/client/guard-action-catalog.tsx +183 -0
  140. package/dist/template/src/client/guard-action-combobox.test.ts +16 -22
  141. package/dist/template/src/client/guard-action-combobox.tsx +134 -234
  142. package/dist/template/src/client/guard-action-options.ts +4 -6
  143. package/dist/template/src/client/i18n/en.json +335 -2
  144. package/dist/template/src/client/i18n/language-switching.test.tsx +233 -0
  145. package/dist/template/src/client/i18n/messages.test.ts +102 -19
  146. package/dist/template/src/client/i18n/messages.ts +31 -16
  147. package/dist/template/src/client/i18n/provider.tsx +6 -3
  148. package/dist/template/src/client/i18n/ru.json +619 -0
  149. package/dist/template/src/client/i18n/uk.json +619 -0
  150. package/dist/template/src/client/i18n/use-formatters.ts +5 -0
  151. package/dist/template/src/client/logout-button.tsx +3 -3
  152. package/dist/template/src/client/policy-draft.ts +138 -0
  153. package/dist/template/src/client/update-audience-editor.tsx +597 -199
  154. package/dist/template/src/client/update-policy-editor.test.ts +101 -29
  155. package/dist/template/src/client/update-policy-editor.tsx +466 -307
  156. package/dist/template/src/client/use-filtered-results.ts +55 -0
  157. package/dist/template/src/server/controllers/manifest.test.ts +23 -3
  158. package/dist/template/src/server/controllers/manifest.ts +15 -1
  159. package/dist/template/src/server/devices/audience.ts +9 -5
  160. package/dist/template/src/server/lib/admin-audience-activation.test.ts +37 -90
  161. package/dist/template/src/server/lib/admin-release-history.test.ts +138 -0
  162. package/dist/template/src/server/lib/admin-updates.ts +160 -534
  163. package/dist/template/src/server/lib/distribution-history.ts +97 -0
  164. package/dist/template/src/server/lib/supabase.js +24 -0
  165. package/dist/template/src/server/lib/supabase.test.ts +38 -1
  166. package/dist/template/src/server/lib/update-policy.test.ts +20 -0
  167. package/dist/template/src/server/lib/update-policy.ts +23 -10
  168. package/dist/template/src/server/manifest/manifest-repository.ts +11 -3
  169. package/dist/template/src/server/manifest/manifest-values.test.ts +2 -0
  170. package/dist/template/src/server/manifest/manifest-values.ts +21 -13
  171. package/dist/template/templates/expo-app/scripts/ota-publish/api/database-client.mjs +7 -30
  172. package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +2 -1
  173. package/dist/template/tsconfig.json +2 -2
  174. package/dist/template-manifest.json +571 -271
  175. package/package.json +1 -1
  176. package/dist/template/scripts/lib/device-identity.test.ts +0 -29
  177. package/dist/template/scripts/lib/ota-client.test.ts +0 -55
  178. package/dist/template/scripts/lib/ota-update-controller.test.ts +0 -232
  179. package/dist/template/scripts/lib/ota-updates-ui.test.tsx +0 -154
  180. package/dist/template/scripts/tests/ota-client-types.mjs +0 -69
  181. package/dist/template/templates/expo-app/ota-client/README.md +0 -73
  182. package/dist/template/templates/expo-app/ota-client/controller.ts +0 -107
  183. package/dist/template/templates/expo-app/ota-client/device-identity.ts +0 -48
  184. package/dist/template/templates/expo-app/ota-client/docs/update-lifecycle.md +0 -61
  185. package/dist/template/templates/expo-app/ota-client/index.ts +0 -16
  186. package/dist/template/templates/expo-app/ota-client/model/hooks/useOtaUpdates.ts +0 -97
  187. package/dist/template/templates/expo-app/ota-client/model/update-controller.ts +0 -293
  188. package/dist/template/templates/expo-app/ota-client/runtime.ts +0 -153
  189. package/dist/template/templates/expo-app/ota-client/types.ts +0 -85
  190. package/dist/template/templates/expo-app/ota-client/ui/ota-update-gate.tsx +0 -25
  191. package/dist/template/templates/expo-app/ota-client/ui/ota-updates.tsx +0 -196
@@ -0,0 +1,53 @@
1
+ -- Personal favorites for the release audience picker. Apply after device targeting.
2
+ begin;
3
+
4
+ create table if not exists public.ota_device_favorites (
5
+ user_id uuid not null references public.ota_admin_users(id) on delete cascade,
6
+ platform text not null check (platform in ('ios', 'android')),
7
+ target_kind text not null,
8
+ target_id text not null,
9
+ name text not null check (length(name) between 1 and 80 and name = btrim(name) and name !~ '[[:cntrl:]]'),
10
+ primary key (user_id, platform, target_kind, target_id),
11
+ check ((target_kind = 'device' and target_id ~ '^[0-9a-f]{64}$')
12
+ or (target_kind = 'installation' and target_id ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'))
13
+ );
14
+
15
+ -- Preserve the bookmark when telemetry expires. Resolve against the full registry,
16
+ -- including installations outside the audience picker's recent-results window.
17
+ create or replace function public.ota_list_device_favorites(p_user uuid, p_platform text)
18
+ returns table(target_kind text, target_id text, name text, last_seen_at timestamptz)
19
+ language sql stable security invoker set search_path = public, pg_temp as $$
20
+ select f.target_kind, f.target_id, f.name,
21
+ case when f.target_kind = 'device' then (
22
+ select max(s.last_seen_at) from public.ota_device_state s
23
+ where s.platform = f.platform and s.device_id = f.target_id
24
+ ) else (
25
+ select s.last_seen_at from public.ota_device_state s
26
+ where s.platform = f.platform and s.installation_id = f.target_id
27
+ ) end
28
+ from public.ota_device_favorites f
29
+ where f.user_id = p_user and f.platform = p_platform
30
+ order by lower(f.name), f.target_kind, f.target_id
31
+ $$;
32
+
33
+ alter table public.ota_device_favorites enable row level security;
34
+ revoke all on public.ota_device_favorites from public;
35
+ revoke all on function public.ota_list_device_favorites(uuid, text) from public;
36
+ do $$
37
+ declare role_name text;
38
+ begin
39
+ foreach role_name in array array['anon', 'authenticated'] loop
40
+ if exists (select from pg_roles where rolname = role_name) then
41
+ execute format('revoke all on public.ota_device_favorites from %I', role_name);
42
+ execute format('revoke all on function public.ota_list_device_favorites(uuid, text) from %I', role_name);
43
+ end if;
44
+ end loop;
45
+ if exists (select from pg_roles where rolname = 'service_role') then
46
+ grant select, insert, update, delete on public.ota_device_favorites to service_role;
47
+ grant execute on function public.ota_list_device_favorites(uuid, text) to service_role;
48
+ drop policy if exists ota_device_favorites_service on public.ota_device_favorites;
49
+ create policy ota_device_favorites_service on public.ota_device_favorites
50
+ to service_role using (true) with check (true);
51
+ end if;
52
+ end $$;
53
+ commit;
@@ -0,0 +1,5 @@
1
+ alter table public.ota_updates
2
+ drop constraint if exists ota_updates_delivery_mode_chk,
3
+ add constraint ota_updates_delivery_mode_chk
4
+ check (delivery_mode in ('manual', 'background', 'manual-postpone')
5
+ and (delivery_mode <> 'manual-postpone' or guard_action is null));
@@ -0,0 +1,283 @@
1
+ -- Apply after device-targeting-activation. Safe to reapply.
2
+ begin;
3
+ select public.lock_ota_distribution_control();
4
+
5
+ -- Deletion may need multiple requests to remove remote objects. A release
6
+ -- being removed remains available to retry cleanup, but can no longer serve.
7
+ alter table public.ota_updates add column if not exists deletion_pending boolean not null default false;
8
+ alter table public.ota_updates drop constraint if exists ota_deleting_update_inactive;
9
+ alter table public.ota_updates add constraint ota_deleting_update_inactive check (not deletion_pending or not is_active);
10
+
11
+ -- Keep the identity of a deleted artifact: deleting the last release must not
12
+ -- make its predecessor look like the release that an operator just stopped.
13
+ create table if not exists public.ota_public_distribution_events (
14
+ id bigint generated always as identity primary key,
15
+ runtime_version text not null,
16
+ channel text not null,
17
+ platform text not null,
18
+ update_id uuid not null,
19
+ event text not null check (event in ('baseline', 'activated', 'stopped')),
20
+ recorded_at timestamptz not null default clock_timestamp()
21
+ );
22
+ create index if not exists idx_public_events_scope
23
+ on public.ota_public_distribution_events(runtime_version, channel, platform, id desc);
24
+ alter table public.ota_public_distribution_events enable row level security;
25
+ revoke all on public.ota_public_distribution_events from public;
26
+
27
+ -- Old widening semantics could leave multiple public rows active. Retain only
28
+ -- the one the anonymous manifest selector actually preferred. Existing rows
29
+ -- with no serving pointer could never serve an anonymous client.
30
+ with ranked as (
31
+ select u.update_id, c.latest_update_id is not null or c.active_update_id is not null as has_pointer,
32
+ row_number() over (partition by u.runtime_version, u.channel, u.platform order by
33
+ (u.update_id = c.active_update_id) desc nulls last,
34
+ (u.update_id = c.latest_update_id) desc nulls last, u.created_at desc, u.update_id desc) as n
35
+ from public.ota_updates u
36
+ left join public.ota_update_channels c using (runtime_version, channel, platform)
37
+ where u.is_active and u.audience_mode = 'all'
38
+ )
39
+ update public.ota_updates u set is_active=false, disabled_at=clock_timestamp(), updated_at=clock_timestamp()
40
+ from ranked r where r.update_id=u.update_id and (r.n>1 or not r.has_pointer);
41
+ -- A baseline observes the live public artifact. Publication timestamps, upload
42
+ -- order and device manifests cannot establish when or to whom older builds were activated.
43
+ insert into public.ota_public_distribution_events(runtime_version,channel,platform,update_id,event)
44
+ select u.runtime_version,u.channel,u.platform,u.update_id,'baseline'
45
+ from public.ota_updates u where u.is_active and u.audience_mode='all' and not exists (
46
+ select 1 from public.ota_public_distribution_events e
47
+ where (e.runtime_version,e.channel,e.platform)=(u.runtime_version,u.channel,u.platform)
48
+ );
49
+ create unique index if not exists idx_ota_one_public_release
50
+ on public.ota_updates(runtime_version,channel,platform) where is_active and audience_mode='all';
51
+
52
+ create or replace function public.ota_activate_audience()
53
+ returns trigger language plpgsql security definer set search_path=public,pg_temp as $$
54
+ begin
55
+ if new.is_active and new.audience_mode='all'
56
+ and not (old.is_active and old.audience_mode='all') then
57
+ perform public.assert_ota_distribution_mutation_allowed('starting public distribution');
58
+ update public.ota_updates set is_active=false, disabled_at=clock_timestamp(), updated_at=clock_timestamp()
59
+ where (runtime_version,channel,platform)=(new.runtime_version,new.channel,new.platform)
60
+ and update_id<>new.update_id and is_active and audience_mode='all';
61
+ end if;
62
+ return new;
63
+ end $$;
64
+ drop trigger if exists trg_ota_updates_activate_audience on public.ota_updates;
65
+ create trigger trg_ota_updates_activate_audience before update of is_active,audience_mode on public.ota_updates
66
+ for each row execute function public.ota_activate_audience();
67
+ drop trigger if exists trg_ota_history_serialize_audience on public.ota_updates;
68
+ create trigger trg_ota_history_serialize_audience before update of audience_mode or delete on public.ota_updates
69
+ for each statement execute function public.serialize_ota_distribution_statement();
70
+
71
+ -- Every upload is a draft. A legacy publisher setting true must still complete
72
+ -- as a draft and must not retire a live release or lock the draft policy.
73
+ create or replace function public.ota_insert_draft()
74
+ returns trigger language plpgsql set search_path=public,pg_temp as $$
75
+ begin
76
+ new.is_active:=false;
77
+ new.policy_published_at:=null;
78
+ return new;
79
+ end $$;
80
+ drop trigger if exists trg_00_ota_insert_draft on public.ota_updates;
81
+ create trigger trg_00_ota_insert_draft before insert on public.ota_updates
82
+ for each row execute function public.ota_insert_draft();
83
+
84
+ create or replace function public.ota_record_public_transition()
85
+ returns trigger language plpgsql security definer set search_path=public,pg_temp as $$
86
+ declare was_public boolean := old.is_active and old.audience_mode='all';
87
+ now_public boolean := false;
88
+ begin
89
+ if tg_op <> 'DELETE' then
90
+ now_public:=new.is_active and new.audience_mode='all';
91
+ -- The writer creates an empty channel. Explicit SQL activation of its first
92
+ -- draft also needs a pointer so that an intentional stop can stay fail-closed.
93
+ if new.is_active and not old.is_active then
94
+ update public.ota_update_channels set latest_update_id=new.update_id,latest_created_at=new.created_at,
95
+ latest_created_at_path=new.created_at_path
96
+ where (runtime_version,channel,platform)=(new.runtime_version,new.channel,new.platform)
97
+ and latest_update_id is null and active_update_id is null;
98
+ end if;
99
+ end if;
100
+ if was_public is distinct from now_public then
101
+ insert into public.ota_public_distribution_events(runtime_version,channel,platform,update_id,event)
102
+ values(old.runtime_version,old.channel,old.platform,old.update_id,
103
+ case when now_public then 'activated' else 'stopped' end);
104
+ end if;
105
+ return null;
106
+ end $$;
107
+ revoke all on function public.ota_record_public_transition() from public;
108
+ drop trigger if exists trg_ota_record_public_transition on public.ota_updates;
109
+ create trigger trg_ota_record_public_transition after update of is_active,audience_mode or delete on public.ota_updates
110
+ for each row execute function public.ota_record_public_transition();
111
+
112
+ -- Pick the previous *distinct activation*, even if the last release has been
113
+ -- stopped or deleted. Never restore a release that is currently device-only.
114
+ create or replace view public.ota_distribution_history as
115
+ select c.id,c.runtime_version,c.channel,c.platform,
116
+ coalesce(latest_event.id,0)::text as revision,
117
+ case when curr.is_active then curr.release end as current_release,
118
+ prev.release as previous_release
119
+ from public.ota_update_channels c
120
+ left join lateral (
121
+ select e.id from public.ota_public_distribution_events e
122
+ where (e.runtime_version,e.channel,e.platform)=(c.runtime_version,c.channel,c.platform)
123
+ order by e.id desc limit 1
124
+ ) latest_event on true
125
+ left join lateral (
126
+ select e.update_id from public.ota_public_distribution_events e
127
+ where (e.runtime_version,e.channel,e.platform)=(c.runtime_version,c.channel,c.platform)
128
+ and e.event <> 'stopped' order by e.id desc limit 1
129
+ ) last_activation on true
130
+ left join lateral (
131
+ select u.is_active, jsonb_build_object('updateId',u.update_id,'comment',u.comment,'createdAt',u.created_at,
132
+ 'appVersion',coalesce(nullif(btrim(u.manifest#>>'{extra,expoClient,version}'),''),
133
+ nullif(btrim(u.manifest#>>'{expoClient,version}'),'')),
134
+ 'activatedAt',case when e.event='activated' then e.recorded_at end) as release
135
+ from public.ota_updates u join public.ota_public_distribution_events e using(update_id,runtime_version,channel,platform)
136
+ where u.update_id=last_activation.update_id and u.audience_mode='all' and e.event<>'stopped'
137
+ order by e.id desc limit 1
138
+ ) curr on true
139
+ left join lateral (
140
+ select jsonb_build_object('updateId',u.update_id,'comment',u.comment,'createdAt',u.created_at,
141
+ 'appVersion',coalesce(nullif(btrim(u.manifest#>>'{extra,expoClient,version}'),''),
142
+ nullif(btrim(u.manifest#>>'{expoClient,version}'),'')),
143
+ 'activatedAt',case when e.event='activated' then e.recorded_at end) as release
144
+ from public.ota_public_distribution_events e join public.ota_updates u using(update_id,runtime_version,channel,platform)
145
+ where (e.runtime_version,e.channel,e.platform)=(c.runtime_version,c.channel,c.platform)
146
+ and e.event<>'stopped' and u.update_id<>last_activation.update_id and u.audience_mode='all' and not u.is_active and not u.deletion_pending
147
+ order by e.id desc limit 1
148
+ ) prev on true;
149
+ revoke all on public.ota_distribution_history from public;
150
+
151
+ -- The switch, validation, activation, public history and serving pointers use
152
+ -- one transaction and the same lock as audience edits and publisher INSERTs.
153
+ create or replace function public.ota_change_distribution(p_update uuid,p_action text,p_revision text default null)
154
+ returns table(result jsonb) language plpgsql security definer set search_path=public,pg_temp as $$
155
+ declare
156
+ u public.ota_updates%rowtype;
157
+ c public.ota_update_channels%rowtype;
158
+ newest public.ota_updates%rowtype;
159
+ serving public.ota_updates%rowtype;
160
+ latest public.ota_updates%rowtype;
161
+ pinned uuid;
162
+ history public.ota_distribution_history%rowtype;
163
+ changed_at timestamptz;
164
+ begin
165
+ perform public.lock_ota_distribution_control();
166
+ if p_action is null or p_action not in ('activate','stop','restore','rollback','promote','select-latest','retire','delete') then
167
+ raise exception 'Invalid distribution action';
168
+ end if;
169
+ if p_action not in ('stop','retire','delete') then
170
+ perform public.assert_ota_distribution_mutation_allowed(p_action);
171
+ end if;
172
+ select * into u from public.ota_updates where update_id=p_update for update;
173
+ if not found then raise exception 'DISTRIBUTION_CONFLICT: Release no longer exists'; end if;
174
+ if u.deletion_pending and p_action not in ('stop','retire','delete') then
175
+ raise exception 'DISTRIBUTION_CONFLICT: This release is being deleted';
176
+ end if;
177
+ insert into public.ota_update_channels(runtime_version,channel,platform)
178
+ values(u.runtime_version,u.channel,u.platform) on conflict(runtime_version,channel,platform) do nothing;
179
+ select * into strict c from public.ota_update_channels
180
+ where (runtime_version,channel,platform)=(u.runtime_version,u.channel,u.platform) for update;
181
+ select * into newest from public.ota_updates
182
+ where (runtime_version,channel,platform)=(u.runtime_version,u.channel,u.platform)
183
+ order by created_at desc,update_id desc limit 1;
184
+ pinned:=c.active_update_id;
185
+ if pinned=c.latest_update_id then pinned:=null; end if;
186
+ if p_action='restore' then
187
+ select * into history from public.ota_distribution_history where id=c.id;
188
+ if p_revision is distinct from history.revision or
189
+ p_update is distinct from (history.previous_release->>'updateId')::uuid then
190
+ raise exception 'DISTRIBUTION_CONFLICT: Previous release changed. Refresh and try again.';
191
+ end if;
192
+ elsif p_action='activate' then
193
+ if p_update<>newest.update_id or pinned is not null then
194
+ raise exception 'DISTRIBUTION_CONFLICT: Direct activation requires the newest release and no active rollback';
195
+ end if;
196
+ elsif p_action='promote' then
197
+ if pinned is null or p_update is distinct from c.latest_update_id then
198
+ raise exception 'DISTRIBUTION_CONFLICT: Promote requires the latest release and an active rollback';
199
+ end if;
200
+ elsif p_action='rollback' then
201
+ select * into serving from public.ota_updates
202
+ where (runtime_version,channel,platform)=(u.runtime_version,u.channel,u.platform) and is_active
203
+ and (u.audience_mode='devices' or audience_mode='all')
204
+ order by (update_id=pinned) desc nulls last, (update_id=c.latest_update_id) desc nulls last,
205
+ created_at desc,update_id desc limit 1;
206
+ if serving.update_id is null or serving.created_at<=u.created_at then
207
+ raise exception 'DISTRIBUTION_CONFLICT: Rollback requires a newer active release in this scope';
208
+ end if;
209
+ elsif p_action='select-latest' then
210
+ if not u.is_active or pinned is not null then
211
+ raise exception 'DISTRIBUTION_CONFLICT: Latest must be active and rollback must be off';
212
+ end if;
213
+ end if;
214
+ changed_at:=clock_timestamp();
215
+ if p_action in ('stop','retire','delete') then
216
+ if u.is_active or p_action='retire' then
217
+ update public.ota_updates set is_active=false,disabled_at=coalesce(disabled_at,changed_at),updated_at=changed_at,
218
+ deletion_pending=deletion_pending or p_action='retire' where update_id=p_update;
219
+ end if;
220
+ if c.active_update_id=p_update or c.latest_update_id=p_update then
221
+ select * into latest from public.ota_updates
222
+ where (runtime_version,channel,platform)=(u.runtime_version,u.channel,u.platform) and is_active
223
+ order by created_at desc,update_id desc limit 1;
224
+ if pinned=p_update or pinned=latest.update_id then pinned:=null; end if;
225
+ update public.ota_update_channels set latest_update_id=latest.update_id,latest_created_at=latest.created_at,
226
+ latest_created_at_path=latest.created_at_path,active_update_id=pinned,active_changed_at=changed_at
227
+ where id=c.id returning * into c;
228
+ end if;
229
+ if p_action='delete' then delete from public.ota_updates where update_id=p_update; end if;
230
+ else
231
+ if not u.is_active then
232
+ update public.ota_updates set is_active=true,disabled_at=null,updated_at=changed_at,
233
+ rolled_back_from_update_id=case when p_action='rollback' then serving.update_id else rolled_back_from_update_id end
234
+ where update_id=p_update returning * into u;
235
+ end if;
236
+ latest:=u;
237
+ pinned:=null;
238
+ if p_action='rollback' then
239
+ latest:=newest; pinned:=p_update;
240
+ elsif p_action='restore' then
241
+ -- A newer active smoke stays selected for its allowed identities. The
242
+ -- anonymous eligibility lookup resolves the manually restored public row.
243
+ select * into latest from public.ota_updates
244
+ where (runtime_version,channel,platform)=(u.runtime_version,u.channel,u.platform) and is_active
245
+ order by created_at desc,update_id desc limit 1;
246
+ end if;
247
+ update public.ota_update_channels set latest_update_id=latest.update_id,latest_created_at=latest.created_at,
248
+ latest_created_at_path=latest.created_at_path,active_update_id=pinned,active_changed_at=changed_at
249
+ where id=c.id and (latest_update_id,active_update_id) is distinct from (latest.update_id,pinned)
250
+ returning * into c;
251
+ if not found then
252
+ select * into c from public.ota_update_channels where (runtime_version,channel,platform)=(u.runtime_version,u.channel,u.platform);
253
+ end if;
254
+ end if;
255
+ return query select jsonb_build_object('updated',true,'disabled',p_action in ('stop','retire','delete'),
256
+ 'state',case when p_action in ('stop','retire','delete') then 'disabled' else 'active' end,
257
+ 'fallbackApplied',p_action in ('stop','retire','delete') and u.audience_mode='devices' and c.latest_update_id is not null,
258
+ 'activeUpdateId',c.active_update_id,'latestUpdateId',c.latest_update_id,'activeChangedAt',c.active_changed_at,
259
+ 'rollbackActive',c.active_update_id is not null and c.active_update_id<>c.latest_update_id,
260
+ 'rollbackAssigned',p_action='rollback','promoted',p_action='promote',
261
+ 'id',u.update_id,'updateId',u.update_id,'buildId',u.build_id,'runtimeVersion',u.runtime_version,
262
+ 'platform',u.platform,'channel',u.channel,'createdAt',u.created_at,'createdAtPath',u.created_at_path,
263
+ 'updatedAt',u.updated_at,'isActive',p_action not in ('stop','retire','delete'),'reason',p_action);
264
+ end $$;
265
+ revoke all on function public.ota_change_distribution(uuid,text,text) from public;
266
+ do $$ begin
267
+ if exists(select from pg_roles where rolname='anon') then
268
+ revoke all on public.ota_distribution_history, public.ota_public_distribution_events from anon;
269
+ revoke all on function public.ota_change_distribution(uuid,text,text) from anon;
270
+ end if;
271
+ if exists(select from pg_roles where rolname='authenticated') then
272
+ revoke all on public.ota_distribution_history, public.ota_public_distribution_events from authenticated;
273
+ revoke all on function public.ota_change_distribution(uuid,text,text) from authenticated;
274
+ end if;
275
+ if exists(select from pg_roles where rolname='service_role') then
276
+ -- The invoker-security audience RPC checks the global stop before the
277
+ -- trigger promotes a live smoke to public distribution.
278
+ grant execute on function public.assert_ota_distribution_mutation_allowed(text) to service_role;
279
+ grant select on public.ota_distribution_history to service_role;
280
+ grant execute on function public.ota_change_distribution(uuid,text,text) to service_role;
281
+ end if;
282
+ end $$;
283
+ commit;