hazo_blog 2.0.0 → 2.16.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 (165) hide show
  1. package/CHANGE_LOG.md +867 -0
  2. package/README.md +1 -0
  3. package/db_setup_postgres.sql +652 -0
  4. package/db_setup_sqlite.sql +341 -0
  5. package/dist/index.d.ts +24 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +24 -0
  8. package/dist/jobs/maintenance-jobs.d.ts +83 -0
  9. package/dist/jobs/maintenance-jobs.d.ts.map +1 -0
  10. package/dist/jobs/maintenance-jobs.js +123 -0
  11. package/dist/lib/domain-dns.d.ts +15 -0
  12. package/dist/lib/domain-dns.d.ts.map +1 -0
  13. package/dist/lib/domain-dns.js +27 -0
  14. package/dist/lib/domain-token.d.ts +23 -0
  15. package/dist/lib/domain-token.d.ts.map +1 -0
  16. package/dist/lib/domain-token.js +42 -0
  17. package/dist/lib/hook-bus.d.ts +165 -0
  18. package/dist/lib/hook-bus.d.ts.map +1 -0
  19. package/dist/lib/hook-bus.js +90 -0
  20. package/dist/lib/index.d.ts +8 -0
  21. package/dist/lib/index.d.ts.map +1 -1
  22. package/dist/lib/index.js +8 -0
  23. package/dist/lib/revisions.d.ts +10 -0
  24. package/dist/lib/revisions.d.ts.map +1 -0
  25. package/dist/lib/revisions.js +18 -0
  26. package/dist/lib/safe-url.d.ts +8 -0
  27. package/dist/lib/safe-url.d.ts.map +1 -0
  28. package/dist/lib/safe-url.js +32 -0
  29. package/dist/lib/sections.d.ts +59 -0
  30. package/dist/lib/sections.d.ts.map +1 -0
  31. package/dist/lib/sections.js +303 -0
  32. package/dist/lib/site-identity.d.ts +45 -0
  33. package/dist/lib/site-identity.d.ts.map +1 -0
  34. package/dist/lib/site-identity.js +116 -0
  35. package/dist/lib/status.d.ts +25 -0
  36. package/dist/lib/status.d.ts.map +1 -0
  37. package/dist/lib/status.js +58 -0
  38. package/dist/lib/theme-schema.d.ts +51 -0
  39. package/dist/lib/theme-schema.d.ts.map +1 -0
  40. package/dist/lib/theme-schema.js +112 -0
  41. package/dist/lib/theme-validate.d.ts +48 -0
  42. package/dist/lib/theme-validate.d.ts.map +1 -0
  43. package/dist/lib/theme-validate.js +149 -0
  44. package/dist/next/byline-routes.d.ts +67 -0
  45. package/dist/next/byline-routes.d.ts.map +1 -0
  46. package/dist/next/byline-routes.js +235 -0
  47. package/dist/next/domain-routes.d.ts +9 -0
  48. package/dist/next/domain-routes.d.ts.map +1 -0
  49. package/dist/next/domain-routes.js +39 -0
  50. package/dist/next/post-routes.d.ts +100 -0
  51. package/dist/next/post-routes.d.ts.map +1 -0
  52. package/dist/next/post-routes.js +291 -0
  53. package/dist/render/engine.d.ts +41 -0
  54. package/dist/render/engine.d.ts.map +1 -0
  55. package/dist/render/engine.js +91 -0
  56. package/dist/render/fallback-theme/templates/404.liquid +1 -0
  57. package/dist/render/fallback-theme/templates/default.liquid +1 -0
  58. package/dist/render/fallback-theme.d.ts +3 -0
  59. package/dist/render/fallback-theme.d.ts.map +1 -0
  60. package/dist/render/fallback-theme.js +34 -0
  61. package/dist/render/html.d.ts +10 -0
  62. package/dist/render/html.d.ts.map +1 -0
  63. package/dist/render/html.js +29 -0
  64. package/dist/render/index.d.ts +13 -0
  65. package/dist/render/index.d.ts.map +1 -0
  66. package/dist/render/index.js +23 -0
  67. package/dist/render/resolve.d.ts +23 -0
  68. package/dist/render/resolve.d.ts.map +1 -0
  69. package/dist/render/resolve.js +32 -0
  70. package/dist/render/tags/faq.d.ts +9 -0
  71. package/dist/render/tags/faq.d.ts.map +1 -0
  72. package/dist/render/tags/faq.js +31 -0
  73. package/dist/render/tags/image.d.ts +12 -0
  74. package/dist/render/tags/image.d.ts.map +1 -0
  75. package/dist/render/tags/image.js +56 -0
  76. package/dist/render/tags/index.d.ts +3 -0
  77. package/dist/render/tags/index.d.ts.map +1 -0
  78. package/dist/render/tags/index.js +16 -0
  79. package/dist/render/tags/menu.d.ts +11 -0
  80. package/dist/render/tags/menu.d.ts.map +1 -0
  81. package/dist/render/tags/menu.js +74 -0
  82. package/dist/render/tags/related-posts.d.ts +11 -0
  83. package/dist/render/tags/related-posts.d.ts.map +1 -0
  84. package/dist/render/tags/related-posts.js +34 -0
  85. package/dist/render/tags/section-list.d.ts +9 -0
  86. package/dist/render/tags/section-list.d.ts.map +1 -0
  87. package/dist/render/tags/section-list.js +71 -0
  88. package/dist/render/tags/seo-head.d.ts +16 -0
  89. package/dist/render/tags/seo-head.d.ts.map +1 -0
  90. package/dist/render/tags/seo-head.js +73 -0
  91. package/dist/render/tags/toc.d.ts +15 -0
  92. package/dist/render/tags/toc.d.ts.map +1 -0
  93. package/dist/render/tags/toc.js +72 -0
  94. package/dist/render/types.d.ts +167 -0
  95. package/dist/render/types.d.ts.map +1 -0
  96. package/dist/render/types.js +13 -0
  97. package/dist/repository/bylines.d.ts +56 -0
  98. package/dist/repository/bylines.d.ts.map +1 -0
  99. package/dist/repository/bylines.js +284 -0
  100. package/dist/repository/domains.d.ts +67 -0
  101. package/dist/repository/domains.d.ts.map +1 -0
  102. package/dist/repository/domains.js +173 -0
  103. package/dist/repository/media.d.ts +45 -0
  104. package/dist/repository/media.d.ts.map +1 -0
  105. package/dist/repository/media.js +248 -0
  106. package/dist/repository/menus.d.ts +58 -0
  107. package/dist/repository/menus.d.ts.map +1 -0
  108. package/dist/repository/menus.js +311 -0
  109. package/dist/repository/posts.d.ts +171 -0
  110. package/dist/repository/posts.d.ts.map +1 -0
  111. package/dist/repository/posts.js +791 -0
  112. package/dist/repository/site-files.d.ts +15 -0
  113. package/dist/repository/site-files.d.ts.map +1 -0
  114. package/dist/repository/site-files.js +73 -0
  115. package/dist/repository/tags.d.ts +51 -0
  116. package/dist/repository/tags.d.ts.map +1 -0
  117. package/dist/repository/tags.js +282 -0
  118. package/dist/repository/themes.d.ts +45 -0
  119. package/dist/repository/themes.d.ts.map +1 -0
  120. package/dist/repository/themes.js +285 -0
  121. package/dist/seo/hazo-seo-sitemap-source.d.ts +63 -0
  122. package/dist/seo/hazo-seo-sitemap-source.d.ts.map +1 -0
  123. package/dist/seo/hazo-seo-sitemap-source.js +85 -0
  124. package/dist/seo/index.d.ts +1 -0
  125. package/dist/seo/index.d.ts.map +1 -1
  126. package/dist/seo/index.js +6 -0
  127. package/dist/service/bylines.d.ts +21 -0
  128. package/dist/service/bylines.d.ts.map +1 -0
  129. package/dist/service/bylines.js +48 -0
  130. package/dist/service/cdn-hooks.d.ts +20 -0
  131. package/dist/service/cdn-hooks.d.ts.map +1 -0
  132. package/dist/service/cdn-hooks.js +38 -0
  133. package/dist/service/domains.d.ts +49 -0
  134. package/dist/service/domains.d.ts.map +1 -0
  135. package/dist/service/domains.js +98 -0
  136. package/dist/service/media.d.ts +79 -0
  137. package/dist/service/media.d.ts.map +1 -0
  138. package/dist/service/media.js +225 -0
  139. package/dist/service/menus.d.ts +43 -0
  140. package/dist/service/menus.d.ts.map +1 -0
  141. package/dist/service/menus.js +165 -0
  142. package/dist/service/posts.d.ts +70 -0
  143. package/dist/service/posts.d.ts.map +1 -0
  144. package/dist/service/posts.js +291 -0
  145. package/dist/service/seo-hooks.d.ts +18 -0
  146. package/dist/service/seo-hooks.d.ts.map +1 -0
  147. package/dist/service/seo-hooks.js +108 -0
  148. package/dist/service/site-files.d.ts +20 -0
  149. package/dist/service/site-files.d.ts.map +1 -0
  150. package/dist/service/site-files.js +71 -0
  151. package/dist/service/tags.d.ts +20 -0
  152. package/dist/service/tags.d.ts.map +1 -0
  153. package/dist/service/tags.js +41 -0
  154. package/dist/service/themes.d.ts +85 -0
  155. package/dist/service/themes.d.ts.map +1 -0
  156. package/dist/service/themes.js +416 -0
  157. package/dist/types/index.d.ts +953 -0
  158. package/dist/types/index.d.ts.map +1 -1
  159. package/dist/ui/content-status-badge.d.ts +7 -0
  160. package/dist/ui/content-status-badge.d.ts.map +1 -0
  161. package/dist/ui/content-status-badge.js +16 -0
  162. package/dist/ui/index.d.ts +1 -0
  163. package/dist/ui/index.d.ts.map +1 -1
  164. package/dist/ui/index.js +1 -0
  165. package/package.json +37 -26
