@protobase/cli 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.
- package/dist/cli/package.js +1 -1
- package/dist/migration/apply-migrations.js +1 -1
- package/dist/migration/load-project.js +1 -51
- package/dist/migration/migration-project.js +53 -0
- package/dist/migration/plan-migrations.js +11 -5
- package/dist/migration/register.js +1 -1
- package/dist/migration/release/apply-release-steps.js +34 -0
- package/dist/migration/release/release-steps.js +28 -0
- package/dist/serve/main.js +7 -1
- package/dist/serve/migrate-release.js +39 -0
- package/dist/serve/runtime-program.js +1 -0
- package/package.json +8 -8
- package/skills/protobase/references/features/build-and-run.md +1 -0
- package/skills/protobase/references/guides/deploy.md +1 -1
- package/skills/protobase/references/reference/cli.md +19 -2
- package/skills/protobase/references/reference/files.md +1 -1
package/dist/cli/package.js
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
import { loadNearestEnvFile } from "../dev/env.js";
|
|
2
|
-
import { projectJobs, projectNotifications, projectResources, projectWebhooks } from "../config/project-parts.js";
|
|
3
|
-
import { mailFeedbackOn } from "../config/mail-feedback-config.js";
|
|
4
|
-
import { unsubscribeOn } from "../config/unsubscribe-config.js";
|
|
5
|
-
import { userTablesOn } from "../config/user-tables-config.js";
|
|
6
|
-
import { projectAuth } from "../config/build-project.js";
|
|
7
2
|
import { loadSourceConfig } from "../config/source-config.js";
|
|
8
3
|
import { withDevUrl } from "../config/tool-url.js";
|
|
9
|
-
import { openAppDatabase } from "../project/pg-db.js";
|
|
10
|
-
import pg from "pg";
|
|
11
|
-
import { assistantSettings, authStoreTables, configExports } from "@protobase/server";
|
|
12
|
-
import { checkStatusOptions } from "@protobase/server/internal";
|
|
13
4
|
//#region src/migration/load-project.ts
|
|
14
5
|
/**
|
|
15
6
|
* The config of the project in `dir` as `protobase dev` merges it: protobase.config.ts, with the convention's resources
|
|
@@ -23,46 +14,5 @@ const loadMigrationConfig = async (dir, extensions = []) => {
|
|
|
23
14
|
extensions
|
|
24
15
|
}), dir);
|
|
25
16
|
};
|
|
26
|
-
/**
|
|
27
|
-
* A merged project config, for what its database needs: its resources' models, a pool on `url`, its auth, and the
|
|
28
|
-
* features that write migrations. `close` ends the pool.
|
|
29
|
-
*/
|
|
30
|
-
const migrationProject = async (config, url) => {
|
|
31
|
-
if (!url) throw new Error("No database: set DATABASE_URL");
|
|
32
|
-
const database = openAppDatabase(pg, url);
|
|
33
|
-
const [resources, jobs, webhooks, notifications, auth] = await Promise.all([
|
|
34
|
-
projectResources(config),
|
|
35
|
-
projectJobs(config),
|
|
36
|
-
projectWebhooks(config),
|
|
37
|
-
projectNotifications(config),
|
|
38
|
-
projectAuth(config, database)
|
|
39
|
-
]);
|
|
40
|
-
const exported = configExports(resources);
|
|
41
|
-
return {
|
|
42
|
-
models: exported.resources.map((source) => source.toModel()),
|
|
43
|
-
views: exported.views.map((source) => source.toModel()),
|
|
44
|
-
db: database.db,
|
|
45
|
-
...auth && { auth },
|
|
46
|
-
...config.events && { events: config.events },
|
|
47
|
-
...webhooks && { webhooks },
|
|
48
|
-
...config.workflows && { workflows: config.workflows },
|
|
49
|
-
...config.realtime && { realtime: config.realtime },
|
|
50
|
-
...config.calls && { calls: config.calls },
|
|
51
|
-
...notifications && { notifications },
|
|
52
|
-
...config.status && { status: checkStatusOptions(config.status) },
|
|
53
|
-
jobs,
|
|
54
|
-
userTables: userTablesOn(config),
|
|
55
|
-
unsubscribe: unsubscribeOn(config),
|
|
56
|
-
mailFeedback: mailFeedbackOn(config),
|
|
57
|
-
readOnlyQueries: assistantSettings(config.assistant, process.env)?.kind === "built-in",
|
|
58
|
-
authTables: auth ? authStoreTables(auth) : {
|
|
59
|
-
tables: [],
|
|
60
|
-
schemas: []
|
|
61
|
-
},
|
|
62
|
-
close: async () => {
|
|
63
|
-
await database.db.destroy();
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
17
|
//#endregion
|
|
68
|
-
export { loadMigrationConfig
|
|
18
|
+
export { loadMigrationConfig };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { projectJobs, projectNotifications, projectResources, projectWebhooks } from "../config/project-parts.js";
|
|
2
|
+
import { mailFeedbackOn } from "../config/mail-feedback-config.js";
|
|
3
|
+
import { unsubscribeOn } from "../config/unsubscribe-config.js";
|
|
4
|
+
import { userTablesOn } from "../config/user-tables-config.js";
|
|
5
|
+
import { projectAuth } from "../config/build-project.js";
|
|
6
|
+
import { openAppDatabase } from "../project/pg-db.js";
|
|
7
|
+
import pg from "pg";
|
|
8
|
+
import { assistantSettings, authStoreTables, configExports } from "@protobase/server";
|
|
9
|
+
import { checkStatusOptions } from "@protobase/server/internal";
|
|
10
|
+
//#region src/migration/migration-project.ts
|
|
11
|
+
/**
|
|
12
|
+
* A merged project config, for what its database needs: its resources' models, a pool on `url`, its auth, and the
|
|
13
|
+
* features that write migrations. `close` ends the pool.
|
|
14
|
+
*/
|
|
15
|
+
const migrationProject = async (config, url) => {
|
|
16
|
+
if (!url) throw new Error("No database: set DATABASE_URL");
|
|
17
|
+
const database = openAppDatabase(pg, url);
|
|
18
|
+
const [resources, jobs, webhooks, notifications, auth] = await Promise.all([
|
|
19
|
+
projectResources(config),
|
|
20
|
+
projectJobs(config),
|
|
21
|
+
projectWebhooks(config),
|
|
22
|
+
projectNotifications(config),
|
|
23
|
+
projectAuth(config, database)
|
|
24
|
+
]);
|
|
25
|
+
const exported = configExports(resources);
|
|
26
|
+
return {
|
|
27
|
+
models: exported.resources.map((source) => source.toModel()),
|
|
28
|
+
views: exported.views.map((source) => source.toModel()),
|
|
29
|
+
db: database.db,
|
|
30
|
+
...auth && { auth },
|
|
31
|
+
...config.events && { events: config.events },
|
|
32
|
+
...webhooks && { webhooks },
|
|
33
|
+
...config.workflows && { workflows: config.workflows },
|
|
34
|
+
...config.realtime && { realtime: config.realtime },
|
|
35
|
+
...config.calls && { calls: config.calls },
|
|
36
|
+
...notifications && { notifications },
|
|
37
|
+
...config.status && { status: checkStatusOptions(config.status) },
|
|
38
|
+
jobs,
|
|
39
|
+
userTables: userTablesOn(config),
|
|
40
|
+
unsubscribe: unsubscribeOn(config),
|
|
41
|
+
mailFeedback: mailFeedbackOn(config),
|
|
42
|
+
readOnlyQueries: assistantSettings(config.assistant, process.env)?.kind === "built-in",
|
|
43
|
+
authTables: auth ? authStoreTables(auth) : {
|
|
44
|
+
tables: [],
|
|
45
|
+
schemas: []
|
|
46
|
+
},
|
|
47
|
+
close: async () => {
|
|
48
|
+
await database.db.destroy();
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
//#endregion
|
|
53
|
+
export { migrationProject };
|
|
@@ -9,17 +9,23 @@ const appliedNames = async (tx) => {
|
|
|
9
9
|
if (record.earlier) return (await tx`select name from public.schema_migrations`).map((row) => row.name);
|
|
10
10
|
return [];
|
|
11
11
|
};
|
|
12
|
+
/** The `*.sql` files of `dir` the database has not recorded as applied, in their order, read in the transaction `tx`. */
|
|
13
|
+
const pendingIn = async (tx, dir) => {
|
|
14
|
+
const files = (await readdir(dir)).filter((file) => file.endsWith(".sql")).sort();
|
|
15
|
+
const done = new Set(await appliedNames(tx));
|
|
16
|
+
return files.filter((file) => !done.has(file));
|
|
17
|
+
};
|
|
18
|
+
/** `pendingIn`, from a read-only transaction of its own. */
|
|
19
|
+
const pendingMigrations = (sql, dir) => sql.begin("read only", (tx) => pendingIn(tx, dir));
|
|
12
20
|
/**
|
|
13
21
|
* `protobase migrate --plan`: prints each `*.sql` file of `dir` that `migrate` would apply, in its order, under its
|
|
14
|
-
* name, with its SQL
|
|
22
|
+
* name, with its SQL: it applies and records nothing. Returns the names.
|
|
15
23
|
*/
|
|
16
24
|
const planMigrations = async (sql, dir, out) => {
|
|
17
|
-
const
|
|
18
|
-
const done = new Set(await sql.begin("read only", (tx) => appliedNames(tx)));
|
|
19
|
-
const pending = files.filter((file) => !done.has(file));
|
|
25
|
+
const pending = await pendingMigrations(sql, dir);
|
|
20
26
|
for (const file of pending) out(`-- ${file}\n${(await readFile(path.join(dir, file), "utf8")).trimEnd()}\n\n`);
|
|
21
27
|
out(pending.length > 0 ? `${pending.length} migrations to apply; nothing was applied\n` : "the database is up to date\n");
|
|
22
28
|
return pending;
|
|
23
29
|
};
|
|
24
30
|
//#endregion
|
|
25
|
-
export { planMigrations };
|
|
31
|
+
export { pendingIn, pendingMigrations, planMigrations };
|
|
@@ -44,7 +44,7 @@ const registerMigration = (program) => {
|
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
46
|
const [{ migrationProject }, { migrationParts }, { runMigration }, { migrationSummary }] = await Promise.all([
|
|
47
|
-
import("./
|
|
47
|
+
import("./migration-project.js"),
|
|
48
48
|
import("./parts.js"),
|
|
49
49
|
import("./snapshot/run-migration.js"),
|
|
50
50
|
import("./summary.js")
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { executeFile, lockKey } from "../apply-migrations.js";
|
|
2
|
+
//#region src/migration/release/apply-release-steps.ts
|
|
3
|
+
/**
|
|
4
|
+
* Applies the steps `find` gives in one transaction, behind the lock `migrate` takes, each as `migrate` runs a file, so
|
|
5
|
+
* none commits early: when one fails, such as a check that refuses the rows already there, none is applied. `find` runs
|
|
6
|
+
* once the lock is held, so a run that waited on another finds the steps from what that one committed.
|
|
7
|
+
*/
|
|
8
|
+
const applyReleaseSteps = async (sql, find, out) => {
|
|
9
|
+
const applied = await sql.begin(async (tx) => {
|
|
10
|
+
await tx`select pg_advisory_xact_lock(${lockKey})`;
|
|
11
|
+
const steps = await find(tx);
|
|
12
|
+
for (const step of steps) {
|
|
13
|
+
out(`applying ${step.name}\n`);
|
|
14
|
+
await tx`select set_config('migrations.file', ${step.sql}, true)`;
|
|
15
|
+
await tx.unsafe(executeFile);
|
|
16
|
+
}
|
|
17
|
+
return steps;
|
|
18
|
+
});
|
|
19
|
+
out(applied.length > 0 ? `applied ${applied.length} steps\n` : "the database is up to date\n");
|
|
20
|
+
};
|
|
21
|
+
/** The steps `find` gives from a read-only transaction, printed as `releaseStepsPlan`: nothing is applied. */
|
|
22
|
+
const planReleaseSteps = async (sql, find, out) => {
|
|
23
|
+
out(releaseStepsPlan(await sql.begin("read only", (tx) => find(tx))));
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The steps as one SQL script, each under its title, ending with a comment that says how many there are: what a caller
|
|
27
|
+
* runs in a transaction of its own, which its rollback undoes. It records the auth store's versions itself.
|
|
28
|
+
*/
|
|
29
|
+
const releaseStepsPlan = (steps) => {
|
|
30
|
+
if (steps.length === 0) return "-- the database is up to date\n";
|
|
31
|
+
return `${steps.map(({ name, title, sql }) => `-- ${name}: ${title}.\n${sql.trimEnd()}\n`).join("\n")}\n-- ${steps.length} steps to apply; nothing was applied\n`;
|
|
32
|
+
};
|
|
33
|
+
//#endregion
|
|
34
|
+
export { applyReleaseSteps, planReleaseSteps, releaseStepsPlan };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { authMigrationTitle } from "../../auth/migration-file.js";
|
|
2
|
+
import { migrationParts } from "../parts.js";
|
|
3
|
+
import { authSchemaMigration } from "@protobase/server";
|
|
4
|
+
//#region src/migration/release/release-steps.ts
|
|
5
|
+
/**
|
|
6
|
+
* What this release of Protobase still needs of the project's database for every feature its config turns on, compared
|
|
7
|
+
* with the database itself: the auth store by its `auth_migrations` record, the other parts by their schemas, as
|
|
8
|
+
* `protobase migration` compares them. In the order they apply; none when the database is current.
|
|
9
|
+
*/
|
|
10
|
+
const releaseSteps = async (project) => {
|
|
11
|
+
const auth = project.auth && await authSchemaMigration(project.auth);
|
|
12
|
+
const steps = auth ? [{
|
|
13
|
+
name: "auth",
|
|
14
|
+
title: authMigrationTitle,
|
|
15
|
+
sql: auth
|
|
16
|
+
}] : [];
|
|
17
|
+
for (const part of migrationParts(project)) {
|
|
18
|
+
const sql = await part.sql({ authWritten: Boolean(auth) });
|
|
19
|
+
if (sql !== void 0) steps.push({
|
|
20
|
+
name: part.name,
|
|
21
|
+
title: part.title,
|
|
22
|
+
sql
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return steps;
|
|
26
|
+
};
|
|
27
|
+
//#endregion
|
|
28
|
+
export { releaseSteps };
|
package/dist/serve/main.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createAdminInBundle } from "./create-admin.js";
|
|
2
2
|
import { provideHostModules } from "./host-modules.js";
|
|
3
3
|
import { migrateBundle } from "./migrate-bundle.js";
|
|
4
|
+
import { migrateRelease } from "./migrate-release.js";
|
|
4
5
|
import { runtimeProgram } from "./runtime-program.js";
|
|
5
6
|
import { serveBundlePath } from "./serve-bundle-path.js";
|
|
6
7
|
//#region src/serve/main.ts
|
|
@@ -9,15 +10,20 @@ const fail = (message) => {
|
|
|
9
10
|
process.exit(1);
|
|
10
11
|
};
|
|
11
12
|
const out = (text) => process.stdout.write(text);
|
|
13
|
+
const exitWhenWritten = () => process.stdout.write("", () => process.exit(0));
|
|
12
14
|
provideHostModules();
|
|
13
15
|
await runtimeProgram({
|
|
14
16
|
serve: async (bundle) => {
|
|
15
17
|
await serveBundlePath(bundle, process.env, out);
|
|
16
18
|
},
|
|
17
19
|
migrate: (bundle) => migrateBundle(bundle, process.env, out),
|
|
20
|
+
migrateRelease: async (bundle, { plan }) => {
|
|
21
|
+
await migrateRelease(bundle, process.env, out, plan);
|
|
22
|
+
exitWhenWritten();
|
|
23
|
+
},
|
|
18
24
|
createAdmin: async (bundle, email, options) => {
|
|
19
25
|
await createAdminInBundle(bundle, email, options, out);
|
|
20
|
-
|
|
26
|
+
exitWhenWritten();
|
|
21
27
|
}
|
|
22
28
|
}).parseAsync(process.argv).catch((error) => fail(error.message));
|
|
23
29
|
//#endregion
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { readManifest } from "../bundle/manifest.js";
|
|
2
|
+
import { checkBundleManifest } from "./bundle-manifest.js";
|
|
3
|
+
import { loadBundle } from "./load-bundle.js";
|
|
4
|
+
import { bundleConfigFile } from "../worker/start-worker.js";
|
|
5
|
+
import { migrationProject } from "../migration/migration-project.js";
|
|
6
|
+
import { pendingIn } from "../migration/plan-migrations.js";
|
|
7
|
+
import { applyReleaseSteps, planReleaseSteps } from "../migration/release/apply-release-steps.js";
|
|
8
|
+
import { releaseSteps } from "../migration/release/release-steps.js";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import postgres from "postgres";
|
|
11
|
+
//#region src/serve/migrate-release.ts
|
|
12
|
+
/**
|
|
13
|
+
* The runtime's `migrate-release`: brings DATABASE_URL to this release's own schema steps for every feature the
|
|
14
|
+
* bundle's config turns on (see releaseSteps), applied in one transaction, or with `plan` printed as SQL and applied
|
|
15
|
+
* not at all. It writes no file: the steps come from the database and this release. The bundle's own migrations go
|
|
16
|
+
* first, since the steps are compared with the database they made. On a failure the error ends the process.
|
|
17
|
+
*/
|
|
18
|
+
const migrateRelease = async (bundle, env, out, plan) => {
|
|
19
|
+
const file = await bundleConfigFile(bundle);
|
|
20
|
+
checkBundleManifest(file);
|
|
21
|
+
const dir = path.dirname(file);
|
|
22
|
+
if (!env.DATABASE_URL) throw new Error("No database: set DATABASE_URL");
|
|
23
|
+
const sql = postgres(env.DATABASE_URL, {
|
|
24
|
+
max: 1,
|
|
25
|
+
onnotice: () => {}
|
|
26
|
+
});
|
|
27
|
+
const { migrations } = await readManifest(dir);
|
|
28
|
+
const project = await migrationProject(await loadBundle(file), env.DATABASE_URL);
|
|
29
|
+
const find = async (tx) => {
|
|
30
|
+
const pending = migrations ? await pendingIn(tx, path.join(dir, migrations)) : [];
|
|
31
|
+
if (pending.length > 0) throw new Error(`The database lacks the bundle's migrations ${pending.join(", ")}: apply them first with \`protobase-serve migrate\``);
|
|
32
|
+
return releaseSteps(project);
|
|
33
|
+
};
|
|
34
|
+
await (plan ? planReleaseSteps(sql, find, out) : applyReleaseSteps(sql, find, out));
|
|
35
|
+
await project.close();
|
|
36
|
+
await sql.end();
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
export { migrateRelease };
|
|
@@ -9,6 +9,7 @@ const runtimeProgram = (actions) => {
|
|
|
9
9
|
const program = new Command("protobase-serve").description("Protobase's serve runtime for a bundle from `protobase build`");
|
|
10
10
|
program.command("serve", { isDefault: true }).description("Serve a bundle: its folder serves the app and the API, its config module the API alone").argument("<bundle>").action((bundle) => actions.serve(bundle));
|
|
11
11
|
program.command("migrate").description("Apply the bundle's pending migrations in one transaction, before a new version serves (DATABASE_URL)").argument("<bundle>").action((bundle) => actions.migrate(bundle));
|
|
12
|
+
program.command("migrate-release").description("Apply this release's own schema steps for the features the bundle's config turns on, in one transaction, writing no file (DATABASE_URL)").argument("<bundle>").option("--plan", "print the steps as SQL, and apply nothing", false).action((bundle, opts) => actions.migrateRelease(bundle, { plan: opts.plan }));
|
|
12
13
|
program.command("create-admin").description("Create an admin user of the bundle's app").argument("<bundle>").argument("<email>").option("--name <name>", "display name").option("--password-stdin", "read the password from stdin", false).option("--generate-password", "generate a strong password and print it once", false).addOption(new Option("--password <value>").hideHelp()).action((bundle, email, opts) => {
|
|
13
14
|
rejectPasswordArgument(opts.password);
|
|
14
15
|
return actions.createAdmin(bundle, email, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.128.0",
|
|
4
4
|
"description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -43,13 +43,13 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@better-auth/core": "^1.7.12",
|
|
45
45
|
"@hono/node-server": "^2.1.3",
|
|
46
|
-
"@protobase/config": "0.
|
|
47
|
-
"@protobase/layout": "0.
|
|
48
|
-
"@protobase/query": "0.
|
|
49
|
-
"@protobase/schema": "0.
|
|
50
|
-
"@protobase/server": "0.
|
|
51
|
-
"@protobase/ui": "0.
|
|
52
|
-
"@protobase/workflows": "0.
|
|
46
|
+
"@protobase/config": "0.128.0",
|
|
47
|
+
"@protobase/layout": "0.128.0",
|
|
48
|
+
"@protobase/query": "0.128.0",
|
|
49
|
+
"@protobase/schema": "0.128.0",
|
|
50
|
+
"@protobase/server": "0.128.0",
|
|
51
|
+
"@protobase/ui": "0.128.0",
|
|
52
|
+
"@protobase/workflows": "0.128.0",
|
|
53
53
|
"@react-email/render": "^2.1.0",
|
|
54
54
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
55
55
|
"@sentry/vite-plugin": "^11.10.0",
|
|
@@ -22,6 +22,7 @@ Develop, build and run an app, on servers, in containers and on phones.
|
|
|
22
22
|
- [Serve runtime](../reference/cli.md#serve): Runs a bundle, set by environment variables, with a health check, startup checks, jobs and a clean shutdown. `PROTOBASE_HOST=127.0.0.1` keeps it to this computer.
|
|
23
23
|
- [Host module list](../reference/cli.md#the-config-module): Each release of `@protobase/cli` carries the modules its runtime supplies to a bundle as `@protobase/cli/host-modules.json`, for a host to check a bundle's imports against the version that serves it.
|
|
24
24
|
- [Migrations at update time](../guides/deploy.md#migrations): The bundle carries the project's migrations, and the runtime applies them in one transaction before the new version serves.
|
|
25
|
+
- [Release steps](../reference/cli.md#release-steps): `protobase-serve migrate-release` brings a database up to a release's own built-in schema steps straight from a built bundle, with a dry-run plan, without writing files into the project.
|
|
25
26
|
- [Single-file runtime](../reference/cli.md#build-serve): The runtime as one file, so a host needs only Bun and that file.
|
|
26
27
|
- [Separate worker](../reference/cli.md#worker): Jobs and event delivery in their own process.
|
|
27
28
|
- [Container images](../guides/deploy.md#images): A runtime image for each release, for amd64 and arm64.
|
|
@@ -32,7 +32,7 @@ The host serves `public/` itself and sends only the manifest's `api` paths to th
|
|
|
32
32
|
|
|
33
33
|
## Migrations
|
|
34
34
|
|
|
35
|
-
Serving never changes the database: the bundle carries the project's `db/migrations` as `migrations/`, and the runtime's `migrate <bundle>` applies the pending ones, the app's and the auth schema's, before a new version serves, with no other writes while it runs. It applies them in one transaction behind a lock, recorded in `migrations.applied` as the presets' `db:migrate` does, so a database either script migrated is current for the other. A database an earlier version migrated keeps its record in `public.schema_migrations`, which `migrate` copies over the first time; a `public.schema_migrations` of another migration tool, without Protobase's `name` and `applied_at` columns, is never read or written. A file that would end that transaction (`BEGIN`, `COMMIT`, `ROLLBACK`) or cannot run in one (`create index concurrently`) is refused, and a refused or failing file leaves the database unchanged. The runtime refuses to start while the auth store has not applied every auth migration of the new version, which it reads from the store's `auth_migrations` table without comparing its tables (see [migrations](../reference/auth.md#migrations)). [`protobase migration`](../reference/cli.md#migration) writes the files the config's features need, such as the auth and jobs schemas, as the project's own migrations, and [a self-hosted image](#a-self-hosted-image) runs `migrate` as a step of its own before the app starts.
|
|
35
|
+
Serving never changes the database: the bundle carries the project's `db/migrations` as `migrations/`, and the runtime's `migrate <bundle>` applies the pending ones, the app's and the auth schema's, before a new version serves, with no other writes while it runs. It applies them in one transaction behind a lock, recorded in `migrations.applied` as the presets' `db:migrate` does, so a database either script migrated is current for the other. A database an earlier version migrated keeps its record in `public.schema_migrations`, which `migrate` copies over the first time; a `public.schema_migrations` of another migration tool, without Protobase's `name` and `applied_at` columns, is never read or written. A file that would end that transaction (`BEGIN`, `COMMIT`, `ROLLBACK`) or cannot run in one (`create index concurrently`) is refused, and a refused or failing file leaves the database unchanged. The runtime refuses to start while the auth store has not applied every auth migration of the new version, which it reads from the store's `auth_migrations` table without comparing its tables (see [migrations](../reference/auth.md#migrations)). [`protobase migration`](../reference/cli.md#migration) writes the files the config's features need, such as the auth and jobs schemas, as the project's own migrations, and [a self-hosted image](#a-self-hosted-image) runs `migrate` as a step of its own before the app starts. A host that serves a bundle on a newer release's runtime brings the database to that release's own schema steps with the runtime's [`migrate-release <bundle>`](../reference/cli.md#release-steps), after `migrate` and before the bundle serves there.
|
|
36
36
|
|
|
37
37
|
## Images
|
|
38
38
|
|
|
@@ -444,10 +444,27 @@ Bundles the serve runtime (`@protobase/cli`'s `src/serve/main.ts`) and all its d
|
|
|
444
444
|
```sh
|
|
445
445
|
bun --no-install protobase-serve.js [serve] <bundle> # the folder: the app and the API; the config module: the API
|
|
446
446
|
bun --no-install protobase-serve.js migrate <bundle> # apply the bundle's migrations to DATABASE_URL
|
|
447
|
+
bun --no-install protobase-serve.js migrate-release <bundle> # [--plan] apply this release's own schema steps to DATABASE_URL
|
|
447
448
|
bun --no-install protobase-serve.js create-admin <bundle> <email> # [--name <name>] [--generate-password | --password-stdin]
|
|
448
449
|
```
|
|
449
450
|
|
|
450
|
-
`migrate` runs none of the app's code and needs only `DATABASE_URL`; `create-admin` loads the app with its settings and refuses an auth schema that is behind; without `--generate-password` or `--password-stdin` it asks for the password at a hidden prompt. Given the folder, the runtime serves the bundle's own copy of the app. [Deploy](../guides/deploy.md#a-self-hosted-image) puts
|
|
451
|
+
`migrate` runs none of the app's code and needs only `DATABASE_URL`; `create-admin` loads the app with its settings and refuses an auth schema that is behind; without `--generate-password` or `--password-stdin` it asks for the password at a hidden prompt. Given the folder, the runtime serves the bundle's own copy of the app. [Deploy](../guides/deploy.md#a-self-hosted-image) puts `serve`, `migrate` and `create-admin` in a self-hosted image.
|
|
452
|
+
|
|
453
|
+
### Release steps
|
|
454
|
+
|
|
455
|
+
A bundle built on one release can be served on the runtime of a newer one, whose built-in schemas may have moved on: new steps of the [sign-in tables](auth.md#migrations), a newer jobs or workflows schema. `migrate-release` brings the database to them straight from the bundle, with no checkout of the project, and writes no file into it:
|
|
456
|
+
|
|
457
|
+
```sh
|
|
458
|
+
bun --no-install protobase-serve.js migrate <bundle> # the bundle's own migrations first
|
|
459
|
+
bun --no-install protobase-serve.js migrate-release <bundle> --plan # print what this release still needs, as SQL
|
|
460
|
+
bun --no-install protobase-serve.js migrate-release <bundle> # apply it
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
It loads the bundle's config with the app's settings, as `create-admin` does, and compares every feature the config turns on with the database itself, as [`migration`](#migration) does with `DATABASE_URL` set: the sign-in tables by the versions their `auth_migrations` table records, the jobs, change capture, workflows, notifications and the other built-in schemas by what the database has. It applies what differs in one transaction, behind the lock `migrate` takes, prints each part it applies, such as `applying auth`, or that the database is up to date, so a second run applies nothing. A step whose check refuses the rows already there, such as two From addresses one person entered with different case, stops it with the step's message and exit code 1, and nothing is applied. It refuses a database that lacks any of the bundle's own migrations, since the steps are compared with what they made.
|
|
464
|
+
|
|
465
|
+
`--plan` applies nothing and prints the same statements as one SQL script, each part under a comment, ending with `-- N steps to apply; nothing was applied`. The script records the sign-in versions it applies itself, so a host can run it in a transaction of its own, such as one it rolls back to try it first, or one it commits while nothing else writes.
|
|
466
|
+
|
|
467
|
+
A project that gets the same steps later from its own [`migration`](#migration) applies them without doing any twice: each sign-in step in its files runs only while `auth_migrations` lacks its version, and the other parts are compared with its database.
|
|
451
468
|
|
|
452
469
|
## `serve`
|
|
453
470
|
|
|
@@ -653,7 +670,7 @@ protobase migration up
|
|
|
653
670
|
|
|
654
671
|
Writes what the config and this version of Protobase need beyond the project's `protobase.snapshot.json` as the project's next migration in `--dir`: one numbered file each run, named after what it holds, such as `004_auth.sql` or `005_changes_webhooks.sql`, after the folder's highest number, and rewrites the snapshot to end at it, so both show in the pull request. The file starts with lines Protobase reads back: `-- protobase:id`, a random id of its own, then the version that wrote it, each sign-in module's last step, and the hash of the snapshot's model once it is applied. It writes nothing, the snapshot included, when nothing differs. Apply the file with the project's other migrations.
|
|
655
672
|
|
|
656
|
-
The snapshot describes the schema the project's migrations make: the sign-in store as the runtime reads it, each module that is on with its tables, columns and indexes under the names the config gives them, its last step, and whether every table whose rows belong to an organization has the column that names it, with `modelHash`, the SHA-256 of exactly that; what the migrations decided, the file it ends at with its id, its `format` and the version of Protobase that wrote it, the modules kept or archived, whether organizations were ever on and the default organization; and `physical`, every schema, table, column and index name the store has. The sign-in store's part of the file comes from the snapshot alone, without a database: the [steps](auth.md#migrations) after each module's last one, those a newer Protobase ships included, each version followed by its row in `auth_migrations`, which the server's startup check reads. With `DATABASE_URL` set, the checks of those steps run first against the database's rows, in a read-only transaction, and refuse as the step would, such as on two accounts with one provider and account id. A name the store has that the config changes, such as a table renamed in [`auth.tables`](auth.md#table-and-column-names), is refused with what changed, since Protobase does not write renames: write it by hand with `--custom` and `--record-config`. <gap>`protobase migration` cannot write the sign-in store of a project with auth migrations from before `protobase.snapshot.json` because `protobase migration baseline`, which reads the database once to start the snapshot, is not implemented.</gap>
|
|
673
|
+
The snapshot describes the schema the project's migrations make: the sign-in store as the runtime reads it, each module that is on with its tables, columns and indexes under the names the config gives them, its last step, and whether every table whose rows belong to an organization has the column that names it, with `modelHash`, the SHA-256 of exactly that; what the migrations decided, the file it ends at with its id, its `format` and the version of Protobase that wrote it, the modules kept or archived, whether organizations were ever on and the default organization; and `physical`, every schema, table, column and index name the store has. The sign-in store's part of the file comes from the snapshot alone, without a database: the [steps](auth.md#migrations) after each module's last one, those a newer Protobase ships included, each version followed by its row in `auth_migrations`, which the server's startup check reads. Each runs only while `auth_migrations` lacks its version, so a database that has it already, such as one [`migrate-release`](#release-steps) brought to a newer release, skips it. With `DATABASE_URL` set, the checks of those steps run first against the database's rows, in a read-only transaction, and refuse as the step would, such as on two accounts with one provider and account id. A name the store has that the config changes, such as a table renamed in [`auth.tables`](auth.md#table-and-column-names), is refused with what changed, since Protobase does not write renames: write it by hand with `--custom` and `--record-config`. <gap>`protobase migration` cannot write the sign-in store of a project with auth migrations from before `protobase.snapshot.json` because `protobase migration baseline`, which reads the database once to start the snapshot, is not implemented.</gap>
|
|
657
674
|
|
|
658
675
|
The rest of the file is what the other features the config enables still need of the database, compared with it, each under its own comment and named in the file name as in brackets: the [jobs schema](jobs.md#the-jobs-schema) when the config has jobs, `webhooks`, `workflows`, `notifications` or `mailFeedback`, or a resource has events, history, comments or an [AI field](ai.md) or is a [time rollup](data-config.md#keeping-it-up-to-date) (`jobs`), the table of the bounces and complaints of [`mailFeedback`](email.md#bounces-and-complaints) (`mail_feedback`), the [capture of events](events.md#migrations-and-startup) and the [history table](history.md#the-table) (`changes`), the columns of [rollup and formula fields](data-config.md#rollups-and-formulas), the triggers that keep the rollups, and the tables, indexes and refresh functions of [time rollups](data-config.md#time-rollups) (`computed`), the role the [assistant's read-only queries](assistant.md#the-built-in-backend) run as, when it finds the built-in assistant's key (`query`), the [comments table](comments.md#the-table) (`comments`), the [webhook subscriptions](webhooks.md#subscriptions-in-the-admin-app) of `webhooks` (`webhooks`), the [workflows, versions, runs and steps](workflows.md#turning-them-on) of `workflows` (`workflows`), the [devices and history tables](notifications.md#table-names) of `notifications` (`notifications`), the table of who unsubscribed from bulk mail of [`unsubscribe`](email.md#unsubscribe) (`unsubscribes`), the `realtime_relay` schema of [`realtime: { processes: 'many' }`](supabase-realtime.md#several-processes) (`realtime_relay`), the `channels` schema of [`realtime: { sqlSends: true }`](channels.md#from-sql) (`channel_sends`), the trigger of [`realtime.relay`](realtime-relay.md#sessions) that ends a deleted session's sockets and `realtime.members` with the triggers that keep it from channel records that list their members, also for an app with sign-in and a live resource whose conversation page has `members` (`relay`), and, with [`userTables`](user-tables.md#the-switch), the schemas of user tables (`user_tables`) and the delete of the definitions of tables that resources in code [own now](user-tables.md#taking-a-table-over-as-code) (`owned_definitions`). Those are compared only with `DATABASE_URL` set, against the database with the project's migrations applied, and the command says so without it. <gap>`protobase migration` cannot write the jobs, change capture, rollup and other feature schemas from the snapshot because recording their models in it is not implemented.</gap> It reads the config merged with those of `--extend` as `dev` merges them, and the `.env` nearest the current directory.
|
|
659
676
|
|
|
@@ -184,7 +184,7 @@ meter_photo: f.file().capture(),
|
|
|
184
184
|
selfie: f.file().capture('user').optional(),
|
|
185
185
|
```
|
|
186
186
|
|
|
187
|
-
The field takes only images: the image types of its `.accept()`, or any image when it names none, and an `.accept()` after it that names another type is an error. On a phone or tablet the form offers Take a photo, which opens the camera, and Choose a photo, which opens the phone's own chooser for a photo taken before; a stored photo offers Retake beside Replace. A desktop browser has no camera to open, so the field picks or takes a dropped image file.
|
|
187
|
+
The field takes only images: the image types of its `.accept()`, or any image when it names none, and an `.accept()` after it that names another type, or none, is an error. On a phone or tablet the form offers Take a photo, which opens the camera, and Choose a photo, which opens the phone's own chooser for a photo taken before; a stored photo offers Retake beside Replace. A desktop browser has no camera to open, so the field picks or takes a dropped image file.
|
|
188
188
|
|
|
189
189
|
### How the type is decided
|
|
190
190
|
|