@protobase/server 0.127.4 → 0.128.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.
@@ -1,22 +1,43 @@
1
1
  import { ddl, ddlText } from "./ddl.js";
2
- import { createMigrationsTable, recordMigrations } from "./migrations-table.js";
2
+ import { createMigrationsTable, migrationsTable, recordMigrations } from "./migrations-table.js";
3
3
  import { legacyVersions } from "./steps/legacy-versions.js";
4
+ import { sql } from "kysely";
4
5
  //#region src/better-auth/store/migration-sql.ts
5
6
  /** The statements as SQL text. */
6
7
  const statementsSql = (statements) => statements.map((statement) => `${ddlText(statement)};`).join("\n\n");
7
8
  const comment = ({ module, step, on }) => `-- ${module} ${step.step} ${step.name}${on === void 0 ? "" : `, on the tables of ${on}`}: ${step.title}.`;
9
+ const partStatements = (names, rendered, versions, i) => {
10
+ const part = rendered[i];
11
+ const recorded = versions.includes(part.version) && !rendered.slice(i + 1).some((later) => later.version === part.version);
12
+ return [...part.statements, ...recorded ? [recordMigrations(names, legacyVersions.filter(({ version }) => version === part.version))] : []];
13
+ };
8
14
  /**
9
15
  * The rendered steps, each under a comment naming it; after the last statements of each version of `versions`, the
10
16
  * row that records it in `auth_migrations`.
11
17
  */