@@ -72,6 +72,13 @@ CREATE TABLE IF NOT EXISTS hazo_blog_posts (
72
72
  _rev INTEGER NOT NULL DEFAULT 1,
73
73
  live_revision_id UUID,
74
74
  draft_revision_id UUID,
75
+ -- Content lifecycle stage (Phase 1a issue 07): draft/in_review/queued/
76
+ -- scheduled/published/trashed. Defaults to 'draft' so pre-2.2.0 rows
77
+ -- (backfilled by the ALTER below) don't break. Validated transitions live
78
+ -- in the app layer (src/lib/status.ts's isValidStatusTransition), not a
79
+ -- DB CHECK constraint, matching this package's existing convention of
80
+ -- enum-shaped columns with no DB-level enum enforcement.
81
+ status TEXT NOT NULL DEFAULT 'draft',
75
82
  deleted_at TIMESTAMPTZ,
76
83
  template TEXT,
77
84
  locale TEXT DEFAULT NULL, -- BCP-47 locale; NULL = default locale
@@ -81,20 +88,53 @@ CREATE TABLE IF NOT EXISTS hazo_blog_posts (
81
88
  og_image TEXT,
82
89
  noindex BOOLEAN NOT NULL DEFAULT FALSE,
83
90
  canonical_override TEXT,
91
+ -- Loose reference to hazo_blog_categories (Phase 1a issue 12) — "primary
92
+ -- category", singular. No FK (same convention as every other
93
+ -- optional/legacy cross-table reference in this package); see
94
+ -- Post.category_id's doc-comment (src/types/index.ts) for why this
95
+ -- deliberately reuses the SAME legacy table the v1.6 BlogPost.category_id
96
+ -- pointed at rather than a new v2 category system.
97
+ category_id TEXT,
98
+ -- Page-layout section list (ticket 05/Phase 1b issue 05) — JSON array of
99
+ -- `{type, version, key, settings, hidden}`, rendered in order by
100
+ -- `{% section_list %}`. See Post.sections_json's doc-comment
101
+ -- (src/types/index.ts) for why this lives on the post row, not a
102
+ -- revision.
103
+ sections_json JSONB NOT NULL DEFAULT '[]'::jsonb,
84
104
  search_vector tsvector,
85
105
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
86
106
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
87
107
  published_at TIMESTAMPTZ,
88
108
  publish_at TIMESTAMPTZ
89
109
  );
110
+ -- Phase 1a issue 07: add `status` to a pre-2.2.0 hazo_blog_posts table.
111
+ -- NOT NULL + DEFAULT backfills existing rows to 'draft' in the same
112
+ -- statement — safe to always include (idempotent), same pattern as the
113
+ -- `locale` columns added to hazo_blog_categories/hazo_blog_tags above.
114
+ ALTER TABLE hazo_blog_posts ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'draft';
115
+ -- Phase 1a issue 12: add `category_id` to a pre-2.7.0 hazo_blog_posts
116
+ -- table. Nullable (no backfill needed — NULL means "uncategorized", the
117
+ -- correct default for every pre-existing row) and idempotent, same pattern
118
+ -- as `status` above.
119
+ ALTER TABLE hazo_blog_posts ADD COLUMN IF NOT EXISTS category_id TEXT;
120
+ -- ⚠️ DDL required (v2.14.0) — add `sections_json` to a pre-2.14.0
121
+ -- hazo_blog_posts table. NOT NULL + DEFAULT backfills existing rows to
122
+ -- `[]` in the same statement — idempotent, same pattern as `status`/
123
+ -- `category_id` above.
124
+ ALTER TABLE hazo_blog_posts ADD COLUMN IF NOT EXISTS sections_json JSONB NOT NULL DEFAULT '[]'::jsonb;
125
+
90
126
  CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_posts_scope_slug_locale
91
127
  ON hazo_blog_posts (scope_id, slug, COALESCE(locale, ''));
92
128
  CREATE INDEX IF NOT EXISTS idx_hazo_blog_posts_scope_type
93
129
  ON hazo_blog_posts (scope_id, type);
130
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_posts_status
131
+ ON hazo_blog_posts (status);
94
132
  CREATE INDEX IF NOT EXISTS idx_hazo_blog_posts_deleted_at
95
133
  ON hazo_blog_posts (deleted_at);
96
134
  CREATE INDEX IF NOT EXISTS idx_hazo_blog_posts_publish_at
97
135
  ON hazo_blog_posts (publish_at);
136
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_posts_category_id
137
+ ON hazo_blog_posts (category_id);
98
138
  CREATE INDEX IF NOT EXISTS idx_hazo_blog_posts_search
99
139
  ON hazo_blog_posts USING GIN (search_vector);
100
140
 
@@ -129,6 +169,114 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_post_tags_unique
129
169
  ON hazo_blog_post_tags (post_id, tag_id);
130
170
  CREATE INDEX IF NOT EXISTS idx_hazo_blog_post_tags_tag ON hazo_blog_post_tags (tag_id);
131
171
 
172
+ -- hazo_blog_bylines (v2, Phase 1a issue 08) — named author attribution,
173
+ -- independent of hazo_auth login accounts: real users, guests, and imported
174
+ -- WordPress authors all get a row here. scope_id is NOT NULL, same rule as
175
+ -- hazo_blog_posts — every byline belongs to exactly one tenant and is never
176
+ -- queryable cross-tenant (see src/repository/bylines.ts).
177
+ --
178
+ -- user_id is a loose, nullable reference to a hazo_auth user id — NOT a DB
179
+ -- FK: hazo_auth is an OPTIONAL peer of this package (see package.json's
180
+ -- peerDependenciesMeta), so this table can't own a REFERENCES clause into a
181
+ -- users table that may not exist. Validated at the application layer
182
+ -- if/when hazo_auth is present (see resolvePostScope's optional_import
183
+ -- convention in src/service/posts.ts).
184
+ CREATE TABLE IF NOT EXISTS hazo_blog_bylines (
185
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
186
+ scope_id TEXT NOT NULL,
187
+ name TEXT NOT NULL,
188
+ slug TEXT NOT NULL,
189
+ bio TEXT,
190
+ avatar_url TEXT,
191
+ user_id TEXT,
192
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
193
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
194
+ );
195
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_bylines_scope_slug
196
+ ON hazo_blog_bylines (scope_id, slug);
197
+
198
+ -- hazo_blog_post_bylines — post↔byline join, ordered by `position` for
199
+ -- display (e.g. "By A, B & C"). Surrogate `id` PK, not composite
200
+ -- `(post_id, byline_id)` — same reasoning as hazo_blog_post_tags above:
201
+ -- hazo_connect's CrudService only deletes by a single PK, so a composite key
202
+ -- wouldn't be deletable through the documented API.
203
+ CREATE TABLE IF NOT EXISTS hazo_blog_post_bylines (
204
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
205
+ post_id UUID NOT NULL REFERENCES hazo_blog_posts(id) ON DELETE CASCADE,
206
+ byline_id UUID NOT NULL REFERENCES hazo_blog_bylines(id) ON DELETE CASCADE,
207
+ position INTEGER NOT NULL DEFAULT 0
208
+ );
209
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_post_bylines_unique
210
+ ON hazo_blog_post_bylines (post_id, byline_id);
211
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_post_bylines_byline
212
+ ON hazo_blog_post_bylines (byline_id);
213
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_post_bylines_post_position
214
+ ON hazo_blog_post_bylines (post_id, position);
215
+
216
+ -- hazo_blog_media (v2, Phase 1a issue 10) — one row per distinct uploaded
217
+ -- file per tenant, deduplicated by (scope_id, hash): the same bytes
218
+ -- uploaded twice to the SAME scope return the existing row; the same bytes
219
+ -- uploaded to a DIFFERENT scope get their own row (dedup is per-tenant, not
220
+ -- global). scope_id is NOT NULL, same rule as hazo_blog_posts/bylines.
221
+ --
222
+ -- variants maps a "{width}-{format}" key (e.g. "640-avif") to that
223
+ -- variant's storage_key; storage_key (this table's own column) is the SAME
224
+ -- value as one entry of variants — the "primary" variant (widest width,
225
+ -- AVIF) chosen to represent this media item at a glance. See
226
+ -- src/service/media.ts's pickPrimaryVariantKey.
227
+ CREATE TABLE IF NOT EXISTS hazo_blog_media (
228
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
229
+ scope_id TEXT NOT NULL,
230
+ filename TEXT NOT NULL,
231
+ mime_type TEXT NOT NULL,
232
+ size_bytes INTEGER NOT NULL,
233
+ width INTEGER NOT NULL,
234
+ height INTEGER NOT NULL,
235
+ hash TEXT NOT NULL,
236
+ blurhash TEXT NOT NULL,
237
+ dominant_colour TEXT NOT NULL,
238
+ focal_point JSONB,
239
+ alt TEXT,
240
+ caption TEXT,
241
+ tags JSONB NOT NULL DEFAULT '[]'::jsonb,
242
+ ai_data JSONB NOT NULL DEFAULT '{}'::jsonb,
243
+ variants JSONB NOT NULL DEFAULT '{}'::jsonb,
244
+ storage_key TEXT NOT NULL,
245
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
246
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
247
+ );
248
+ -- Dedup key: unique PER SCOPE, not globally — the same file uploaded to two
249
+ -- different tenants must not dedup across them.
250
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_media_scope_hash
251
+ ON hazo_blog_media (scope_id, hash);
252
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_media_scope_created
253
+ ON hazo_blog_media (scope_id, created_at);
254
+
255
+ -- hazo_blog_media_usage — which content field references a hazo_blog_media
256
+ -- row, maintained on every content save (src/service/posts.ts's
257
+ -- createPost/updatePost, via src/service/media.ts's
258
+ -- syncMediaUsageForEntity). Surrogate `id` PK, not composite
259
+ -- `(entity_type, entity_id, field)` — same reasoning as
260
+ -- hazo_blog_post_tags/hazo_blog_post_bylines above: hazo_connect's
261
+ -- CrudService only deletes by a single PK.
262
+ CREATE TABLE IF NOT EXISTS hazo_blog_media_usage (
263
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
264
+ media_id UUID NOT NULL REFERENCES hazo_blog_media(id) ON DELETE CASCADE,
265
+ entity_type TEXT NOT NULL,
266
+ entity_id TEXT NOT NULL,
267
+ field TEXT NOT NULL,
268
+ scope_id TEXT NOT NULL
269
+ );
270
+ -- One media reference per (entity_type, entity_id, field) at a time.
271
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_media_usage_unique
272
+ ON hazo_blog_media_usage (entity_type, entity_id, field);
273
+ -- "Where used" lookups, keyed from the content side.
274
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_media_usage_entity
275
+ ON hazo_blog_media_usage (entity_type, entity_id);
276
+ -- Reverse lookups, keyed from the media side (used/unused filter).
277
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_media_usage_media
278
+ ON hazo_blog_media_usage (media_id);
279
+
132
280
  -- package-owned display/behavior settings, e.g. which blog index layout is
