@protobase/server 0.129.1 → 0.130.1
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.
- package/dist/assistant/query-runner/grants-sql.js +32 -0
- package/dist/assistant/query-runner/query-input.js +33 -0
- package/dist/assistant/query-runner/query-module.js +44 -0
- package/dist/assistant/query-runner/query-runner.d.ts +8 -11
- package/dist/assistant/query-runner/query-runner.js +15 -57
- package/dist/assistant/query-runner/read-query.js +54 -0
- package/dist/assistant/query-runner/runner-sql.js +4 -20
- package/dist/assistant/query-runner/steps/001-role.js +19 -0
- package/dist/auth-store.d.ts +2 -2
- package/dist/auth-store.js +3 -3
- package/dist/better-auth/auth-schema.d.ts +13 -4
- package/dist/better-auth/auth-schema.js +40 -32
- package/dist/better-auth/auth-settings.d.ts +59 -11
- package/dist/better-auth/auth-settings.js +57 -13
- package/dist/better-auth/auth-tables.d.ts +6 -3
- package/dist/better-auth/auth-tables.js +12 -7
- package/dist/better-auth/authenticator.js +10 -2
- package/dist/better-auth/create-auth.d.ts +62 -7
- package/dist/better-auth/create-auth.js +55 -14
- package/dist/better-auth/device-sign-in-check.js +13 -0
- package/dist/better-auth/names/declaration.d.ts +18 -5
- package/dist/better-auth/names/declaration.js +3 -1
- package/dist/better-auth/names/modules/auth.js +9 -0
- package/dist/better-auth/names/modules/index.d.ts +34 -4
- package/dist/better-auth/names/modules/organizations.js +10 -3
- package/dist/better-auth/names/modules/personal-access-tokens.js +6 -3
- package/dist/better-auth/names/modules-on.js +25 -2
- package/dist/better-auth/names/resolve-names.d.ts +9 -1
- package/dist/better-auth/names/resolve-names.js +15 -9
- package/dist/better-auth/organizations/accept-invitation.js +5 -4
- package/dist/better-auth/organizations/add-every-user.js +3 -2
- package/dist/better-auth/organizations/host-scope.d.ts +10 -0
- package/dist/better-auth/organizations/host-scope.js +15 -0
- package/dist/better-auth/organizations/host.d.ts +12 -6
- package/dist/better-auth/organizations/host.js +99 -61
- package/dist/better-auth/organizations/kept-organizations.d.ts +21 -0
- package/dist/better-auth/organizations/kept-organizations.js +62 -0
- package/dist/better-auth/organizations/member-endpoints.js +4 -3
- package/dist/better-auth/organizations/options.d.ts +15 -0
- package/dist/better-auth/organizations/options.js +19 -1
- package/dist/better-auth/organizations/people-endpoint.js +3 -2
- package/dist/better-auth/organizations/pinned-membership.js +81 -0
- package/dist/better-auth/organizations/pinned-sessions.js +46 -0
- package/dist/better-auth/organizations/session-organization.js +3 -2
- package/dist/better-auth/organizations/store.d.ts +1 -1
- package/dist/better-auth/organizations/store.js +14 -9
- package/dist/better-auth/personal-access-tokens/pinned-tokens.js +23 -0
- package/dist/better-auth/personal-access-tokens/token-plugin.js +75 -54
- package/dist/better-auth/personal-access-tokens/token-sessions.js +6 -3
- package/dist/better-auth/personal-access-tokens/token-store.js +15 -11
- package/dist/better-auth/store/adoption.js +25 -10
- package/dist/better-auth/store/drift.js +5 -4
- package/dist/better-auth/store/legacy-record.d.ts +10 -0
- package/dist/better-auth/store/legacy-record.js +56 -0
- package/dist/better-auth/store/migration-sql.js +31 -26
- package/dist/better-auth/store/offline/archive-revival.d.ts +11 -0
- package/dist/better-auth/store/offline/archive-revival.js +25 -0
- package/dist/better-auth/store/offline/baseline-store.d.ts +11 -7
- package/dist/better-auth/store/offline/baseline-store.js +15 -12
- package/dist/better-auth/store/offline/contract-gate.d.ts +18 -0
- package/dist/better-auth/store/offline/contract-gate.js +35 -0
- package/dist/better-auth/store/offline/module-archive.d.ts +41 -0
- package/dist/better-auth/store/offline/module-archive.js +95 -0
- package/dist/better-auth/store/offline/module-delete.d.ts +22 -0
- package/dist/better-auth/store/offline/module-delete.js +99 -0
- package/dist/better-auth/store/offline/module-switches.d.ts +32 -0
- package/dist/better-auth/store/offline/module-switches.js +113 -0
- package/dist/better-auth/store/offline/name-changes.d.ts +4 -1
- package/dist/better-auth/store/offline/name-changes.js +5 -2
- package/dist/better-auth/store/offline/offline-steps.d.ts +21 -7
- package/dist/better-auth/store/offline/offline-steps.js +29 -20
- package/dist/better-auth/store/offline/retired-watch.d.ts +5 -0
- package/dist/better-auth/store/offline/retired-watch.js +43 -0
- package/dist/better-auth/store/offline/settings-names.d.ts +3 -2
- package/dist/better-auth/store/offline/settings-names.js +10 -3
- package/dist/better-auth/store/offline/store-model.d.ts +27 -1
- package/dist/better-auth/store/offline/store-model.js +22 -5
- package/dist/better-auth/store/resources-outside.js +1 -1
- package/dist/better-auth/store/step-record.d.ts +13 -0
- package/dist/better-auth/store/step-record.js +59 -0
- package/dist/better-auth/store/steps/index.js +6 -1
- package/dist/better-auth/store/steps/legacy-versions.d.ts +7 -0
- package/dist/better-auth/store/steps/legacy-versions.js +4 -0
- package/dist/better-auth/store/steps/organizations/003-default-organization.js +113 -0
- package/dist/better-auth/store/steps/render.d.ts +3 -0
- package/dist/better-auth/store/steps/render.js +48 -25
- package/dist/better-auth/store/steps/step-lint.d.ts +14 -0
- package/dist/better-auth/store/steps/step-lint.js +31 -10
- package/dist/better-auth/store/steps/step.d.ts +18 -0
- package/dist/better-auth/store/steps/step.js +7 -1
- package/dist/better-auth/store/store-db.d.ts +6 -0
- package/dist/better-auth/store/store-db.js +11 -5
- package/dist/better-auth/store/store-names.d.ts +4 -0
- package/dist/better-auth/store/store-sql.d.ts +1 -1
- package/dist/better-auth/token-standing.js +12 -7
- package/dist/better-auth/users.d.ts +7 -2
- package/dist/better-auth/users.js +25 -3
- package/dist/built-in-resources.js +1 -1
- package/dist/calls/calls-module.js +42 -0
- package/dist/calls/calls-sql.js +4 -30
- package/dist/changes/capture-names.d.ts +3 -1
- package/dist/changes/capture-names.js +5 -2
- package/dist/changes/capture-schema-sql.d.ts +10 -0
- package/dist/changes/capture-schema-sql.js +21 -0
- package/dist/changes/capture-schema.d.ts +0 -18
- package/dist/changes/capture-schema.js +1 -74
- package/dist/changes/capture-sql.js +30 -24
- package/dist/changes/changes-input.js +36 -0
- package/dist/changes/changes-module.js +44 -0
- package/dist/changes/read-capture.js +67 -0
- package/dist/changes/steps/001-outbox.js +23 -0
- package/dist/channels/sends-module.js +37 -0
- package/dist/channels/sends-schema-sql.js +5 -5
- package/dist/comments/comments-module.js +30 -0
- package/dist/comments/comments-sql.js +1 -22
- package/dist/computed/buckets-sql.d.ts +5 -1
- package/dist/computed/buckets-sql.js +39 -46
- package/dist/computed/{buckets-schema.d.ts → buckets-targets.d.ts} +2 -3
- package/dist/computed/buckets-targets.js +50 -0
- package/dist/computed/computed-input.js +65 -0
- package/dist/computed/computed-module.js +102 -0
- package/dist/computed/formula-sql.js +14 -1
- package/dist/computed/read-computed.js +100 -0
- package/dist/computed/rollup-sql.js +13 -10
- package/dist/config-exports.d.ts +2 -2
- package/dist/create-admin.js +21 -5
- package/dist/derived/derived-module.js +89 -0
- package/dist/derived/derived-sql.js +1 -47
- package/dist/functions/define-function.d.ts +10 -4
- package/dist/functions/routes.js +5 -3
- package/dist/functions/supabase-routes.js +7 -5
- package/dist/history/history-module.js +34 -0
- package/dist/index.d.ts +19 -28
- package/dist/index.js +11 -21
- package/dist/internal.d.ts +30 -10
- package/dist/internal.js +26 -8
- package/dist/jobs/functions-sql.js +7 -11
- package/dist/jobs/job-data.js +36 -0
- package/dist/jobs/job-types.d.ts +6 -3
- package/dist/jobs/jobs-module.d.ts +5 -0
- package/dist/jobs/jobs-module.js +84 -0
- package/dist/jobs/run-job.d.ts +1 -1
- package/dist/jobs/run-job.js +7 -5
- package/dist/jobs/runtime.d.ts +9 -4
- package/dist/jobs/runtime.js +11 -6
- package/dist/jobs/schema.d.ts +0 -11
- package/dist/jobs/schema.js +1 -59
- package/dist/jobs/steps/001-pg-boss-45.js +483 -0
- package/dist/live/notices-module.js +30 -0
- package/dist/live/notices-sql.js +2 -1
- package/dist/mail/feedback/feedback-schema.js +3 -30
- package/dist/mail/feedback/mail-feedback-module.js +37 -0
- package/dist/mail/unsubscribe/unsubscribe-schema.js +3 -21
- package/dist/mail/unsubscribe/unsubscribes-module.js +31 -0
- package/dist/migration-record/check-record.d.ts +9 -7
- package/dist/migration-record/check-record.js +12 -11
- package/dist/migration-record/module-needs.js +30 -0
- package/dist/migration-record/read-record.js +27 -15
- package/dist/migration-record/record-model.d.ts +7 -2
- package/dist/migration-record/record-model.js +14 -6
- package/dist/migration-record/startup-decision.js +18 -7
- package/dist/migration-sql/additive-statements.d.ts +4 -0
- package/dist/migration-sql/additive-statements.js +105 -0
- package/dist/notifications/devices-module.js +61 -0
- package/dist/notifications/history-module.js +50 -0
- package/dist/notifications/notifications-targets.js +23 -0
- package/dist/notifications/read-table.js +16 -0
- package/dist/notifications/schema-sql.js +1 -22
- package/dist/notifications/schema.js +1 -101
- package/dist/notifications/table-names.js +40 -0
- package/dist/organization-scope/app-scope.js +38 -0
- package/dist/organization-scope/auth-request-organization.js +71 -0
- package/dist/organization-scope/organization-tables.js +20 -0
- package/dist/organization-scope/sign-in-organization.js +21 -0
- package/dist/package.js +1 -1
- package/dist/personal-mail/shared-sender-store.js +4 -2
- package/dist/realtime/relay/realtime-relay-module.js +37 -0
- package/dist/relation-found.js +6 -0
- package/dist/relay/read-relay.js +85 -0
- package/dist/relay/realtime-members-sql.js +51 -28
- package/dist/relay/relay-module.js +53 -0
- package/dist/relay/relay-targets.js +28 -0
- package/dist/relay/session-end.js +1 -3
- package/dist/relay/sessions-sql.js +4 -27
- package/dist/rest/table-routes.js +9 -2
- package/dist/schema-modules/feature-schemas-sql.d.ts +40 -0
- package/dist/schema-modules/feature-schemas-sql.js +51 -0
- package/dist/schema-modules/module-difference.d.ts +31 -0
- package/dist/schema-modules/module-difference.js +98 -0
- package/dist/schema-modules/modules-sql.js +13 -0
- package/dist/schema-modules/modules.d.ts +37 -0
- package/dist/schema-modules/modules.js +56 -0
- package/dist/schema-modules/needed-modules.d.ts +41 -0
- package/dist/schema-modules/needed-modules.js +61 -0
- package/dist/schema-modules/resource-difference.js +83 -0
- package/dist/schema-modules/schema-module.d.ts +112 -0
- package/dist/schema-modules/schema-module.js +8 -0
- package/dist/status/status-module.js +63 -0
- package/dist/status/status-targets.js +21 -0
- package/dist/types.d.ts +6 -1
- package/dist/user-tables/reloading-admin.d.ts +1 -0
- package/dist/user-tables/reloading-admin.js +1 -0
- package/dist/user-tables/schema-sql.js +1 -14
- package/dist/user-tables/schema.js +4 -27
- package/dist/user-tables/user-tables-module.js +38 -0
- package/dist/webhooks/table/delivery-log-module.js +36 -0
- package/dist/webhooks/table/owner-roles.js +3 -2
- package/dist/webhooks/table/schema-sql.js +27 -26
- package/dist/webhooks/table/webhooks-schema-module.js +63 -0
- package/dist/webhooks/table/webhooks-targets.js +26 -0
- package/dist/webhooks/webhooks.js +1 -1
- package/dist/workflows/custom-steps/custom-steps-module.js +51 -0
- package/dist/workflows/custom-steps/custom-steps-sql.js +10 -7
- package/dist/workflows/custom-steps/runners-sql.js +10 -7
- package/dist/workflows/read-catalogue.js +23 -0
- package/dist/workflows/schema-sql.js +12 -9
- package/dist/workflows/status-sql.js +12 -6
- package/dist/workflows/storage/storage-module.js +39 -0
- package/dist/workflows/storage/storage-sql.js +8 -5
- package/dist/workflows/workflows-schema-module.js +49 -0
- package/dist/workflows/workflows-tables-sql.js +32 -0
- package/dist/workflows/workflows-targets.js +16 -0
- package/package.json +6 -6
- package/dist/assistant/query-runner/runner-plan.js +0 -30
- package/dist/better-auth/store/migrations-table.js +0 -27
- package/dist/better-auth/store/modules-off.js +0 -17
- package/dist/calls/calls-sql.d.ts +0 -11
- package/dist/changes/capture-plan.js +0 -79
- package/dist/channels/sends-schema.d.ts +0 -7
- package/dist/channels/sends-schema.js +0 -18
- package/dist/comments/comments-sql.d.ts +0 -14
- package/dist/computed/buckets-plan.d.ts +0 -2
- package/dist/computed/buckets-plan.js +0 -88
- package/dist/computed/buckets-schema.js +0 -39
- package/dist/computed/computed-plan.js +0 -81
- package/dist/computed/computed-schema.d.ts +0 -16
- package/dist/computed/computed-schema.js +0 -63
- package/dist/derived/derived-sql.d.ts +0 -12
- package/dist/mail/feedback/feedback-schema.d.ts +0 -7
- package/dist/mail/unsubscribe/unsubscribe-schema.d.ts +0 -10
- package/dist/notifications/schema.d.ts +0 -14
- package/dist/realtime/relay/relay-schema.d.ts +0 -7
- package/dist/realtime/relay/relay-schema.js +0 -20
- package/dist/relay/realtime-members-schema.js +0 -67
- package/dist/relay/relay-schema.d.ts +0 -14
- package/dist/relay/relay-schema.js +0 -28
- package/dist/status/schema.d.ts +0 -12
- package/dist/status/schema.js +0 -33
- package/dist/user-tables/schema.d.ts +0 -11
- package/dist/webhooks/table/schema.d.ts +0 -16
- package/dist/webhooks/table/schema.js +0 -56
- package/dist/workflows/schema.d.ts +0 -13
- package/dist/workflows/schema.js +0 -88
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
import { ModuleId } from "../../names/modules/index.js";
|
|
2
1
|
import { StoreNames } from "../store-names.js";
|
|
3
|
-
import { LastSteps } from "./store-model.js";
|
|
2
|
+
import { KeptModule, LastSteps } from "./store-model.js";
|
|
3
|
+
import { AppliedStep } from "../step-record.js";
|
|
4
4
|
//#region src/better-auth/store/offline/baseline-store.d.ts
|
|
5
5
|
/** A relation in the store's schema: its `pg_class.relkind` and its columns. */
|
|
6
6
|
export type LiveRelation = {
|
|
7
7
|
kind: string;
|
|
8
8
|
columns: string[];
|
|
9
9
|
};
|
|
10
|
-
/** The store as the database has it: the
|
|
10
|
+
/** The store as the database has it: the parts of steps it recorded, and every relation of its schema by name. */
|
|
11
11
|
export type LiveStore = {
|
|
12
|
-
|
|
12
|
+
applied: AppliedStep[];
|
|
13
13
|
relations: Record<string, LiveRelation>;
|
|
14
14
|
};
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* What the store is: each module's last step, the modules off that have tables, whether organizations were ever on,
|
|
17
|
+
* the relations Protobase did not make, and whether it has `auth_steps`.
|
|
18
|
+
*/
|
|
16
19
|
export type BaselineStore = {
|
|
17
20
|
lastSteps: LastSteps;
|
|
18
|
-
kept:
|
|
21
|
+
kept: KeptModule[];
|
|
19
22
|
organizationsEverOn: boolean;
|
|
20
23
|
external: string[];
|
|
24
|
+
stepRecord: boolean;
|
|
21
25
|
};
|
|
22
26
|
/**
|
|
23
27
|
* The store at `live` under the config's `names`: each module that is on at the step the record holds, every module
|
|
24
|
-
* that is off but has a table as kept, organizations as ever on when a table whose rows belong to an organization has
|
|
28
|
+
* that is off but has a table as kept at the step the record holds, organizations as ever on when a table whose rows belong to an organization has
|
|
25
29
|
* the column that names it, and the user table as external when the config reads it through a view over another
|
|
26
30
|
* system's (`users.keyTable`). Refuses a user table the database has as anything but a table without `users.keyTable`,
|
|
27
31
|
* since the steps' foreign keys would point at it, and a table a recorded step made that the schema lacks under the
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { allModules } from "../../names/modules-on.js";
|
|
2
2
|
import { resolveNames } from "../../names/resolve-names.js";
|
|
3
|
-
import { legacyVersions } from "../steps/legacy-versions.js";
|
|
4
3
|
import { moduleSteps } from "../steps/index.js";
|
|
4
|
+
import { refuseUnshipped } from "../step-record.js";
|
|
5
|
+
import { keptModule } from "./store-model.js";
|
|
5
6
|
//#region src/better-auth/store/offline/baseline-store.ts
|
|
6
7
|
const tableKinds = ["r", "p"];
|
|
7
8
|
const relationOf = (live, name) => Object.hasOwn(live.relations, name) ? live.relations[name] : void 0;
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Each module's last step whose own part, and that of every step before it in its chain, the store recorded. Refuses a
|
|
11
|
+
* recorded step this version does not ship, which the prefix would otherwise leave out unseen.
|
|
12
|
+
*/
|
|
13
|
+
const recordedSteps = (applied) => {
|
|
14
|
+
refuseUnshipped(applied);
|
|
13
15
|
const steps = {};
|
|
14
16
|
for (const module of allModules) {
|
|
15
|
-
const had = moduleSteps[module].findIndex((step) => !
|
|
17
|
+
const had = moduleSteps[module].findIndex((step) => !applied.some((part) => part.module === module && part.step === step.step && part.target === module));
|
|
16
18
|
const count = had === -1 ? moduleSteps[module].length : had;
|
|
17
19
|
if (count > 0) steps[module] = moduleSteps[module][count - 1].step;
|
|
18
20
|
}
|
|
@@ -20,14 +22,14 @@ const recordedSteps = (versions) => {
|
|
|
20
22
|
};
|
|
21
23
|
/**
|
|
22
24
|
* The store at `live` under the config's `names`: each module that is on at the step the record holds, every module
|
|
23
|
-
* that is off but has a table as kept, organizations as ever on when a table whose rows belong to an organization has
|
|
25
|
+
* that is off but has a table as kept at the step the record holds, organizations as ever on when a table whose rows belong to an organization has
|
|
24
26
|
* the column that names it, and the user table as external when the config reads it through a view over another
|
|
25
27
|
* system's (`users.keyTable`). Refuses a user table the database has as anything but a table without `users.keyTable`,
|
|
26
28
|
* since the steps' foreign keys would point at it, and a table a recorded step made that the schema lacks under the
|
|
27
29
|
* config's name, such as one renamed since, naming each.
|
|
28
30
|
*/
|
|
29
31
|
const baselineStore = (names, live) => {
|
|
30
|
-
const recorded = recordedSteps(live.
|
|
32
|
+
const recorded = recordedSteps(live.applied);
|
|
31
33
|
const on = names.names.modules;
|
|
32
34
|
const every = resolveNames({
|
|
33
35
|
...names.names.input,
|
|
@@ -43,9 +45,9 @@ const baselineStore = (names, live) => {
|
|
|
43
45
|
const missing = [...lastSteps.auth !== void 0 && !externalUser ? ["user"] : [], ...made].filter((internal) => !has(internal));
|
|
44
46
|
if (missing.length > 0) {
|
|
45
47
|
const listed = missing.map((internal) => every.tables[internal].name).join(", ");
|
|
46
|
-
throw new Error(`The auth store's schema lacks ${listed}, which the
|
|
48
|
+
throw new Error(`The auth store's schema lacks ${listed}, which the steps the store recorded made: name each as the store has it in auth.tables, or apply the project's migrations first`);
|
|
47
49
|
}
|
|
48
|
-
const kept = allModules.filter((module) => !on.includes(module) && Object.values(every.tables).some((table) => table.module === module && has(table.internal)));
|
|
50
|
+
const kept = allModules.filter((module) => !on.includes(module) && Object.values(every.tables).some((table) => table.module === module && has(table.internal))).map((module) => keptModule(names, module, recorded[module] ?? 0));
|
|
49
51
|
const organizationsEverOn = Object.values(every.tables).some((table) => {
|
|
50
52
|
const column = table.columns.organization_id;
|
|
51
53
|
return table.scope === "organization" && column !== void 0 && (relationOf(live, table.name)?.columns.includes(column.name) ?? false);
|
|
@@ -55,7 +57,8 @@ const baselineStore = (names, live) => {
|
|
|
55
57
|
lastSteps,
|
|
56
58
|
kept,
|
|
57
59
|
organizationsEverOn,
|
|
58
|
-
external: externalUser ? [`${prefix}${user.name}`] : []
|
|
60
|
+
external: externalUser ? [`${prefix}${user.name}`] : [],
|
|
61
|
+
stepRecord: has("auth_steps")
|
|
59
62
|
};
|
|
60
63
|
};
|
|
61
64
|
//#endregion
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RawBuilder } from "kysely";
|
|
2
|
+
//#region src/better-auth/store/offline/contract-gate.d.ts
|
|
3
|
+
/** The line that marks the statement under it as taking what the migration `id` stopped older code from using. */
|
|
4
|
+
export declare const contractMark: (id: string) => string;
|
|
5
|
+
/** `statements` as SQL text, each under the mark naming the migration `id`. */
|
|
6
|
+
export declare const markedSql: (id: string, statements: readonly RawBuilder<unknown>[]) => string;
|
|
7
|
+
/** A migration a sign-off waits on: its id and file name, as `migrations.history` records them. */
|
|
8
|
+
export type SignedOff = {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The block that refuses, unless `migrations.history` has `after`'s row, `hours` have passed since it was applied,
|
|
14
|
+
* and, when `followed`, a row applied after it: what `step` says it does waits for the update to have served that long,
|
|
15
|
+
* and for the next one to have followed it.
|
|
16
|
+
*/
|
|
17
|
+
export declare const signOffGate: (after: SignedOff, hours: number, step: string, followed: boolean) => RawBuilder<unknown>;
|
|
18
|
+
//#endregion
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ddl, ddlText } from "../ddl.js";
|
|
2
|
+
import { recordTable } from "../../../migration-record/record-table.js";
|
|
3
|
+
import { sql } from "kysely";
|
|
4
|
+
//#region src/better-auth/store/offline/contract-gate.ts
|
|
5
|
+
/** The line that marks the statement under it as taking what the migration `id` stopped older code from using. */
|
|
6
|
+
const contractMark = (id) => `-- protobase:contract ${id}`;
|
|
7
|
+
/** `statements` as SQL text, each under the mark naming the migration `id`. */
|
|
8
|
+
const markedSql = (id, statements) => statements.map((statement) => `${contractMark(id)}\n${ddlText(statement)};`).join("\n\n");
|
|
9
|
+
/**
|
|
10
|
+
* The block that refuses, unless `migrations.history` has `after`'s row, `hours` have passed since it was applied,
|
|
11
|
+
* and, when `followed`, a row applied after it: what `step` says it does waits for the update to have served that long,
|
|
12
|
+
* and for the next one to have followed it.
|
|
13
|
+
*/
|
|
14
|
+
const signOffGate = (after, hours, step, followed) => {
|
|
15
|
+
const waits = `${step} waits until ${after.name}`;
|
|
16
|
+
const next = ddl`
|
|
17
|
+
if not exists (select from ${sql.raw(recordTable)} where applied_at > applied) then
|
|
18
|
+
raise exception '%', ${sql.lit(`${waits} is followed by a migration applied after it, as the next update applies one`)};
|
|
19
|
+
end if;`;
|
|
20
|
+
return ddl`do $gate$
|
|
21
|
+
declare
|
|
22
|
+
applied timestamptz;
|
|
23
|
+
begin
|
|
24
|
+
select applied_at into applied from ${sql.raw(recordTable)} where id = ${sql.lit(after.id)};
|
|
25
|
+
if applied is null then
|
|
26
|
+
raise exception '%', ${sql.lit(`${waits} is applied`)};
|
|
27
|
+
end if;${followed ? next : sql``}
|
|
28
|
+
if now() - applied < ${sql.lit(`${hours} hours`)}::interval then
|
|
29
|
+
raise exception '%', format(${sql.lit(`${waits} was applied ${hours} hours ago; it was applied at %s`)}, applied);
|
|
30
|
+
end if;
|
|
31
|
+
end
|
|
32
|
+
$gate$`;
|
|
33
|
+
};
|
|
34
|
+
//#endregion
|
|
35
|
+
export { contractMark, markedSql, signOffGate };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ModuleId } from "../../names/modules/index.js";
|
|
2
|
+
import { DeferredOff, KeptModule, RetiredName } from "./store-model.js";
|
|
3
|
+
//#region src/better-auth/store/offline/module-archive.d.ts
|
|
4
|
+
/** A module archived: where its tables went, the columns it retired, the choice made, and the migration that archived it. */
|
|
5
|
+
export type ArchivedModule = {
|
|
6
|
+
module: ModuleId;
|
|
7
|
+
step: number;
|
|
8
|
+
/** The store's schema, which the retired columns' tables are in; `null` when the search path decides. */
|
|
9
|
+
store: string | null;
|
|
10
|
+
/** The schema its tables moved to (`archiveSchema`), and their names there. */
|
|
11
|
+
schema: string;
|
|
12
|
+
tables: string[];
|
|
13
|
+
columns: RetiredName[];
|
|
14
|
+
choice: DeferredOff['choice'];
|
|
15
|
+
/** The id of the migration that archived it, which the one that deletes it names. */
|
|
16
|
+
file: string;
|
|
17
|
+
};
|
|
18
|
+
/** The schema the migration numbered `number` archives tables to. */
|
|
19
|
+
export declare const archiveSchema: (number: number) => string;
|
|
20
|
+
/** The name a retired column takes once archived; one past 63 bytes is shortened as Postgres would not. */
|
|
21
|
+
export declare const archivedColumn: (column: string) => string;
|
|
22
|
+
/**
|
|
23
|
+
* The statements that archive `kept` from the store's schema `store` to `schema`: the watch's schema, table and
|
|
24
|
+
* function where missing, the foreign keys between its tables and the others dropped, its tables moved, each watched
|
|
25
|
+
* as a whole, and each column it retired renamed to its archive and watched under it, counted as the column.
|
|
26
|
+
*/
|
|
27
|
+
export declare const archiveStatements: (store: string | null, schema: string, kept: KeptModule & {
|
|
28
|
+
off: DeferredOff;
|
|
29
|
+
}) => import("kysely").RawBuilder<unknown>[];
|
|
30
|
+
/**
|
|
31
|
+
* The statements that take from every role that owns or holds a privilege on a table of `schema`, one of its columns
|
|
32
|
+
* or the schema itself, and from everyone, the use of its tables and sequences, then of the schema, the role applying
|
|
33
|
+
* the migration included, each with the grants made from them: written once the file has moved every table there,
|
|
34
|
+
* which needs the schema. The schema goes last, as revoking on its tables looks it up.
|
|
35
|
+
*/
|
|
36
|
+
export declare const revokeStatements: (schema: string) => import("kysely").RawBuilder<unknown>[];
|
|
37
|
+
/** The snapshot's record of `kept` once archived to `schema` by the migration `file`. */
|
|
38
|
+
export declare const archivedModule: (store: string | null, schema: string, kept: KeptModule & {
|
|
39
|
+
off: DeferredOff;
|
|
40
|
+
}, file: string) => ArchivedModule;
|
|
41
|
+
//#endregion
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { generatedName } from "../../names/identifier-length.js";
|
|
2
|
+
import { columnId, tableId } from "../../names/identifiers.js";
|
|
3
|
+
import { ddl } from "../ddl.js";
|
|
4
|
+
import { retiredSchema, watchBaseStatements, watchTrigger, watchTriggerName } from "./retired-watch.js";
|
|
5
|
+
import { sql } from "kysely";
|
|
6
|
+
//#region src/better-auth/store/offline/module-archive.ts
|
|
7
|
+
/** The schema the migration numbered `number` archives tables to. */
|
|
8
|
+
const archiveSchema = (number) => `protobase_archive_${number}`;
|
|
9
|
+
/** The name a retired column takes once archived; one past 63 bytes is shortened as Postgres would not. */
|
|
10
|
+
const archivedColumn = (column) => generatedName(`${column}_archive`);
|
|
11
|
+
/** The trigger that counts the writes of an archived table. */
|
|
12
|
+
const tableWatchTrigger = "retired_writes";
|
|
13
|
+
const qualified = (store, table) => tableId(store ?? void 0, table);
|
|
14
|
+
/** `tables` as an array of `regclass`, for a block to compare with the catalogue. */
|
|
15
|
+
const regclasses = (tables) => ddl`array[${sql.join(tables.map((table) => sql.lit(String(table))))}]::regclass[]`;
|
|
16
|
+
const dropCrossingKeys = (tables) => ddl`do $archive$
|
|
17
|
+
declare
|
|
18
|
+
found record;
|
|
19
|
+
begin
|
|
20
|
+
for found in select conrelid::regclass as relation, conname from pg_constraint where contype = 'f' and (conrelid = any(${tables})) <> (confrelid = any(${tables})) loop
|
|
21
|
+
execute format('alter table %s drop constraint %I', found.relation, found.conname);
|
|
22
|
+
end loop;
|
|
23
|
+
end
|
|
24
|
+
$archive$`;
|
|
25
|
+
/**
|
|
26
|
+
* The statements that archive `kept` from the store's schema `store` to `schema`: the watch's schema, table and
|
|
27
|
+
* function where missing, the foreign keys between its tables and the others dropped, its tables moved, each watched
|
|
28
|
+
* as a whole, and each column it retired renamed to its archive and watched under it, counted as the column.
|
|
29
|
+
*/
|
|
30
|
+
const archiveStatements = (store, schema, kept) => {
|
|
31
|
+
const tables = Object.values(kept.tables).map(({ name }) => name);
|
|
32
|
+
return [
|
|
33
|
+
...watchBaseStatements(),
|
|
34
|
+
ddl`create schema if not exists ${schema}`,
|
|
35
|
+
dropCrossingKeys(regclasses(tables.map((table) => qualified(store, table)))),
|
|
36
|
+
...tables.map((table) => ddl`alter table ${qualified(store, table)} set schema ${schema}`),
|
|
37
|
+
...tables.map((table) => ddl`create trigger ${tableWatchTrigger} after insert or update or delete on ${tableId(schema, table)} for each row execute function ${retiredSchema}.${"count_write"}(${sql.lit("*")})`),
|
|
38
|
+
...kept.off.columns.flatMap(({ table, column }) => {
|
|
39
|
+
const at = qualified(store, table);
|
|
40
|
+
const archived = columnId(at, archivedColumn(column));
|
|
41
|
+
return [
|
|
42
|
+
ddl`drop trigger if exists ${watchTriggerName(column)} on ${at}`,
|
|
43
|
+
ddl`alter table ${at} rename column ${column} to ${archived}`,
|
|
44
|
+
watchTrigger(at, columnId(at, column), archived)
|
|
45
|
+
];
|
|
46
|
+
})
|
|
47
|
+
];
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* The statements that take from every role that owns or holds a privilege on a table of `schema`, one of its columns
|
|
51
|
+
* or the schema itself, and from everyone, the use of its tables and sequences, then of the schema, the role applying
|
|
52
|
+
* the migration included, each with the grants made from them: written once the file has moved every table there,
|
|
53
|
+
* which needs the schema. The schema goes last, as revoking on its tables looks it up.
|
|
54
|
+
*/
|
|
55
|
+
const revokeStatements = (schema) => {
|
|
56
|
+
const at = sql.lit(schema);
|
|
57
|
+
return [ddl`do $revoke$
|
|
58
|
+
declare
|
|
59
|
+
holders text[];
|
|
60
|
+
holder text;
|
|
61
|
+
begin
|
|
62
|
+
select array_agg(distinct found.role::regrole::text) into holders from (
|
|
63
|
+
select relowner as role from pg_class where relnamespace = ${at}::regnamespace
|
|
64
|
+
union all
|
|
65
|
+
select privilege.grantee from pg_class, aclexplode(relacl) as privilege where relnamespace = ${at}::regnamespace and privilege.grantee <> 0
|
|
66
|
+
union all
|
|
67
|
+
select privilege.grantee from pg_class join pg_attribute on attrelid = pg_class.oid, aclexplode(attacl) as privilege where relnamespace = ${at}::regnamespace and privilege.grantee <> 0
|
|
68
|
+
union all
|
|
69
|
+
select privilege.grantee from pg_namespace, aclexplode(nspacl) as privilege where pg_namespace.oid = ${at}::regnamespace and privilege.grantee <> 0
|
|
70
|
+
) as found;
|
|
71
|
+
foreach holder in array coalesce(holders, '{}') loop
|
|
72
|
+
execute format('revoke all on all tables in schema %I from %s cascade', ${at}, holder);
|
|
73
|
+
execute format('revoke all on all sequences in schema %I from %s cascade', ${at}, holder);
|
|
74
|
+
end loop;
|
|
75
|
+
execute format('revoke all on all tables in schema %I from public cascade', ${at});
|
|
76
|
+
execute format('revoke all on all sequences in schema %I from public cascade', ${at});
|
|
77
|
+
foreach holder in array coalesce(holders, '{}') loop
|
|
78
|
+
execute format('revoke all on schema %I from %s cascade', ${at}, holder);
|
|
79
|
+
end loop;
|
|
80
|
+
end
|
|
81
|
+
$revoke$`, ddl`revoke all on schema ${schema} from public, current_user cascade`];
|
|
82
|
+
};
|
|
83
|
+
/** The snapshot's record of `kept` once archived to `schema` by the migration `file`. */
|
|
84
|
+
const archivedModule = (store, schema, kept, file) => ({
|
|
85
|
+
module: kept.module,
|
|
86
|
+
step: kept.step,
|
|
87
|
+
store,
|
|
88
|
+
schema,
|
|
89
|
+
tables: Object.values(kept.tables).map(({ name }) => name),
|
|
90
|
+
columns: kept.off.columns,
|
|
91
|
+
choice: kept.off.choice,
|
|
92
|
+
file
|
|
93
|
+
});
|
|
94
|
+
//#endregion
|
|
95
|
+
export { archiveSchema, archiveStatements, archivedColumn, archivedModule, regclasses, revokeStatements, tableWatchTrigger };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { TableId } from "../../names/identifiers.js";
|
|
2
|
+
import { ModuleId } from "../../names/modules/index.js";
|
|
3
|
+
import { StoreModel } from "./store-model.js";
|
|
4
|
+
import { ArchivedModule } from "./module-archive.js";
|
|
5
|
+
//#region src/better-auth/store/offline/module-delete.d.ts
|
|
6
|
+
/** The table that records the store's steps, with its module and target columns, by their names. */
|
|
7
|
+
export type StepRecord = {
|
|
8
|
+
table: TableId;
|
|
9
|
+
module: string;
|
|
10
|
+
target: string;
|
|
11
|
+
};
|
|
12
|
+
/** The step record of the store `model` describes; `undefined` for one without it. */
|
|
13
|
+
export declare const stepRecordOf: (model: StoreModel) => StepRecord | undefined;
|
|
14
|
+
/** `forgetSteps` as SQL text. */
|
|
15
|
+
export declare const forgetStepsSql: (steps: StepRecord, id: ModuleId) => string;
|
|
16
|
+
/**
|
|
17
|
+
* The statements that delete `entry` for good once nothing wrote it for `hours` (`unwrittenCheck`): the use of its
|
|
18
|
+
* schema taken back for the role applying it, its tables dropped, and the schema once nothing else is in it, then each
|
|
19
|
+
* column archive with its watch; and with `steps`, while the module is still off, its rows in `auth_steps`.
|
|
20
|
+
*/
|
|
21
|
+
export declare const deleteStatements: (entry: ArchivedModule, hours: number, steps: StepRecord | undefined) => import("kysely").RawBuilder<unknown>[];
|
|
22
|
+
//#endregion
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { columnId, tableId } from "../../names/identifiers.js";
|
|
2
|
+
import { ddl } from "../ddl.js";
|
|
3
|
+
import { statementsSql } from "../migration-sql.js";
|
|
4
|
+
import { retiredSchema, watchTriggerName } from "./retired-watch.js";
|
|
5
|
+
import { stillWrittenTargets } from "./module-switches.js";
|
|
6
|
+
import { archivedColumn, tableWatchTrigger } from "./module-archive.js";
|
|
7
|
+
import { sql } from "kysely";
|
|
8
|
+
//#region src/better-auth/store/offline/module-delete.ts
|
|
9
|
+
const watched = (entry) => [...entry.tables.map((table) => ({
|
|
10
|
+
table: tableId(entry.schema, table),
|
|
11
|
+
schema: sql.lit(entry.schema),
|
|
12
|
+
name: table,
|
|
13
|
+
column: "*",
|
|
14
|
+
trigger: tableWatchTrigger
|
|
15
|
+
})), ...entry.columns.map(({ table, column }) => ({
|
|
16
|
+
table: tableId(entry.store ?? void 0, table),
|
|
17
|
+
schema: entry.store === null ? sql.raw("current_schema()") : sql.lit(entry.store),
|
|
18
|
+
name: table,
|
|
19
|
+
column,
|
|
20
|
+
trigger: watchTriggerName(archivedColumn(column))
|
|
21
|
+
}))];
|
|
22
|
+
/** The message a delete refuses with while the watch counts a write of `entry` within the last `hours`. */
|
|
23
|
+
const stillWritten = (entry, hours) => `${entry.module} was written within the last ${hours} hours, which retired.writes counts: %s. Find what still writes it, and delete it once nothing has for that long`;
|
|
24
|
+
/**
|
|
25
|
+
* The block that refuses unless every table and column archive of `entry` is watched, by a trigger that fires for the
|
|
26
|
+
* app's writes (enabled, or always), and the watch counts no write of any within the last `hours`: a write names each
|
|
27
|
+
* one written and its count.
|
|
28
|
+
*/
|
|
29
|
+
const unwrittenCheck = (entry, hours) => {
|
|
30
|
+
const parts = watched(entry);
|
|
31
|
+
const triggers = parts.map(({ table, trigger }) => ddl`exists (select from pg_trigger where tgrelid = ${sql.lit(String(table))}::regclass and tgname = ${sql.lit(trigger)} and tgenabled in ('O', 'A'))`);
|
|
32
|
+
const keys = parts.map(({ schema, name, column }) => ddl`(format('%I.%I', ${schema}, ${sql.lit(name)}), ${sql.lit(column)})`);
|
|
33
|
+
return ddl`do $unwritten$
|
|
34
|
+
declare
|
|
35
|
+
found text;
|
|
36
|
+
begin
|
|
37
|
+
if to_regclass(${sql.lit(`${retiredSchema}.writes`)}) is null or not (${sql.join(triggers, sql` and `)}) then
|
|
38
|
+
raise exception '%', ${sql.lit(`${entry.module} has a table or column archive that retired.writes does not watch, so whether something still writes it is unknown: it is not deleted`)};
|
|
39
|
+
end if;
|
|
40
|
+
select string_agg(counted.table_name || '.' || counted.column_name || ' ' || counted.writes, ', ' order by counted.table_name, counted.column_name) into found
|
|
41
|
+
from (
|
|
42
|
+
select table_name, column_name, sum(writes) as writes from ${sql.raw(retiredSchema)}.writes
|
|
43
|
+
where hour >= date_trunc('hour', now() - ${sql.lit(`${hours} hours`)}::interval) and (table_name, column_name) in (${sql.join(keys)})
|
|
44
|
+
group by table_name, column_name
|
|
45
|
+
) as counted;
|
|
46
|
+
if found is not null then
|
|
47
|
+
raise exception '%', format(${sql.lit(stillWritten(entry, hours))}, found);
|
|
48
|
+
end if;
|
|
49
|
+
end
|
|
50
|
+
$unwritten$`;
|
|
51
|
+
};
|
|
52
|
+
/** The step record of the store `model` describes; `undefined` for one without it. */
|
|
53
|
+
const stepRecordOf = (model) => {
|
|
54
|
+
const table = model.modules.auth?.tables.auth_steps;
|
|
55
|
+
if (!table) return;
|
|
56
|
+
return {
|
|
57
|
+
table: tableId(model.schema ?? void 0, table.name),
|
|
58
|
+
module: table.columns.module,
|
|
59
|
+
target: table.columns.target
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* The statement that forgets the steps of the module `id` and those applied to its tables, so it starts again from its
|
|
64
|
+
* first, but its parts on the tables of the modules whose columns it leaves in use while off (`stillWrittenTargets`),
|
|
65
|
+
* which stay as they are and are not applied again.
|
|
66
|
+
*/
|
|
67
|
+
const forgetSteps = ({ table, module, target }, id) => {
|
|
68
|
+
const kept = stillWrittenTargets(id);
|
|
69
|
+
const own = ddl`${columnId(table, module)} = ${sql.lit(id)}`;
|
|
70
|
+
const parts = kept.length === 0 ? own : ddl`(${own} and ${columnId(table, target)} not in (${sql.join(kept.map((other) => sql.lit(other)))}))`;
|
|
71
|
+
return ddl`delete from ${table} where ${parts} or ${columnId(table, target)} = ${sql.lit(id)}`;
|
|
72
|
+
};
|
|
73
|
+
/** `forgetSteps` as SQL text. */
|
|
74
|
+
const forgetStepsSql = (steps, id) => statementsSql([forgetSteps(steps, id)]);
|
|
75
|
+
/**
|
|
76
|
+
* The statements that delete `entry` for good once nothing wrote it for `hours` (`unwrittenCheck`): the use of its
|
|
77
|
+
* schema taken back for the role applying it, its tables dropped, and the schema once nothing else is in it, then each
|
|
78
|
+
* column archive with its watch; and with `steps`, while the module is still off, its rows in `auth_steps`.
|
|
79
|
+
*/
|
|
80
|
+
const deleteStatements = (entry, hours, steps) => [
|
|
81
|
+
ddl`grant usage on schema ${entry.schema} to current_user`,
|
|
82
|
+
unwrittenCheck(entry, hours),
|
|
83
|
+
ddl`drop table ${sql.join(entry.tables.map((table) => tableId(entry.schema, table)))}`,
|
|
84
|
+
ddl`do $empty$
|
|
85
|
+
begin
|
|
86
|
+
if not exists (select from pg_class where relnamespace = ${sql.lit(entry.schema)}::regnamespace) then
|
|
87
|
+
execute format('drop schema %I', ${sql.lit(entry.schema)});
|
|
88
|
+
end if;
|
|
89
|
+
end
|
|
90
|
+
$empty$`,
|
|
91
|
+
...entry.columns.flatMap(({ table, column }) => {
|
|
92
|
+
const at = tableId(entry.store ?? void 0, table);
|
|
93
|
+
const archived = archivedColumn(column);
|
|
94
|
+
return [ddl`drop trigger if exists ${watchTriggerName(archived)} on ${at}`, ddl`alter table ${at} drop column ${columnId(at, archived)}`];
|
|
95
|
+
}),
|
|
96
|
+
...steps ? [forgetSteps(steps, entry.module)] : []
|
|
97
|
+
];
|
|
98
|
+
//#endregion
|
|
99
|
+
export { deleteStatements, forgetSteps, forgetStepsSql, stepRecordOf, stillWritten, unwrittenCheck };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ModuleId } from "../../names/modules/index.js";
|
|
2
|
+
import { ResolvedTable } from "../../names/resolve-names.js";
|
|
3
|
+
import { StoreNames } from "../store-names.js";
|
|
4
|
+
import "./retired-watch.js";
|
|
5
|
+
//#region src/better-auth/store/offline/module-switches.d.ts
|
|
6
|
+
/** What becomes of a module's tables once it is off. */
|
|
7
|
+
export declare const offChoices: readonly ["keep", "archive", "delete"];
|
|
8
|
+
export type OffChoice = (typeof offChoices)[number];
|
|
9
|
+
/** The setting `protobase migrate --force` sets in its transactions, which lets a module whose kept tables hold rows on again. */
|
|
10
|
+
export declare const forceSetting = "protobase.force";
|
|
11
|
+
/** The columns `module` retires (`retiredColumns`) by the names of their tables and themselves, as a snapshot records them. */
|
|
12
|
+
export declare const retiredNames: (names: StoreNames, module: ModuleId, existing: readonly ModuleId[], externalUser?: boolean) => {
|
|
13
|
+
table: string;
|
|
14
|
+
column: string;
|
|
15
|
+
}[];
|
|
16
|
+
/** The tables of `module` its steps up to `step` made, under the store's names. */
|
|
17
|
+
export declare const keptTables: (names: StoreNames, module: ModuleId, step: number) => ResolvedTable<string>[];
|
|
18
|
+
/** The message that refuses turning `module` on again while its kept tables hold rows: `found` lists each table with its count. */
|
|
19
|
+
export declare const revivalRefusal: (module: ModuleId, found: string) => string;
|
|
20
|
+
/**
|
|
21
|
+
* The block that refuses, before the steps, to turn `module` on again while the tables it kept at `step` hold rows,
|
|
22
|
+
* naming each table with its count; `protobase migrate --force` lets it through. Empty tables pass.
|
|
23
|
+
*/
|
|
24
|
+
export declare const revivalCheckSql: (names: StoreNames, module: ModuleId, step: number) => string;
|
|
25
|
+
/** The statements that keep `module` while off, as SQL text under a comment naming it; none when there are none. */
|
|
26
|
+
export declare const keepSql: (names: StoreNames, module: ModuleId, existing: readonly ModuleId[], externalUser?: boolean) => string;
|
|
27
|
+
/**
|
|
28
|
+
* The statements that stop the watch on the columns `module` retired, as it turns on again, whose values they kept;
|
|
29
|
+
* none when it retired none.
|
|
30
|
+
*/
|
|
31
|
+
export declare const unwatchSql: (names: StoreNames, module: ModuleId, existing: readonly ModuleId[], externalUser?: boolean) => string | undefined;
|
|
32
|
+
//#endregion
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { builtInModules } from "../../names/modules/index.js";
|
|
2
|
+
import { allModules } from "../../names/modules-on.js";
|
|
3
|
+
import { resolveNames } from "../../names/resolve-names.js";
|
|
4
|
+
import { ddl } from "../ddl.js";
|
|
5
|
+
import { moduleSteps } from "../steps/index.js";
|
|
6
|
+
import { statementsSql } from "../migration-sql.js";
|
|
7
|
+
import { unwatchStatements, watchStatements } from "./retired-watch.js";
|
|
8
|
+
import { sql } from "kysely";
|
|
9
|
+
//#region src/better-auth/store/offline/module-switches.ts
|
|
10
|
+
/** What becomes of a module's tables once it is off. */
|
|
11
|
+
const offChoices = [
|
|
12
|
+
"keep",
|
|
13
|
+
"archive",
|
|
14
|
+
"delete"
|
|
15
|
+
];
|
|
16
|
+
/** The setting `protobase migrate --force` sets in its transactions, which lets a module whose kept tables hold rows on again. */
|
|
17
|
+
const forceSetting = "protobase.force";
|
|
18
|
+
const everyTable = (names) => resolveNames({
|
|
19
|
+
...names.names.input,
|
|
20
|
+
modules: allModules
|
|
21
|
+
}).tables;
|
|
22
|
+
const writtenWhileOff = { organizations: { personal_access_token: { organization_id: "organization" } } };
|
|
23
|
+
/** The modules on whose tables `module` adds columns that stay written while it is off (`writtenWhileOff`), which its archive leaves as they are. */
|
|
24
|
+
const stillWrittenTargets = (module) => Object.keys(writtenWhileOff[module] ?? {}).map((table) => builtInModules.find((declaration) => table in declaration.tables).id);
|
|
25
|
+
/**
|
|
26
|
+
* The columns `module` adds that stay written while it is off, on the tables of `existing`, the modules the store has
|
|
27
|
+
* before the migration, each with the table its foreign key points at, under the store's names.
|
|
28
|
+
*/
|
|
29
|
+
const stillWrittenColumns = (names, module, existing) => {
|
|
30
|
+
const every = everyTable(names);
|
|
31
|
+
return Object.entries(writtenWhileOff[module] ?? {}).filter(([table]) => existing.includes(every[table].module)).flatMap(([table, columns]) => Object.entries(columns).map(([column, references]) => ({
|
|
32
|
+
table: every[table],
|
|
33
|
+
column: every[table].columns[column],
|
|
34
|
+
references: every[references]
|
|
35
|
+
})));
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* The columns `module` adds to the tables of the modules that stay on, which retire once it is off: each table one of
|
|
39
|
+
* `existing`, whose tables the store has before the migration; a column the server still writes while it is off stays
|
|
40
|
+
* in use (`writtenWhileOff`). A table of a module turned on in the same migration is made without the column. The user
|
|
41
|
+
* table is left alone when it is a relation Protobase did not make.
|
|
42
|
+
*/
|
|
43
|
+
const retiredColumns = (names, module, existing, externalUser = names.users.keyTable !== void 0) => {
|
|
44
|
+
const every = everyTable(names);
|
|
45
|
+
const declaration = builtInModules.find(({ id }) => id === module);
|
|
46
|
+
return Object.entries(declaration.columns ?? {}).filter(([table]) => {
|
|
47
|
+
const kept = names.names.tables[table];
|
|
48
|
+
return kept !== void 0 && existing.includes(kept.module) && !(table === "user" && externalUser);
|
|
49
|
+
}).flatMap(([table, columns]) => Object.keys(columns).filter((column) => !(column in (writtenWhileOff[module]?.[table] ?? {}))).map((column) => ({
|
|
50
|
+
table: every[table],
|
|
51
|
+
column: every[table].columns[column]
|
|
52
|
+
})));
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* The statements that keep `module`'s tables and rows while it is off: each column it retires (`retiredColumns`) loses
|
|
56
|
+
* `not null`, so the code that no longer writes it still inserts rows, and is watched for the writes of the code that
|
|
57
|
+
* still does (`watchStatements`).
|
|
58
|
+
*/
|
|
59
|
+
const keepStatements = (names, module, existing, externalUser) => {
|
|
60
|
+
const columns = retiredColumns(names, module, existing, externalUser);
|
|
61
|
+
return [...columns.map(({ table, column }) => ddl`alter table ${table} alter column ${column} drop not null`), ...watchStatements(columns)];
|
|
62
|
+
};
|
|
63
|
+
/** The columns `module` retires (`retiredColumns`) by the names of their tables and themselves, as a snapshot records them. */
|
|
64
|
+
const retiredNames = (names, module, existing, externalUser) => retiredColumns(names, module, existing, externalUser).map(({ table, column }) => ({
|
|
65
|
+
table: table.name,
|
|
66
|
+
column: column.name
|
|
67
|
+
}));
|
|
68
|
+
/** The tables of `module` its steps up to `step` made, under the store's names. */
|
|
69
|
+
const keptTables = (names, module, step) => {
|
|
70
|
+
const every = everyTable(names);
|
|
71
|
+
return moduleSteps[module].filter((made) => made.step <= step).flatMap((made) => made.tables.map((table) => every[table]));
|
|
72
|
+
};
|
|
73
|
+
/** The message that refuses turning `module` on again while its kept tables hold rows: `found` lists each table with its count. */
|
|
74
|
+
const revivalRefusal = (module, found) => `The ${module} module was kept while off, and its tables still hold rows: ${found}. Turning it on again brings them back into use, such as personal access tokens, which act again for their users: delete them in a migration of the project's own written before this one, or apply with \`protobase migrate --force\` to bring them back`;
|
|
75
|
+
/**
|
|
76
|
+
* The block that refuses, before the steps, to turn `module` on again while the tables it kept at `step` hold rows,
|
|
77
|
+
* naming each table with its count; `protobase migrate --force` lets it through. Empty tables pass.
|
|
78
|
+
*/
|
|
79
|
+
const revivalCheckSql = (names, module, step) => {
|
|
80
|
+
const counts = keptTables(names, module, step).map((table) => ddl`select ${sql.lit(table.name)} as name, (select count(*) from ${table}) as rows`);
|
|
81
|
+
const check = ddl`do $revive$
|
|
82
|
+
declare
|
|
83
|
+
found text;
|
|
84
|
+
begin
|
|
85
|
+
if coalesce(current_setting(${sql.lit(forceSetting)}, true), '') <> 'true' then
|
|
86
|
+
select string_agg(counted.name || ' ' || counted.rows, ', ' order by counted.name) into found
|
|
87
|
+
from (${sql.join(counts, sql` union all `)}) as counted
|
|
88
|
+
where counted.rows > 0;
|
|
89
|
+
if found is not null then
|
|
90
|
+
raise exception '%', format(${sql.lit(revivalRefusal(module, "%s"))}, found);
|
|
91
|
+
end if;
|
|
92
|
+
end if;
|
|
93
|
+
end
|
|
94
|
+
$revive$`;
|
|
95
|
+
return `-- ${module} on again: its kept tables must be empty, or the migration applied with \`protobase migrate --force\`.\n${statementsSql([check])}`;
|
|
96
|
+
};
|
|
97
|
+
/** The statements that keep `module` while off, as SQL text under a comment naming it; none when there are none. */
|
|
98
|
+
const keepSql = (names, module, existing, externalUser) => {
|
|
99
|
+
const statements = keepStatements(names, module, existing, externalUser);
|
|
100
|
+
if (statements.length === 0) return `-- ${module} off: its tables and rows are kept as they are.`;
|
|
101
|
+
return `-- ${module} off: its tables and rows are kept, and the columns it adds to the tables of the modules that are on may stay empty, watched for writes in retired.writes.\n${statementsSql(statements)}`;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* The statements that stop the watch on the columns `module` retired, as it turns on again, whose values they kept;
|
|
105
|
+
* none when it retired none.
|
|
106
|
+
*/
|
|
107
|
+
const unwatchSql = (names, module, existing, externalUser) => {
|
|
108
|
+
const statements = unwatchStatements(retiredColumns(names, module, existing, externalUser));
|
|
109
|
+
if (statements.length === 0) return;
|
|
110
|
+
return `-- ${module} on again: the columns it adds are in use again, with the values they kept, and no longer watched.\n${statementsSql(statements)}`;
|
|
111
|
+
};
|
|
112
|
+
//#endregion
|
|
113
|
+
export { forceSetting, keepSql, keepStatements, keptTables, offChoices, retiredColumns, retiredNames, revivalCheckSql, revivalRefusal, stillWrittenColumns, stillWrittenTargets, unwatchSql };
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { StoreModel } from "./store-model.js";
|
|
2
2
|
//#region src/better-auth/store/offline/name-changes.d.ts
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* How `after` changes what `before`, from `source`, has, one line each, the last steps and the tables' scopes aside:
|
|
5
|
+
* a scope is Protobase's to declare, not a name the store has; none when it only adds.
|
|
6
|
+
*/
|
|
4
7
|
export declare const nameChanges: (before: StoreModel, after: StoreModel, source?: string) => string[];
|
|
5
8
|
//#endregion
|
|
@@ -4,11 +4,14 @@ const changes = (before, after, at, source) => {
|
|
|
4
4
|
if (typeof before !== "object" || before === null || typeof after !== "object" || after === null) return before === after ? [] : [`${at} is ${JSON.stringify(before)} in ${source} and ${JSON.stringify(after)} now`];
|
|
5
5
|
return Object.entries(before).flatMap(([key, value]) => changes(value, after[key], at === "" ? key : `${at}.${key}`, source));
|
|
6
6
|
};
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* How `after` changes what `before`, from `source`, has, one line each, the last steps and the tables' scopes aside:
|
|
9
|
+
* a scope is Protobase's to declare, not a name the store has; none when it only adds.
|
|
10
|
+
*/
|
|
8
11
|
const nameChanges = (before, after, source = "the snapshot") => {
|
|
9
12
|
const withoutSteps = (model) => ({
|
|
10
13
|
...model,
|
|
11
|
-
modules: Object.fromEntries(Object.entries(model.modules).map(([module, entry]) => [module, entry.tables]))
|
|
14
|
+
modules: Object.fromEntries(Object.entries(model.modules).map(([module, entry]) => [module, Object.fromEntries(Object.entries(entry.tables).map(([internal, { scope: _, ...table }]) => [internal, table]))]))
|
|
12
15
|
});
|
|
13
16
|
return changes(withoutSteps(before), withoutSteps(after), "", source);
|
|
14
17
|
};
|
|
@@ -1,15 +1,29 @@
|
|
|
1
|
+
import { ModuleId } from "../../names/modules/index.js";
|
|
1
2
|
import { StoreNames } from "../store-names.js";
|
|
3
|
+
import { OrganizationsInput } from "../steps/step.js";
|
|
2
4
|
import { LastSteps } from "./store-model.js";
|
|
3
5
|
//#region src/better-auth/store/offline/offline-steps.d.ts
|
|
4
6
|
/**
|
|
5
|
-
* The
|
|
6
|
-
*
|
|
7
|
-
* version, so a database that has it already skips it; for a store the snapshot has none of
|
|
8
|
-
* (`lastSteps` undefined), that table first. `checks`: the checks of the steps on tables the store has already, which
|
|
9
|
-
* refuse when its rows break what a step assumes. `undefined` when the snapshot has every step. `externalUser`: the
|
|
10
|
-
* snapshot records the user table as a relation Protobase did not make, whose statements are left out.
|
|
7
|
+
* The store as a project's snapshot has it: each module's last step, those kept while off included; the modules that
|
|
8
|
+
* are on; and whether it has `auth_steps`, which a store a version before it made lacks.
|
|
11
9
|
*/
|
|
12
|
-
export
|
|
10
|
+
export type SnapshotStore = {
|
|
11
|
+
lastSteps: LastSteps;
|
|
12
|
+
on: readonly ModuleId[];
|
|
13
|
+
stepRecord: boolean;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The parts of the modules that are on that the snapshot's store lacks, each followed by its row in `auth_steps`, each
|
|
17
|
+
* run only while that table lacks it, so a database that has it already skips it; for a store the snapshot has none of
|
|
18
|
+
* (`store` undefined), that table first, and for one without it, that table, filled from `auth_migrations`. `checks`:
|
|
19
|
+
* the checks of the parts on tables the store has already, which refuse when its rows break what a step assumes;
|
|
20
|
+
* `after`, what the parts leave for the migration's `.after.sql`, when they leave anything. `undefined` when the
|
|
21
|
+
* snapshot's store has every part. `externalUser`: the snapshot records the user table as a relation Protobase did
|
|
22
|
+
* not make, whose statements are left out. `organizations`: the config's default organization and the role of the
|
|
23
|
+
* users there already (`renderSteps`).
|
|
24
|
+
*/
|
|
25
|
+
export declare const offlineStepsMigration: (names: StoreNames, store: SnapshotStore | undefined, organizations: OrganizationsInput | undefined, externalUser?: boolean) => {
|
|
26
|
+
after?: string | undefined;
|
|
13
27
|
sql: string;
|
|
14
28
|
checks: string[];
|
|
15
29
|
} | undefined;
|