@marktiderman/genesis-cli 1.20.0 → 1.21.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 (33) hide show
  1. package/README.md +28 -0
  2. package/dist/cli.js +89 -2
  3. package/dist/cli.js.map +1 -1
  4. package/dist/doctor.js +34 -0
  5. package/dist/doctor.js.map +1 -1
  6. package/dist/genesis-catalog.json +83 -14
  7. package/dist/index.js +34 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/starter/README.md +8 -5
  10. package/dist/starter/__tests__/components/setup.ts +11 -0
  11. package/dist/starter/__tests__/starter.test.ts +19 -0
  12. package/dist/starter/package.json +4 -3
  13. package/dist/starter/src/lib/app.ts +13 -5
  14. package/dist/starter/src/lib/define-starter.ts +1 -1
  15. package/dist/starter/src/lib/devtools-overlay.ts +3 -4
  16. package/dist/starter/src/lib/native-modules.ts +5 -1
  17. package/dist/starter/src/lib/offline.ts +14 -0
  18. package/dist/starter/supabase/migrations/20261024000750_support_default_tenant.sql +18 -0
  19. package/dist/starter/supabase/migrations/20261024000800_genesis_support_tickets.sql +800 -0
  20. package/dist/starter/supabase/migrations/20261025000070_starter_tenancy.sql +26 -0
  21. package/dist/starter/supabase/migrations/20261025000080_genesis_chat_tenants.sql +716 -0
  22. package/dist/starter/supabase/tests/database/chat_people.test.sql +1 -1
  23. package/dist/starter/supabase/tests/database/genesis_chat.test.sql +10 -2
  24. package/dist/starter/supabase/tests/database/genesis_chat_coaching.test.sql +8 -0
  25. package/dist/starter/supabase/tests/database/genesis_chat_search_threads.test.sql +8 -0
  26. package/dist/starter/supabase/tests/database/genesis_chat_tenants.test.sql +259 -0
  27. package/dist/starter/supabase/tests/database/genesis_support.test.sql +15 -3
  28. package/dist/starter/supabase/tests/database/genesis_support_findings.test.sql +10 -0
  29. package/dist/starter/supabase/tests/database/genesis_support_tickets.test.sql +327 -0
  30. package/dist/starter/supabase/tests/database/starter_tenancy.test.sql +19 -0
  31. package/dist/vite.js +34 -0
  32. package/dist/vite.js.map +1 -1
  33. package/package.json +2 -2
package/README.md CHANGED
@@ -437,6 +437,34 @@ steps point at the notifications runner (`genesis-notifications`), which sends i
437
437
  0001 after 0005 works too (it adopts the tables); then stop calling `genesis-push-send`, as
438
438
  notifications' steps say.
439
439
 
440
+ A migration that calls a hook the app defines (a function Genesis never ships) names it in
441
+ `needs`, with what to add:
442
+
443
+ ```json
444
+ {
445
+ "file": "migrations/0004_genesis_chat_tenants.sql",
446
+ "needs": [{ "function": "private.genesis_viewer_people", "how": "add the tenancy hook before it (genesis-supabase 0016's header)" }]
447
+ }
448
+ ```
449
+
450
+ When `genesis add`, `migrate` or `upgrade` would copy it into an app where no migration defines that
451
+ function (by `create [or replace] function`, in the app's migrations or one copied before it in
452
+ the same run, outside SQL comments), its row says `needs <function> first: <how>` and the
453
+ command warns, before `supabase db push` fails on it.
454
+
455
+ A migration another block's migration reads names that one in `runsBefore`:
456
+
457
+ ```json
458
+ {
459
+ "file": "migrations/0006_genesis_support_chat_tenants.sql",
460
+ "runsBefore": [{ "package": "@marktiderman/genesis-chat", "file": "migrations/0004_genesis_chat_tenants.sql" }]
461
+ }
462
+ ```
463
+
464
+ When both are new in the same `genesis add`, `migrate` or `upgrade` run, it is copied first (with
465
+ its own package's earlier migrations, which keep their order), so it gets the earlier timestamp.
466
+ When the other is already in the app, nothing moves: the migration has to handle that case itself.
467
+
440
468
  In an app whose database files live elsewhere ([A phone app beside a web app](#a-phone-app-beside-a-web-app)),
441
469
  migrate plans nothing and says where to run it.
442
470
 
package/dist/cli.js CHANGED
@@ -121,6 +121,39 @@ function parseManifest(home, id, raw, version, options = {}) {
121
121
  });
122
122
  }
123
123
  }