133
281
  -- active — one row per (scope_id, setting_key)
134
282
  CREATE TABLE IF NOT EXISTS hazo_blog_settings (
@@ -141,3 +289,507 @@ CREATE TABLE IF NOT EXISTS hazo_blog_settings (
141
289
  );
142
290
  CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_settings_scope_key
143
291
  ON hazo_blog_settings (scope_id, setting_key);
292
+
293
+ -- hazo_blog_app_site_domains (Phase 1a issue 14) — custom domains that serve
294
+ -- a tenant's public site. scope_id is NOT NULL here (unlike most other
295
+ -- tables in this file) for the same reason as hazo_blog_posts/bylines/media:
296
+ -- every row must belong to exactly one concrete tenant, because this table
297
+ -- IS the security boundary for unauthenticated public host-based routing
298
+ -- (Host header -> domain row -> scope_id, with NO membership check at all —
299
+ -- see hazo_blog's own CLAUDE.md / the platform's lib/host-resolution.ts).
300
+ --
301
+ -- `domain` is unique GLOBALLY (across every scope), not per-scope — a
302
+ -- domain can only ever point at one tenant's content at a time (that's what
303
+ -- DNS + TLS already enforce out in the world); allowing the same domain
304
+ -- string under two different scope_ids would let a second tenant silently
305
+ -- shadow/hijack the first's domain in this table even though only one of
306
+ -- them could actually prove ownership via DNS. This is a deliberate
307
+ -- deviation from this file's usual `(scope_id, slug)`-style compound
308
+ -- uniqueness.
309
+ --
310
+ -- "Only one primary domain per scope" is enforced at the service layer
311
+ -- (src/service/domains.ts's setPrimaryDomain), not a DB constraint — same
312
+ -- convention as every other invariant in this schema (see hazo_blog_posts'
313
+ -- `status` column comment).
314
+ CREATE TABLE IF NOT EXISTS hazo_blog_app_site_domains (
315
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
316
+ scope_id TEXT NOT NULL,
317
+ domain TEXT NOT NULL,
318
+ verified BOOLEAN NOT NULL DEFAULT FALSE,
319
+ is_primary BOOLEAN NOT NULL DEFAULT FALSE,
320
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
321
+ );
322
+ -- Partial, not a plain unique index: only a VERIFIED row blocks another
323
+ -- tenant from registering the same domain string. An unverified claim is a
324
+ -- squat attempt, not ownership — allowing it to globally lock out the real
325
+ -- owner (who hasn't gotten around to adding/verifying it yet) would be a
326
+ -- denial-of-service via domain-claim squatting. Multiple unverified rows for
327
+ -- the same domain, across different scopes, are allowed to coexist; the
328
+ -- first one to pass DNS TXT verification wins and `verifyDomain` (see
329
+ -- ../src/service/domains.ts) deletes the losing unverified siblings.
330
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_app_site_domains_domain_verified
331
+ ON hazo_blog_app_site_domains (domain) WHERE verified;
332
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_app_site_domains_scope
333
+ ON hazo_blog_app_site_domains (scope_id);
334
+
335
+ -- hazo_blog_menus (Phase 1b issue 04) — named navigation menus (e.g. "main",
336
+ -- "footer"), rendered by the `{% menu "handle" %}` Liquid tag
337
+ -- (`hazo_blog/render`'s `registerPlatformTags`). `scope_id` is NOT NULL
338
+ -- here, unlike hazo_blog_categories/tags/settings above — a deliberate
339
+ -- deviation from this file's own "every other table carries a NULLABLE
340
+ -- scope_id" convention (see this file's header comment). Menus are
341
+ -- multi-tenant-platform-first, not an add-on-mode (GoTimer/kinstripe)
342
+ -- feature: there is no useful "global menu" concept the way a shared
343
+ -- legacy category/tag can be, and v1.6 add-on consumers (pinned below
344
+ -- 2.x) never pull this DDL at all, so there's no back-compat reason to
345
+ -- allow a NULL scope here the way hazo_blog_posts/bylines/media/
346
+ -- app_site_domains didn't either — same reasoning as those tables'
347
+ -- NOT NULL scope_id.
348
+ CREATE TABLE IF NOT EXISTS hazo_blog_menus (
349
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
350
+ scope_id TEXT NOT NULL,
351
+ name TEXT NOT NULL,
352
+ handle TEXT NOT NULL,
353
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
354
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
355
+ );
356
+ -- One menu per `(scope_id, handle)` — `{% menu "handle" %}` looks a menu up
357
+ -- by handle within a single tenant's scope, so a duplicate handle in the
358
+ -- same scope would make that lookup ambiguous.
359
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_menus_scope_handle
360
+ ON hazo_blog_menus (scope_id, handle);
361
+
362
+ -- hazo_blog_menu_items — one entry in a menu. Self-referencing `parent_id`
363
+ -- gives unlimited nesting depth (admin UI: indent/outdent; render: nested
364
+ -- `<ul>`). `ON DELETE CASCADE` on both `menu_id` and `parent_id` means
365
+ -- deleting a menu (or any item) removes its entire subtree in one
366
+ -- statement — same reasoning `hazo_blog_post_bylines`/`hazo_blog_media`
367
+ -- already rely on for their own cascades.
368
+ --
369
+ -- `item_type` distinguishes a REFERENCE item (`ref_type`/`ref_id` point at
370
+ -- a live post/page/category row; `label`/`url` are unused except `label`
371
+ -- as an optional override of the resolved title) from a CUSTOM item
372
+ -- (`label`/`url` are the item's own data; `ref_type`/`ref_id` are unused).
373
+ -- Enforced at the service/repository layer (`src/repository/menus.ts`),
374
+ -- not a DB CHECK constraint — same "invariant enforced in application
375
+ -- code, not SQL" convention as hazo_blog_posts' `status` column comment
376
+ -- and hazo_blog_app_site_domains' `is_primary` uniqueness.
377
+ CREATE TABLE IF NOT EXISTS hazo_blog_menu_items (
378
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
379
+ menu_id UUID NOT NULL REFERENCES hazo_blog_menus(id) ON DELETE CASCADE,
380
+ scope_id TEXT NOT NULL,
381
+ parent_id UUID REFERENCES hazo_blog_menu_items(id) ON DELETE CASCADE,
382
+ position INTEGER NOT NULL DEFAULT 0,
383
+ item_type TEXT NOT NULL DEFAULT 'custom', -- 'reference' | 'custom'
384
+ ref_type TEXT, -- 'post' | 'page' | 'category' — reference items only
385
+ ref_id TEXT, -- id of the referenced row — reference items only
386
+ label TEXT, -- item text; override of the resolved title for reference items, required for custom items
387
+ url TEXT, -- required for custom items; unused for reference items (resolved at render time)
388
+ css_class TEXT,
389
+ target TEXT, -- e.g. '_blank'
390
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
391
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
392
+ );
393
+ -- Drives both `listMenuItems` (ordered tree build) and `reorderMenuItems`.
394
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_menu_items_menu_parent_position
395
+ ON hazo_blog_menu_items (menu_id, parent_id, position);
396
+
397
+ -- hazo_blog_themes (Phase 1b issue 06) — a registered theme. `scope_id`
398
+ -- NULL = built-in/global (scanned from the `hazo_blog_themes` package at
399
+ -- boot, visible to every site); non-NULL = that site's own zip upload,
400
+ -- visible ONLY to it. No DB-level UNIQUE(scope_id, handle) here — Postgres
401
+ -- (and SQLite) treat every NULL as distinct for uniqueness purposes, so a
402
+ -- composite unique index including a nullable scope_id would NOT actually
403
+ -- prevent two different built-in themes from sharing a handle; that's
404
+ -- enforced in application code instead (`src/repository/themes.ts`'s
405
+ -- `findBuiltInByHandle` upsert-by-handle check), same "invariant enforced
406
+ -- in application code, not SQL" convention this file's own
407
+ -- hazo_blog_menu_items comment documents for `item_type`.
408
+ --
409
+ -- `version`/`storage_path` always mirror this theme's CURRENT (latest)
410
+ -- version; `hazo_blog_theme_versions` keeps every version ever installed,
411
+ -- including this one, for history.
412
+ CREATE TABLE IF NOT EXISTS hazo_blog_themes (
413
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
414
+ scope_id TEXT, -- NULL = built-in/global
415
+ name TEXT NOT NULL,
416
+ handle TEXT NOT NULL,
417
+ version TEXT NOT NULL,
418
+ author TEXT NOT NULL,
419
+ licence TEXT NOT NULL,
420
+ description TEXT,
421
+ screenshots JSONB NOT NULL DEFAULT '[]',
422
+ source TEXT NOT NULL, -- 'built-in' | 'upload'
423
+ storage_path TEXT NOT NULL, -- built-in: local fs dir; upload: R2 key prefix
424
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
425
+ );
426
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_themes_scope
427
+ ON hazo_blog_themes (scope_id);
428
+
429
+ -- hazo_blog_theme_versions — one row per version ever installed for a
430
+ -- theme (ticket's own "version history" requirement). `ON DELETE CASCADE`
431
+ -- so deleting a theme (`ThemeRepository.deleteTheme`, which also does this
432
+ -- explicitly itself for engines without enforced FK cascades) takes its
433
+ -- version history with it.
434
+ CREATE TABLE IF NOT EXISTS hazo_blog_theme_versions (
435
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
436
+ theme_id UUID NOT NULL REFERENCES hazo_blog_themes(id) ON DELETE CASCADE,
437
+ version TEXT NOT NULL,
438
+ storage_path TEXT NOT NULL,
439
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
440
+ );
441
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_theme_versions_theme
442
+ ON hazo_blog_theme_versions (theme_id, created_at);
443
+
444
+ -- hazo_blog_site_theme — the theme currently ACTIVE for a site. ONE row
445
+ -- per `scope_id`, enforced by the UNIQUE index below PLUS upsert semantics
446
+ -- in `ThemeRepository.setSiteTheme` (selects the existing row for this
447
+ -- scope and updates it in place, never inserts a second one) — there is
448
+ -- no separate "is_active" flag to go out of sync with: the row's mere
449
+ -- existence for a scope IS that scope's active theme.
450
+ CREATE TABLE IF NOT EXISTS hazo_blog_site_theme (
451
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
452
+ scope_id TEXT NOT NULL,
453
+ theme_id UUID NOT NULL REFERENCES hazo_blog_themes(id) ON DELETE CASCADE,
454
+ settings JSONB NOT NULL DEFAULT '{}', -- keyed by theme.json settings[].key
455
+ activated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
456
+ );
457
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_site_theme_scope
458
+ ON hazo_blog_site_theme (scope_id);
459
+
460
+ -- hazo_blog_site_files (Phase 1b issue 09) — versioned per-site files, e.g.
461
+ -- the Settings -> Appearance custom CSS textarea. `scope_id` NOT NULL, same
462
+ -- "platform-multi-tenant-first" reasoning as hazo_blog_menus/
463
+ -- hazo_blog_site_theme above — there's no useful "global" custom CSS.
464
+ --
465
+ -- VERSIONING: every save INSERTS a new row (never an UPDATE of `content`)
466
+ -- with `version` one higher than the previous current row for this
467
+ -- `(scope_id, kind)`, and flips the previous current row's `is_current` to
468
+ -- FALSE in the same `SiteFileRepository.setCurrentFile` call — full edit
469
+ -- history for free, at the cost of one extra row per (infrequent, admin-
470
+ -- only) save. "At most one current row per (scope_id, kind)" is enforced in
471
+ -- APPLICATION code (select-current, flip, insert-new), not a DB constraint
472
+ -- — same convention as hazo_blog_app_site_domains.is_primary/
473
+ -- hazo_blog_menu_items.item_type (see those tables' own comments).
474
+ CREATE TABLE IF NOT EXISTS hazo_blog_site_files (
475
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
476
+ scope_id TEXT NOT NULL,
477
+ kind TEXT NOT NULL, -- e.g. 'custom_css' — see SiteFileKind
478
+ content TEXT NOT NULL DEFAULT '',
479
+ version INTEGER NOT NULL DEFAULT 1,
480
+ is_current BOOLEAN NOT NULL DEFAULT TRUE,
481
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
482
+ );
483
+ -- Full version history is retrievable; one row per (scope_id, kind, version).
484
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_hazo_blog_site_files_scope_kind_version
485
+ ON hazo_blog_site_files (scope_id, kind, version);
486
+ -- The common lookup: "give me the current row for this site+kind".
487
+ CREATE INDEX IF NOT EXISTS idx_hazo_blog_site_files_current
488
+ ON hazo_blog_site_files (scope_id, kind, is_current);
489
+
490
+ -- ============================================================================
491
+ -- Row-Level Security (Phase 1a issue 17, platform's tenant-isolation suite).
492
+ -- Postgres-only — there is no SQLite equivalent of RLS, so this section has
493
+ -- no counterpart in db_setup_sqlite.sql and no attempt is made to create
494
+ -- one. This is the first Postgres-only DDL section in this package;
495
+ -- precedent for future Postgres-only features: put them in their own
496
+ -- clearly-headed section at the end of db_setup_postgres.sql like this one,
497
+ -- never silently diverge the two dialect files' table/column shapes.
498
+ --
499
+ -- WHY THIS EXISTS: the repository layer already filters every query by
500
+ -- scope_id (see `live()`/the generic scope helper in src/repository/
501
+ -- index.ts and every table-specific repository). RLS is DEFENCE IN DEPTH
502
+ -- on top of that — a backstop for the case where a future query forgets the
503
+ -- scope filter, not the primary tenant-isolation mechanism.
504
+ --
505
+ -- FAIL-SAFE DEFAULT: every policy below treats an UNSET
506
+ -- `app.current_scope_id` GUC as "deny all", not "allow all". This is the
507
+ -- safer of the two choices the ticket asked us to pick between: an app that
508
+ -- forgets to set the GUC for a request gets zero rows (a loud, visible
509
+ -- failure — 404s/empty lists) rather than every tenant's data. The
510
+ -- alternative (unset = allow all) would make RLS fail OPEN exactly when
511
+ -- something has already gone wrong upstream, defeating the point of a
512
+ -- backstop.
513
+ --
514
+ -- `hazo_blog_current_scope_id()` wraps `current_setting('app.current_scope_id',
515
+ -- true)` — the second (`true`) argument makes it return NULL instead of
516
+ -- raising when the GUC was never set in this session at all, which is most
517
+ -- of what lets the fail-safe-deny comparisons below work as plain SQL.
518
+ --
519
+ -- THE GENUINELY TRICKY PART, found by actually testing this against a real
520
+ -- Postgres rather than assuming: a custom GUC's "unset" value is NOT stable
521
+ -- across a pooled connection's lifetime. The FIRST time a session has never
522
+ -- touched `app.current_scope_id`, `current_setting(..., true)` correctly
523
+ -- returns SQL NULL. But once ANY transaction on that same physical
524
+ -- connection has done `SET LOCAL app.current_scope_id = '...'` and then
525
+ -- committed, Postgres resets the GUC back to '' (empty string) for the rest
526
+ -- of that session — NOT back to NULL — because the GUC now exists in the
527
+ -- session's custom-GUC table with its pre-transaction value, which was ''.
528
+ -- Confirmed by hand: `BEGIN; SET LOCAL app.foo = 'bar'; COMMIT; SELECT
529
+ -- current_setting('app.foo', true);` returns `''`, not NULL. Since this
530
+ -- app's Postgres connections are pooled and reused across requests (both
531
+ -- by `postgres.js`'s own pool and, in production, by pgBouncer), a later
532
+ -- request on a RECYCLED connection that forgets to set the GUC would see
533
+ -- `current_setting(...) = ''`, which is NOT NULL — so a naive `IS NOT NULL`
534
+ -- fail-safe check would wrongly treat that as "a scope was set" and leak
535
+ -- every NULL-scope_id (global) row on the nullable-scope_id tables. This
536
+ -- function collapses '' back to NULL with `NULLIF`, so "unset" means the
537
+ -- same thing (deny) whether this is a fresh connection or a recycled one
538
+ -- that simply forgot to set the GUC for the current request.
539
+ CREATE OR REPLACE FUNCTION hazo_blog_current_scope_id() RETURNS TEXT AS $$
540
+ SELECT NULLIF(current_setting('app.current_scope_id', true), '');
541
+ $$ LANGUAGE sql STABLE;
542
+ --
543
+ -- TABLE OWNERSHIP GOTCHA: `infra/provision.sh` creates the app's Postgres
544
+ -- role as the OWNER of the `blogsketch` database (`createdb -O
545
+ -- blogsketch_app`), and Postgres table owners BYPASS row security by
546
+ -- default even when RLS is enabled — `ENABLE ROW LEVEL SECURITY` alone is a
547
+ -- no-op for the app's own connection. Every table below also gets `FORCE
548
+ -- ROW LEVEL SECURITY`, which is what actually binds the table owner (and
549
+ -- therefore the app) to these policies too. Verified by hand against a
550
+ -- real local Postgres: without FORCE, the owning role reads every tenant's
551
+ -- rows regardless of the GUC; with FORCE, it's correctly scoped. (A true
552
+ -- Postgres superuser still always bypasses RLS no matter what — FORCE only
553
+ -- affects the table owner when the owner is a regular role, which is the
554
+ -- case here.)
555
+ --
556
+ -- SCOPE_ID NULLABILITY: tables where scope_id is NOT NULL
557
+ -- (hazo_blog_posts/revisions/bylines/media/media_usage/
558
+ -- app_site_domains) get strict `scope_id = current_setting(...)` policies.
559
+ -- Tables where scope_id is nullable (hazo_blog_categories/tags/settings)
560
+ -- follow this file's own documented dual-mode convention (see this file's
561
+ -- header comment): a NULL scope_id is the legacy/single-tenant/add-on-mode
562
+ -- row, visible across every tenant (same "global category/tag" rule
563
+ -- `assertCategoryVisible`/the tags repository's doc-comment already
564
+ -- enforce at the application layer — this mirrors that, it doesn't
565
+ -- invent a new rule). `hazo_blog_post_tags`/`hazo_blog_post_bylines` carry
566
+ -- no scope_id column of their own (join tables) — their policies reach
567
+ -- through `post_id` to `hazo_blog_posts.scope_id` instead.
568
+ --
569
+ -- CONSUMER COMPATIBILITY: this section assumes a consumer that always sets
570
+ -- `app.current_scope_id` per request (i.e. a genuinely multi-tenant
571
+ -- deployment, like this platform). A single-tenant/add-on-mode consumer
572
+ -- that never sets the GUC would see ZERO rows under these policies, not its
573
+ -- own data — by design (fail-safe deny), but a real behaviour change.
574
+ -- GoTimer/kinstripe (add-on mode) are pinned to hazo_blog ~1.6 and never
575
+ -- pull this DDL, so this doesn't affect them; flag this compatibility note
576
+ -- again before ever applying this section to a single-tenant deployment on
577
+ -- hazo_blog 2.x+.
578
+ --
579
+ -- APP-SIDE WIRING: these policies are only a real backstop once the app
580
+ -- actually issues `SET LOCAL app.current_scope_id = '<scope>'` inside the
581
+ -- transaction wrapping each request's DB work. As of Phase 1a issue 17,
582
+ -- the BlogSketch platform app does NOT yet do this (see that ticket's
583
+ -- write-up/docs/TENANT_ISOLATION_TESTING.md in the platform repo for why,
584
+ -- and the follow-up it tracks) — the policies themselves are implemented
585
+ -- and proven correct via direct SQL in this package's own test suite, but
586
+ -- production traffic does not yet benefit from them until that follow-up
587
+ -- lands. Said differently: query-level scope filtering (already in place)
588
+ -- is still the thing actually protecting production right now.
589
+
590
+ ALTER TABLE hazo_blog_categories ENABLE ROW LEVEL SECURITY;
591
+ ALTER TABLE hazo_blog_categories FORCE ROW LEVEL SECURITY;
592
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_categories;
593
+ CREATE POLICY tenant_isolation ON hazo_blog_categories
594
+ USING (
595
+ hazo_blog_current_scope_id() IS NOT NULL
596
+ AND (scope_id IS NULL OR scope_id = hazo_blog_current_scope_id())
597
+ )
598
+ WITH CHECK (
599
+ hazo_blog_current_scope_id() IS NOT NULL
600
+ AND (scope_id IS NULL OR scope_id = hazo_blog_current_scope_id())
601
+ );
602
+
603
+ ALTER TABLE hazo_blog_tags ENABLE ROW LEVEL SECURITY;
604
+ ALTER TABLE hazo_blog_tags FORCE ROW LEVEL SECURITY;
605
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_tags;
606
+ CREATE POLICY tenant_isolation ON hazo_blog_tags
607
+ USING (
608
+ hazo_blog_current_scope_id() IS NOT NULL
609
+ AND (scope_id IS NULL OR scope_id = hazo_blog_current_scope_id())
610
+ )
611
+ WITH CHECK (
612
+ hazo_blog_current_scope_id() IS NOT NULL
613
+ AND (scope_id IS NULL OR scope_id = hazo_blog_current_scope_id())
614
+ );
615
+
616
+ ALTER TABLE hazo_blog_posts ENABLE ROW LEVEL SECURITY;
617
+ ALTER TABLE hazo_blog_posts FORCE ROW LEVEL SECURITY;
618
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_posts;
619
+ CREATE POLICY tenant_isolation ON hazo_blog_posts
620
+ USING (scope_id = hazo_blog_current_scope_id())
621
+ WITH CHECK (scope_id = hazo_blog_current_scope_id());
622
+
623
+ ALTER TABLE hazo_blog_revisions ENABLE ROW LEVEL SECURITY;
624
+ ALTER TABLE hazo_blog_revisions FORCE ROW LEVEL SECURITY;
625
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_revisions;
626
+ CREATE POLICY tenant_isolation ON hazo_blog_revisions
627
+ USING (scope_id = hazo_blog_current_scope_id())
628
+ WITH CHECK (scope_id = hazo_blog_current_scope_id());
629
+
630
+ -- Join table, no scope_id column of its own — reach through post_id.
631
+ ALTER TABLE hazo_blog_post_tags ENABLE ROW LEVEL SECURITY;
632
+ ALTER TABLE hazo_blog_post_tags FORCE ROW LEVEL SECURITY;
633
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_post_tags;
634
+ CREATE POLICY tenant_isolation ON hazo_blog_post_tags
635
+ USING (
636
+ EXISTS (
637
+ SELECT 1 FROM hazo_blog_posts p
638
+ WHERE p.id = hazo_blog_post_tags.post_id
639
+ AND p.scope_id = hazo_blog_current_scope_id()
640
+ )
641
+ )
642
+ WITH CHECK (
643
+ EXISTS (
644
+ SELECT 1 FROM hazo_blog_posts p
645
+ WHERE p.id = hazo_blog_post_tags.post_id
646
+ AND p.scope_id = hazo_blog_current_scope_id()
647
+ )
648
+ );
649
+
650
+ ALTER TABLE hazo_blog_bylines ENABLE ROW LEVEL SECURITY;
651
+ ALTER TABLE hazo_blog_bylines FORCE ROW LEVEL SECURITY;
652
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_bylines;
653
+ CREATE POLICY tenant_isolation ON hazo_blog_bylines
654
+ USING (scope_id = hazo_blog_current_scope_id())
655
+ WITH CHECK (scope_id = hazo_blog_current_scope_id());
656
+
657
+ -- Join table, no scope_id column of its own — reach through post_id (same
658
+ -- approach as hazo_blog_post_tags above; byline_id would work just as well
659
+ -- but post_id is present on every row and keeps both join-table policies
660
+ -- in this file symmetrical).
661
+ ALTER TABLE hazo_blog_post_bylines ENABLE ROW LEVEL SECURITY;
662
+ ALTER TABLE hazo_blog_post_bylines FORCE ROW LEVEL SECURITY;
663
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_post_bylines;
664
+ CREATE POLICY tenant_isolation ON hazo_blog_post_bylines
665
+ USING (
666
+ EXISTS (
667
+ SELECT 1 FROM hazo_blog_posts p
668
+ WHERE p.id = hazo_blog_post_bylines.post_id
669
+ AND p.scope_id = hazo_blog_current_scope_id()
670
+ )
671
+ )
672
+ WITH CHECK (
673
+ EXISTS (
674
+ SELECT 1 FROM hazo_blog_posts p
675
+ WHERE p.id = hazo_blog_post_bylines.post_id
676
+ AND p.scope_id = hazo_blog_current_scope_id()
677
+ )
678
+ );
679
+
680
+ ALTER TABLE hazo_blog_media ENABLE ROW LEVEL SECURITY;
681
+ ALTER TABLE hazo_blog_media FORCE ROW LEVEL SECURITY;
682
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_media;
683
+ CREATE POLICY tenant_isolation ON hazo_blog_media
684
+ USING (scope_id = hazo_blog_current_scope_id())
685
+ WITH CHECK (scope_id = hazo_blog_current_scope_id());
686
+
687
+ ALTER TABLE hazo_blog_media_usage ENABLE ROW LEVEL SECURITY;
688
+ ALTER TABLE hazo_blog_media_usage FORCE ROW LEVEL SECURITY;
689
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_media_usage;
690
+ CREATE POLICY tenant_isolation ON hazo_blog_media_usage
691
+ USING (scope_id = hazo_blog_current_scope_id())
692
+ WITH CHECK (scope_id = hazo_blog_current_scope_id());
693
+
694
+ ALTER TABLE hazo_blog_settings ENABLE ROW LEVEL SECURITY;
695
+ ALTER TABLE hazo_blog_settings FORCE ROW LEVEL SECURITY;
696
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_settings;
697
+ CREATE POLICY tenant_isolation ON hazo_blog_settings
698
+ USING (
699
+ hazo_blog_current_scope_id() IS NOT NULL
700
+ AND (scope_id IS NULL OR scope_id = hazo_blog_current_scope_id())
701
+ )
702
+ WITH CHECK (
703
+ hazo_blog_current_scope_id() IS NOT NULL
704
+ AND (scope_id IS NULL OR scope_id = hazo_blog_current_scope_id())
705
+ );
706
+
707
+ ALTER TABLE hazo_blog_app_site_domains ENABLE ROW LEVEL SECURITY;
708
+ ALTER TABLE hazo_blog_app_site_domains FORCE ROW LEVEL SECURITY;
709
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_app_site_domains;
710
+ CREATE POLICY tenant_isolation ON hazo_blog_app_site_domains
711
+ USING (scope_id = hazo_blog_current_scope_id())
712
+ WITH CHECK (scope_id = hazo_blog_current_scope_id());
713
+
714
+ -- hazo_blog_menus/hazo_blog_menu_items (Phase 1b issue 04) — scope_id is
715
+ -- NOT NULL on both (see their own table comments above), so these get the
716
+ -- same strict policy as hazo_blog_posts/bylines/media/app_site_domains,
717
+ -- not the nullable-scope_id dual-mode policy hazo_blog_categories/tags
718
+ -- use.
719
+ ALTER TABLE hazo_blog_menus ENABLE ROW LEVEL SECURITY;
720
+ ALTER TABLE hazo_blog_menus FORCE ROW LEVEL SECURITY;
721
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_menus;
722
+ CREATE POLICY tenant_isolation ON hazo_blog_menus
723
+ USING (scope_id = hazo_blog_current_scope_id())
724
+ WITH CHECK (scope_id = hazo_blog_current_scope_id());
725
+
726
+ ALTER TABLE hazo_blog_menu_items ENABLE ROW LEVEL SECURITY;
727
+ ALTER TABLE hazo_blog_menu_items FORCE ROW LEVEL SECURITY;
728
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_menu_items;
729
+ CREATE POLICY tenant_isolation ON hazo_blog_menu_items
730
+ USING (scope_id = hazo_blog_current_scope_id())
731
+ WITH CHECK (scope_id = hazo_blog_current_scope_id());
732
+
733
+ -- hazo_blog_themes (Phase 1b issue 06) — nullable `scope_id` (NULL =
734
+ -- built-in/global, visible to every scope), so this gets the SAME
735
+ -- dual-mode policy hazo_blog_categories/tags/settings use above, not the
736
+ -- strict hazo_blog_menus-style policy.
737
+ ALTER TABLE hazo_blog_themes ENABLE ROW LEVEL SECURITY;
738
+ ALTER TABLE hazo_blog_themes FORCE ROW LEVEL SECURITY;
739
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_themes;
740
+ CREATE POLICY tenant_isolation ON hazo_blog_themes
741
+ USING (
742
+ hazo_blog_current_scope_id() IS NOT NULL
743
+ AND (scope_id IS NULL OR scope_id = hazo_blog_current_scope_id())
744
+ )
745
+ WITH CHECK (
746
+ hazo_blog_current_scope_id() IS NOT NULL
747
+ AND (scope_id IS NULL OR scope_id = hazo_blog_current_scope_id())
748
+ );
749
+
750
+ -- hazo_blog_theme_versions has NO scope_id of its own — it inherits its
751
+ -- theme's visibility via the `theme_id` join, same reasoning
752
+ -- hazo_blog_post_tags (no direct scope_id) relies on its post_id join for.
753
+ -- RLS can't express a join-based policy as cheaply as a direct column
754
+ -- check, so this table is deliberately NOT given its own RLS policy here
755
+ -- — the application-level scope check already happens in
756
+ -- `ThemeService`/`ThemeRepository` (every version lookup goes through a
757
+ -- theme id the caller already proved visible). Flagged as a known gap,
758
+ -- same category `docs/TENANT_ISOLATION_TESTING.md` already tracks for
759
+ -- `hazo_seo_*` tables having no RLS policies.
760
+ ALTER TABLE hazo_blog_theme_versions ENABLE ROW LEVEL SECURITY;
761
+ ALTER TABLE hazo_blog_theme_versions FORCE ROW LEVEL SECURITY;
762
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_theme_versions;
763
+ CREATE POLICY tenant_isolation ON hazo_blog_theme_versions
764
+ USING (
765
+ EXISTS (
766
+ SELECT 1 FROM hazo_blog_themes t
767
+ WHERE t.id = hazo_blog_theme_versions.theme_id
768
+ AND (t.scope_id IS NULL OR t.scope_id = hazo_blog_current_scope_id())
769
+ )
770
+ )
771
+ WITH CHECK (
772
+ EXISTS (
773
+ SELECT 1 FROM hazo_blog_themes t
774
+ WHERE t.id = hazo_blog_theme_versions.theme_id
775
+ AND (t.scope_id IS NULL OR t.scope_id = hazo_blog_current_scope_id())
776
+ )
777
+ );
778
+
779
+ ALTER TABLE hazo_blog_site_theme ENABLE ROW LEVEL SECURITY;
780
+ ALTER TABLE hazo_blog_site_theme FORCE ROW LEVEL SECURITY;
781
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_site_theme;
782
+ CREATE POLICY tenant_isolation ON hazo_blog_site_theme
783
+ USING (scope_id = hazo_blog_current_scope_id())
784
+ WITH CHECK (scope_id = hazo_blog_current_scope_id());
785
+
786
+ -- hazo_blog_site_files (Phase 1b issue 09) — scope_id is NOT NULL (see its
787
+ -- own table comment above), so this gets the same strict policy as
788
+ -- hazo_blog_menus/hazo_blog_site_theme, not the nullable-scope_id dual-mode
789
+ -- policy hazo_blog_categories/tags/settings/themes use.
790
+ ALTER TABLE hazo_blog_site_files ENABLE ROW LEVEL SECURITY;
791
+ ALTER TABLE hazo_blog_site_files FORCE ROW LEVEL SECURITY;
792
+ DROP POLICY IF EXISTS tenant_isolation ON hazo_blog_site_files;
793
+ CREATE POLICY tenant_isolation ON hazo_blog_site_files
794
+ USING (scope_id = hazo_blog_current_scope_id())
795
+ WITH CHECK (scope_id = hazo_blog_current_scope_id());