12
- const stepsSql = (names, rendered, versions) => rendered.map((part, i) => {
13
- const record = versions.includes(part.version) && !rendered.slice(i + 1).some((later) => later.version === part.version) ? [recordMigrations(names, legacyVersions.filter(({ version }) => version === part.version))] : [];
14
- return `${comment(part)}\n${statementsSql([...part.statements, ...record])}`;
15
- }).join("\n\n");
16
- /** The migrations table, for a store that has none yet, in the store's schema, made when it is missing. */
17
- const migrationsTableSql = (names) => {
18
+ const stepsSql = (names, rendered, versions) => rendered.map((part, i) => `${comment(part)}\n${statementsSql(partStatements(names, rendered, versions, i))}`).join("\n\n");
19
+ const blockQuote = "$version$";
20
+ const statementQuote = "$statement$";
21
+ const unlessRecorded = (names, version, statements) => {
22
+ const texts = statements.map(ddlText);
23
+ const quoted = texts.find((text) => text.includes(blockQuote) || text.includes(statementQuote));
24
+ if (quoted) throw new Error(`A step's statement holds ${blockQuote} or ${statementQuote}, which guard it: ${quoted}`);
25
+ const table = migrationsTable(names);
26
+ const recorded = ddlText(ddl`select from ${table} where ${table.columns.version} = ${sql.lit(version)}`);
27
+ return `do ${blockQuote} begin\nif not exists (${recorded}) then\n${texts.map((text) => `execute ${statementQuote}${text}${statementQuote};`).join("\n")}\nend if;\nend ${blockQuote};`;
28
+ };
29
+ /**
30
+ * The rendered steps as `stepsSql` writes them, each part run only while `auth_migrations` lacks its version, so a
31
+ * database that has a version already, such as one `protobase-serve migrate-release` brought to a release, skips it.
32
+ */
33
+ const guardedStepsSql = (names, rendered, versions) => rendered.map((part, i) => `${comment(part)}\n${unlessRecorded(names, part.version, partStatements(names, rendered, versions, i))}`).join("\n\n");
34
+ /**
35
+ * The migrations table, for a store that has none yet, in the store's schema, made when it is missing. With
36
+ * `ifMissing`, the table too is made only when missing, for a database a host made it in already.
37
+ */
38
+ const migrationsTableSql = (names, ifMissing = false) => {
18
39
  const schema = names.schema === void 0 ? [] : [ddl`create schema if not exists ${names.schema}`];
19
- return `-- The record of the auth store's migrations.\n${statementsSql([...schema, createMigrationsTable(names)])}`;
40
+ return `-- The record of the auth store's migrations.\n${statementsSql([...schema, createMigrationsTable(names, ifMissing)])}`;
20
41
  };
21
42
  //#endregion
22
- export { migrationsTableSql, statementsSql, stepsSql };
43
+ export { guardedStepsSql, migrationsTableSql, statementsSql, stepsSql };
@@ -3,11 +3,11 @@ import { sql } from "kysely";
3
3
  //#region src/better-auth/store/migrations-table.ts
4
4
  /** The table, in the store's schema, as the store's names resolve `auth_migrations`. */
5
5
  const migrationsTable = ({ names }) => names.table("auth_migrations");
6
- /** The statement that makes the table. */
7
- const createMigrationsTable = (names) => {
6
+ /** The statement that makes the table; with `ifMissing`, only when it is not there yet. */
7
+ const createMigrationsTable = (names, ifMissing = false) => {
8
8
  const table = migrationsTable(names);
9
9
  const { version, name, applied_at } = table.columns;
10
- return ddl`create table ${table} (
10
+ return ddl`create table ${sql.raw(ifMissing ? "if not exists " : "")}${table} (
11
11
  ${version} integer primary key,
12
12
  ${name} text not null,
13
13
  ${applied_at} timestamptz default current_timestamp not null
@@ -3,7 +3,8 @@ import { LastSteps } from "./store-model.js";
3
3
  //#region src/better-auth/store/offline/offline-steps.d.ts
4
4
  /**
5
5
  * The steps of the modules that are on after `lastSteps`, the snapshot's, each version of the store's former chain
6
- * followed by its row in `auth_migrations`, which the server's check reads; for a store the snapshot has none of
6
+ * followed by its row in `auth_migrations`, which the server's check reads, each run only while that table lacks its
7
+ * version, so a database that has it already skips it; for a store the snapshot has none of
7
8
  * (`lastSteps` undefined), that table first. `checks`: the checks of the steps on tables the store has already, which
8
9
  * refuse when its rows break what a step assumes. `undefined` when the snapshot has every step.
9
10
  */
@@ -1,5 +1,5 @@
1
1
  import { legacyVersions } from "../steps/legacy-versions.js";
2
- import { migrationsTableSql, statementsSql, stepsSql } from "../migration-sql.js";
2
+ import { guardedStepsSql, migrationsTableSql, statementsSql } from "../migration-sql.js";
3
3
  import { moduleSteps } from "../steps/index.js";
4
4
  import { renderSteps } from "../steps/render.js";
5
5
  import { migrationNames } from "../steps/step.js";
@@ -13,7 +13,8 @@ const refuseNewer = (lastSteps) => {
13
13
  };
14
14
  /**
15
15
  * The steps of the modules that are on after `lastSteps`, the snapshot's, each version of the store's former chain
16
- * followed by its row in `auth_migrations`, which the server's check reads; for a store the snapshot has none of
16
+ * followed by its row in `auth_migrations`, which the server's check reads, each run only while that table lacks its
17
+ * version, so a database that has it already skips it; for a store the snapshot has none of
17
18
  * (`lastSteps` undefined), that table first. `checks`: the checks of the steps on tables the store has already, which
18
19
  * refuse when its rows break what a step assumes. `undefined` when the snapshot has every step.
19
20
  */
@@ -25,10 +26,10 @@ const offlineStepsMigration = (names, lastSteps) => {
25
26
  const rendered = renderSteps(migration, names.names.modules, had);
26
27
  if (rendered.length === 0) return void 0;
27
28
  const recorded = (version) => Object.entries(moduleSteps).every(([module, steps]) => steps.filter((step) => step.version === version).every((step) => had(module, step)));
28
- const steps = stepsSql(names, rendered, legacyVersions.map(({ version }) => version).filter((version) => !recorded(version)));
29
+ const steps = guardedStepsSql(names, rendered, legacyVersions.map(({ version }) => version).filter((version) => !recorded(version)));
29
30
  const checks = rendered.filter(({ module, step, on }) => on === void 0 && step.check && had(module, moduleSteps[module][0])).map(({ step }) => statementsSql([step.check(migration)]));
30
31
  return {
31
- sql: lastSteps ? steps : [migrationsTableSql(names), steps].join("\n\n"),
32
+ sql: lastSteps ? steps : [migrationsTableSql(names, true), steps].join("\n\n"),
32
33
  checks
33
34
  };
34
35
  };
package/dist/package.js CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.127.4";
2
+ var version = "0.128.0";
3
3
  //#endregion
4
4
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/server",
3
- "version": "0.127.4",
3
+ "version": "0.128.0",
4
4
  "description": "The Protobase REST API as a Hono app, with Better Auth sign-in.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -44,11 +44,11 @@
44
44
  "@better-auth/sso": "^1.7.12",
45
45
  "@livekit/protocol": "1.53.1",
46
46
  "@modelcontextprotocol/server": "^2.3.1",
47
- "@protobase/layout": "0.127.4",
48
- "@protobase/query": "0.127.4",
49
- "@protobase/schema": "0.127.4",
50
- "@protobase/workflow-plugins": "0.127.4",
51
- "@protobase/workflows": "0.127.4",
47
+ "@protobase/layout": "0.128.0",
48
+ "@protobase/query": "0.128.0",
49
+ "@protobase/schema": "0.128.0",
50
+ "@protobase/workflow-plugins": "0.128.0",
51
+ "@protobase/workflows": "0.128.0",
52
52
  "@react-email/render": "^2.1.0",
53
53
  "@react-pdf/primitives": "^4.4.0",
54
54
  "@react-pdf/renderer": "^4.9.0",