124
+ if (m["needs"] !== void 0) {
125
+ if (!Array.isArray(m["needs"])) problems.push(`${where2}.needs: must be a list of { function, how }`);
126
+ else {
127
+ out.needs = m["needs"].flatMap((n, j) => {
128
+ const at2 = `${where2}.needs[${j}]`;
129
+ if (!isObject(n) || typeof n["function"] !== "string" || typeof n["how"] !== "string" || !n["how"].trim()) {
130
+ problems.push(`${at2}: must be { function: "schema.name", how: "what to add" }`);
131
+ return [];
132
+ }
133
+ if (!SQL_FUNCTION.test(n["function"])) {
134
+ problems.push(`${at2}.function: "${n["function"]}" must be a schema-qualified function name, like private.genesis_viewer_people`);
135
+ return [];
136
+ }
137
+ return [{ function: n["function"], how: n["how"].trim() }];
138
+ });
139
+ }
140
+ }
141
+ if (m["runsBefore"] !== void 0) {
142
+ if (!Array.isArray(m["runsBefore"])) problems.push(`${where2}.runsBefore: must be a list of { package, file }`);
143
+ else {
144
+ out.runsBefore = m["runsBefore"].map((r, j) => {
145
+ const at2 = `${where2}.runsBefore[${j}]`;
146
+ if (!isObject(r)) {
147
+ problems.push(`${at2}: must be an object`);
148
+ return { package: home, file: "" };
149
+ }
150
+ const before = { package: pkgOf(r["package"], at2), file: pathOf(r["file"], `${at2}.file`) };
151
+ if (before.file && !MIGRATION_FILE.test(before.file.split("/").pop())) problems.push(`${at2}.file: "${before.file}" must be named NNNN_name.sql`);
152
+ if (before.package === out.package && before.file === file) problems.push(`${at2}: a migration can't run before itself`);
153
+ return before;
154
+ });
155
+ }
156
+ }
124
157
  if (m["test"] !== void 0) {
125
158
  out.test = pathOf(m["test"], `${where2}.test`);
126
159
  if (out.test && !/^[a-z0-9_]+\.test\.sql$/.test(out.test.split("/").pop().replace(/^\d{4}_/, ""))) problems.push(`${where2}.test: "${out.test}" must be named [NNNN_]name.test.sql`);
@@ -231,7 +264,7 @@ function declarationFields(block) {
231
264
  function isBlockDef(value) {
232
265
  return isObject(value) && typeof value["id"] === "string" && BLOCK_ID.test(value["id"]) && (value["contributes"] === void 0 || isObject(value["contributes"])) && (isObject(value["contributes"]) || Array.isArray(value["requires"]));
233
266
  }
234
- var GENESIS_PACKAGE, BLOCK_ID, MIGRATION_FILE, FUNCTION_NAME, ROUTE, INSTALL_SPEC, SECRET, ManifestError, isObject, strings;
267
+ var GENESIS_PACKAGE, BLOCK_ID, MIGRATION_FILE, FUNCTION_NAME, ROUTE, INSTALL_SPEC, SECRET, SQL_FUNCTION, ManifestError, isObject, strings;
235
268
  var init_manifest = __esm({
236
269
  "src/blocks/manifest.ts"() {
237
270
  GENESIS_PACKAGE = /^@marktiderman\/genesis(-[a-z0-9-]+)?$/;
@@ -241,6 +274,7 @@ var init_manifest = __esm({
241
274
  ROUTE = /^\/(?:[A-Za-z0-9_\-[\]().]+\/?)*$/;
242
275
  INSTALL_SPEC = /^(@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*(@[A-Za-z0-9.^~<>=|* -]+)?$/;
243
276
  SECRET = /^[A-Z][A-Z0-9_]*$/;
277
+ SQL_FUNCTION = /^[a-z_][a-z0-9_]*\.[a-z_][a-z0-9_]*$/;
244
278
  ManifestError = class extends Error {
245
279
  constructor(problems) {
246
280
  super(`Invalid block manifest:
@@ -689,20 +723,24 @@ __export(migrations_exports, {
689
723
  MIGRATIONS_DIR: () => MIGRATIONS_DIR,
690
724
  TESTS_DIR: () => TESTS_DIR,
691
725
  applyMigrationPlan: () => applyMigrationPlan,
726
+ definesFunction: () => definesFunction,
692
727
  formatMigrationRows: () => formatMigrationRows,
693
728
  migrationHeader: () => migrationHeader,
694
729
  needsAttention: () => needsAttention,
695
730
  nextStamp: () => nextStamp,
696
731
  nextTimestamps: () => nextTimestamps,
732
+ orderByRunsBefore: () => orderByRunsBefore,
697
733
  planMigrations: () => planMigrations,
698
734
  providingMigrations: () => providingMigrations,
699
735
  readAppMigrations: () => readAppMigrations,
700
736
  rememberMigrationCopies: () => rememberMigrationCopies,
701
737
  stemOf: () => stemOf,
702
738
  stemOrNull: () => stemOrNull,
739
+ stripSqlComments: () => stripSqlComments,
703
740
  supersededFunctions: () => supersededFunctions,
704
741
  textSha: () => textSha,
705
742
  timestampOf: () => timestampOf,
743
+ unmetNeeds: () => unmetNeeds,
706
744
  validHeaderFile: () => validHeaderFile
707
745
  });
708
746
  function migrationHeader(pkg, file, source) {
@@ -729,6 +767,46 @@ function readAppMigrations(root, dir = MIGRATIONS_DIR) {
729
767
  return { path: p, name, content };
730
768
  });
731
769
  }
770
+ function stripSqlComments(sql) {
771
+ return sql.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/--[^\n]*/g, " ");
772
+ }
773
+ function definesFunction(sql, fn) {
774
+ const [schema, name] = fn.split(".");
775
+ const quoted = (id) => `(?:${id}|"${id}")`;
776
+ sql = stripSqlComments(sql);
777
+ return new RegExp(`create\\s+(?:or\\s+replace\\s+)?function\\s+${quoted(schema)}\\s*\\.\\s*${quoted(name)}\\s*\\(`, "i").test(sql);
778
+ }
779
+ function orderByRunsBefore(sources) {
780
+ if (!sources.some((m) => m.runsBefore?.length)) return sources.slice();
781
+ const key2 = (m) => `${m.package} ${m.file}`;
782
+ const index = /* @__PURE__ */ new Map();
783
+ sources.forEach((m, i) => {
784
+ if (!index.has(key2(m))) index.set(key2(m), i);
785
+ });
786
+ const after = sources.map(() => /* @__PURE__ */ new Set());
787
+ const last = /* @__PURE__ */ new Map();
788
+ sources.forEach((m, i) => {
789
+ const prev = last.get(m.package);
790
+ if (prev !== void 0) after[i].add(prev);
791
+ last.set(m.package, i);
792
+ for (const target of m.runsBefore ?? []) {
793
+ const t = index.get(key2(target));
794
+ if (t !== void 0 && t !== i) after[t].add(i);
795
+ }
796
+ });
797
+ const out = [];
798
+ const done = /* @__PURE__ */ new Set();
799
+ while (out.length < sources.length) {
800
+ const next = sources.findIndex((_, i) => !done.has(i) && [...after[i]].every((d) => done.has(d)));
801
+ if (next === -1) return sources.slice();
802
+ done.add(next);
803
+ out.push(sources[next]);
804
+ }
805
+ return out;
806
+ }
807
+ function unmetNeeds(src, defined) {
808
+ return (src.needs ?? []).filter((n) => !defined(n.function)).map((n) => `needs ${n.function} first: ${n.how}`);
809
+ }
732
810
  function needsAttention(row) {
733
811
  return row.state !== "current" && row.state !== "new" && row.state !== "deleted" && row.state !== "provided" && !row.forward;
734
812
  }
@@ -784,6 +862,7 @@ function nextTimestamps(existing, now, count) {
784
862
  function planMigrations(root, sources, options = { now: /* @__PURE__ */ new Date() }) {
785
863
  const app = readAppMigrations(root);
786
864
  const rows = [];
865
+ sources = orderByRunsBefore(sources);
787
866
  const providers = providingMigrations([{ migrations: sources }]);
788
867
  for (const p of options.providers ?? []) if (!providers.some((o) => o.package === p.package && o.file === p.file)) providers.push(p);
789
868
  const providerAt = (p) => {
@@ -828,6 +907,12 @@ function planMigrations(root, sources, options = { now: /* @__PURE__ */ new Date
828
907
  continue;
829
908
  }
830
909
  const row2 = { ...base, state: "new" };
910
+ const earlier = sources.slice(0, sources.indexOf(src)).filter((s) => s.source !== null);
911
+ const unmet = unmetNeeds(src, (fn) => app.some((m) => definesFunction(m.content, fn)) || earlier.some((s) => definesFunction(s.source, fn)));
912
+ if (unmet.length) {
913
+ row2.unmet = unmet;
914
+ row2.detail = unmet.join("; ");
915
+ }
831
916
  rows.push(row2);
832
917
  pending.push({ row: row2, src, kind: "migration" });
833
918
  continue;
@@ -9914,6 +9999,7 @@ async function runAdd(options) {
9914
9999
  const providers = providingMigrations([full, ...catalog]);
9915
10000
  const migrations2 = home ? { rows: [], creates: [] } : planMigrations(root, sourceMigrations(root, full), { now, ...lock.migrations ? { copied: lock.migrations } : {}, providers });
9916
10001
  const superseded = supersededFunctions(migrations2.rows, providers);
10002
+ for (const r of migrations2.rows) for (const u of r.unmet ?? []) planWarnings.push(`${r.package} ${r.file} ${u}`);
9917
10003
  const replaced2 = full.functions.filter((fn) => superseded.has(`${fn.package}:${fn.dir}`)).map((fn) => {
9918
10004
  const by = superseded.get(`${fn.package}:${fn.dir}`);
9919
10005
  const block = providers.find((p) => p.package === by.package && p.file === by.file)?.block;
@@ -10228,6 +10314,7 @@ async function runMigrate(options) {
10228
10314
  }
10229
10315
  if (!ids.length && !sources.length) warnings.push("No blocks in genesis.lock.json and no copied package migrations: nothing to compare. Add a block (genesis add <block>), or pass --block.");
10230
10316
  const plan = planMigrations(root, sources, { now: options.now ?? /* @__PURE__ */ new Date(), ...options.forward ? { forward: true } : {}, ...lock.migrations ? { copied: lock.migrations } : {}, providers: providingMigrations([...manifests, ...catalog]) });
10317
+ for (const r of plan.rows) for (const u of r.unmet ?? []) warnings.push(`${r.package} ${r.file} ${u}`);
10231
10318
  const attention = plan.rows.some(needsAttention);
10232
10319
  const notifies = ids.some((id) => (lock.blocks[id]?.package ?? (id === "notifications" ? NOTIFICATIONS_PACKAGE : "")) === NOTIFICATIONS_PACKAGE);
10233
10320
  const types = notifies ? await planNotificationTypes(root, {
@@ -11020,7 +11107,7 @@ async function runUpgrade(options) {
11020
11107
  manifests.push(manifest);
11021
11108
  if (!home) {
11022
11109
  for (const row of planMigrations(root, sourceMigrations(root, manifest), { now: /* @__PURE__ */ new Date(), tests: false, ...lock.migrations ? { copied: lock.migrations } : {}, providers: providingMigrations(catalog) }).rows) {
11023
- if (row.state === "new") newMigrations.push(`${row.package} ${row.file}`);
11110
+ if (row.state === "new") newMigrations.push(`${row.package} ${row.file}${row.unmet?.length ? ` (${row.unmet.join("; ")})` : ""}`);
11024
11111
  }
11025
11112
  }
11026
11113
  if (manifest.version && manifest.version !== entry.version) lock.blocks[id] = { ...entry, version: manifest.version };