@protobase/cli 0.55.7 → 0.55.8
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
CHANGED
|
@@ -14,7 +14,7 @@ import { runPgTool } from "./pg-tools.js";
|
|
|
14
14
|
import { realtimeMigration, realtimePolicies } from "./realtime-policies.js";
|
|
15
15
|
import { replayScript } from "./replay.js";
|
|
16
16
|
import { schemaMigration } from "./schema-migration.js";
|
|
17
|
-
import {
|
|
17
|
+
import { countSsoUsers, refuseSsoUsers } from "./sso-users.js";
|
|
18
18
|
import { appSchemas, cronJobs, cronTimezone, dumpedTables, extensions, httpTriggers, hypertables, pgmqQueues, pgsodiumColumns, platformMigrationTables, realtimeTables, routinesIn, tablesSize, vaultSecretValues, vaultSecrets } from "./source-facts.js";
|
|
19
19
|
import { ensureApiRoles, existingSchemas, existingTables } from "./target-checks.js";
|
|
20
20
|
import { creatableExtensions } from "./target-extensions.js";
|
|
@@ -144,7 +144,7 @@ const runDumpPhase = async (options, out) => {
|
|
|
144
144
|
await reserved`begin isolation level repeatable read read only`;
|
|
145
145
|
const [exported] = await reserved`select pg_export_snapshot() as snapshot`;
|
|
146
146
|
const snapshot = exported.snapshot;
|
|
147
|
-
refuseSsoUsers(await
|
|
147
|
+
refuseSsoUsers(await countSsoUsers((text) => reserved.unsafe(text)));
|
|
148
148
|
const { manifest: source, plan, realtimePolicies: policies, replay } = await readSource(reserved, options.targetUrl);
|
|
149
149
|
const created = await checkTarget(options.targetUrl, [...source.schemas, "realtime"], source.tables);
|
|
150
150
|
if (created.length > 0) out(`created the roles ${created.join(", ")} in the project's database\n`);
|
|
@@ -1,14 +1,34 @@
|
|
|
1
1
|
//#region src/supabase/sso-users.ts
|
|
2
|
-
/**
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return
|
|
2
|
+
/** The query that lists the SSO users, which the refusal prints instead of their addresses. */
|
|
3
|
+
const ssoUsersQuery = "select id, email from auth.users where is_sso_user";
|
|
4
|
+
/** Counts the SSO users of auth.users; none where GoTrue's schema predates `is_sso_user`. */
|
|
5
|
+
const countSsoUsers = async (query) => {
|
|
6
|
+
if ((await query(`select 1 from information_schema.columns where table_schema = 'auth' and table_name = 'users' and column_name = 'is_sso_user'`)).length === 0) return {
|
|
7
|
+
shared: 0,
|
|
8
|
+
alone: 0
|
|
9
|
+
};
|
|
10
|
+
const [row] = await query(`select count(*) filter (where shared)::int as shared, count(*) filter (where not shared)::int as alone from (select exists (select 1 from auth.users other where other.id <> sso.id and lower(other.email) = lower(sso.email)) as shared from auth.users sso where sso.is_sso_user) as sso`);
|
|
11
|
+
return {
|
|
12
|
+
shared: row.shared,
|
|
13
|
+
alone: row.alone
|
|
14
|
+
};
|
|
7
15
|
};
|
|
8
|
-
|
|
9
|
-
|
|
16
|
+
const users = (count) => `${count} ${count === 1 ? "user" : "users"}`;
|
|
17
|
+
/**
|
|
18
|
+
* Fails when the Supabase project has SSO users, saying how many, how to list them and what deleting one does. It
|
|
19
|
+
* prints no address, since CLI output gets pasted into issues, and offers no fix: what to do with the users is the
|
|
20
|
+
* developer's call.
|
|
21
|
+
*/
|
|
22
|
+
const refuseSsoUsers = ({ shared, alone }) => {
|
|
23
|
+
const count = shared + alone;
|
|
10
24
|
if (count === 0) return;
|
|
11
|
-
throw new Error(
|
|
25
|
+
throw new Error([
|
|
26
|
+
`The Supabase project has ${count} single sign-on (SAML) ${count === 1 ? "user" : "users"}, which the import cannot carry over: Supabase opens them only through their identity provider, and Better Auth would find them by email.`,
|
|
27
|
+
`${users(shared)} ${shared === 1 ? "shares its" : "share their"} address with another user of auth.users, ${users(alone)} ${alone === 1 ? "has" : "have"} an address of ${alone === 1 ? "its" : "their"} own.`,
|
|
28
|
+
`List them in Supabase with: ${ssoUsersQuery}`,
|
|
29
|
+
`Deleting a user in Supabase deletes or breaks the app's rows that reference it: a foreign key to auth.users that cascades deletes them, one that sets null leaves them without their user, and any other refuses the delete.`,
|
|
30
|
+
`What to do about these users is your decision, not the import's: decide it in Supabase, then import again`
|
|
31
|
+
].join("\n"));
|
|
12
32
|
};
|
|
13
33
|
//#endregion
|
|
14
|
-
export { refuseSsoUsers,
|
|
34
|
+
export { countSsoUsers, refuseSsoUsers, ssoUsersQuery };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.55.
|
|
3
|
+
"version": "0.55.8",
|
|
4
4
|
"description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"skills"
|
|
34
34
|
],
|
|
35
35
|
"scripts": {
|
|
36
|
-
"build": "tsdown",
|
|
37
|
-
"prepack": "
|
|
36
|
+
"build": "bun ../../scripts/protobase-skill.ts && tsdown",
|
|
37
|
+
"prepack": "bun run build"
|
|
38
38
|
},
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|
|
@@ -42,12 +42,12 @@
|
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@better-auth/core": "^1.7.12",
|
|
44
44
|
"@hono/node-server": "^2.1.3",
|
|
45
|
-
"@protobase/config": "0.55.
|
|
46
|
-
"@protobase/layout": "0.55.
|
|
47
|
-
"@protobase/query": "0.55.
|
|
48
|
-
"@protobase/schema": "0.55.
|
|
49
|
-
"@protobase/server": "0.55.
|
|
50
|
-
"@protobase/ui": "0.55.
|
|
45
|
+
"@protobase/config": "0.55.8",
|
|
46
|
+
"@protobase/layout": "0.55.8",
|
|
47
|
+
"@protobase/query": "0.55.8",
|
|
48
|
+
"@protobase/schema": "0.55.8",
|
|
49
|
+
"@protobase/server": "0.55.8",
|
|
50
|
+
"@protobase/ui": "0.55.8",
|
|
51
51
|
"@react-email/render": "^2.1.0",
|
|
52
52
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
53
53
|
"@tailwindcss/vite": "^4.3.3",
|
|
@@ -538,7 +538,7 @@ protobase import supabase [<connection> | --source-env <variable>] [--env DATABA
|
|
|
538
538
|
|
|
539
539
|
Turns a Supabase project's database into the project in the current directory, in two runs with the project's migrations between them. It needs PostgreSQL's `pg_dump` and `psql` on `PATH`, of the Supabase database's major version or newer, and reads the `.env` nearest the current directory for the project's database (`--env`).
|
|
540
540
|
|
|
541
|
-
1. **The first run** takes the Supabase database's connection string, for its `postgres` role (the session pooler's or the direct one; the transaction pooler cannot hold the snapshot the dumps share). It fails unless the database has Supabase's `auth` and `storage` schemas and its `supabase_admin` role, and while `auth.users` has single sign-on (SAML) users, which Supabase opens only through their identity provider and Better Auth would find by email
|
|
541
|
+
1. **The first run** takes the Supabase database's connection string, for its `postgres` role (the session pooler's or the direct one; the transaction pooler cannot hold the snapshot the dumps share). It fails unless the database has Supabase's `auth` and `storage` schemas and its `supabase_admin` role, and while `auth.users` has single sign-on (SAML) users, which Supabase opens only through their identity provider and Better Auth would find by email, so the import cannot carry them over. It then says how many share their address with another user and how many have one of their own, and prints `select id, email from auth.users where is_sso_user` to list them rather than their addresses. Deleting a user in Supabase deletes or breaks the app's rows that reference it, so what to do about these users is the developer's decision, made in Supabase before importing again. It notes the Supabase CLI's `supabase/config.toml`, `supabase/migrations/` and `supabase/functions/` when the checkout has them. It converts `supabase/functions` into the project's `functions/` (see [functions moved from Supabase](../guides/api-functions.md#functions-moved-from-supabase)): `Deno.serve(handler)` and std's `serve(handler)` become `export default defineFunction({ supabase: true }, { fetch: handler })`, an existing default export is wrapped the same way, so each function also answers at `/api/sb/functions/v1/<name>`, `npm:`, `jsr:` and esm.sh imports become bare imports of packages it adds to `package.json`, `Deno.env.get(name)` becomes `process.env[name]`, `EdgeRuntime.waitUntil` becomes `waitUntil` of `@protobase/server`, a function with `verify_jwt = false` in `config.toml` becomes public (`{ public: true, supabase: true }`), and `_shared` and the like are converted beside them. It stops before anything else when `functions/` already has one of their names. It dumps `auth`, `storage` and every schema the app made, schema and data apart, as of one snapshot, leaving out the platform's own migration tables, and writes the schemas as the project's next migration, `NNN_supabase_schema.sql`. It asks the project's database which of the app's extensions its role may create, by trying each in a transaction it rolls back: Postgres alone allows the trusted ones, Supabase's Postgres image all it allowlists, such as pg_net, pgmq and Vault. The migration creates those first, then, where pg_net is among them and the app has Database Webhooks, the `supabase_functions.http_request` function their triggers call, over pg_net; then the dump without what fails there, as comments: grants and default privileges for `dashboard_user`, `supabase_*`, `authenticator` and `postgres`, statements about the `public` schema, Realtime's publication, pgsodium's security labels, the views that decrypt with it and the triggers that encrypt with it, database webhooks without pg_net, functions in plv8, plls or plcoffee where the database has no such language, and a timescaledb hypertable's insert-blocking trigger. It then writes `NNN_supabase_users.sql`, the nullable columns Better Auth needs on `auth.users` (`name`, `email_verified`, `image`, `app_role`, `banned`, `ban_reason`, `ban_expires`) and GoTrue's `'authenticated'` as the default of `role` and `aud`. It writes `NNN_supabase_realtime.sql`, which makes `realtime.messages`, `realtime.topic()`, and Supabase's `realtime.send()` and `realtime.broadcast_changes()`, so the app's triggers and SQL keep [broadcasting from the database](supabase-realtime.md#broadcast-from-the-database), with the source's row security policies on `realtime.messages`, so its [private channels](supabase-realtime.md#private-channels) keep their access; a policy that names only Supabase's own roles is left out as a comment. It points the app at `auth.users`: the `auth` of `protobase.config.ts` gets `users: { table: 'users', columns: { role: 'app_role' }, metadata: 'raw_user_meta_data' }`, `passwordHashes: ['bcrypt']` and `generateUserId: 'uuid'`, or the config gets an `auth` of them when it has none (see [an existing users table](auth.md#an-existing-users-table)). It removes the migrations `protobase auth migration` or `protobase migration` wrote for Protobase's own user table, since `auth.users` replaces it, and stops before writing anything when the project has no `protobase.config.ts`, or one whose `auth` is not an object it can see, or when the project's database has a schema the dump creates already, such as the `auth` schema those migrations made. The project's database needs the roles `anon`, `authenticated` and `service_role` that the dump's grants and policies name; the command makes them when its role may, and otherwise stops, naming them. The data dump goes to `supabase-import/`, which it adds to `.gitignore`, since it holds every row and the users' password hashes. Where the project's database can create Vault, pgmq and pg_cron, `supabase-import/replay.sql` holds what a dump cannot carry: Vault's secrets with their values, read decrypted since only Supabase's key opens their ciphertext, pgmq's queues with their messages, since pgmq owns their tables, and pg_cron's jobs, which live in its own schema.
|
|
542
542
|
2. **Apply the migration** with the project's other migrations, such as `bun run db:migrate`.
|
|
543
543
|
3. **The second run**, without a connection string, loads the data in one transaction, with the app's triggers off so a sign-up trigger on `auth.users` does not run again for users who already have their rows, and fails the transaction unless every table holds the source's number of rows. In the same transaction it makes Vault's secrets and pgmq's queues again through `vault.create_secret`, `pgmq.create` and `pgmq.send`, each message with its headers and the time it becomes visible. Where the project's database has pg_cron, as Supabase's Postgres image does when it serves that database, it schedules pg_cron's jobs again there with `cron.schedule`, run by the importing role, an inactive one inactive. Elsewhere it writes each job of plain SQL as a [recurring job](jobs.md#recurring-jobs) of its own in `jobs/`, of the same name, schedule and `cron.timezone`, that runs its SQL as the app's database role, tried once each time as pg_cron did, and declares it in the `jobs` of `protobase.config.ts` (or writes one); they run once [`migration`](#migration) has added the jobs schema. Recurring jobs wake an app that sleeps, so they run where a host stops an idle app too. A job that calls what the database lacks, such as pg_net's `net.http_*`, `cron` or Vault, an inactive one and a schedule recurring jobs do not take, such as `30 seconds` or `$`, stay in the report, as does a job of another database. It loads only into empty tables, deletes the data dump and the replay, runs [`scaffold`](#scaffold) on the imported tables of the app's schemas (not `auth` or `storage`), giving the tables of the `supabase_realtime` publication [`.live()`](live.md) so [`/api/sb/realtime/v1`](supabase-realtime.md) keeps sending their `postgres_changes` once [`migration`](#migration) has written their capture triggers, writes Better Auth's other tables next to `auth.users` as [`auth migration`](#auth-migration) does (`NNN_auth.sql`), then `NNN_supabase_accounts.sql`, and writes `supabase-import/report.md`: the rows loaded, the API keys `/api/sb` needs and the `createClient` change to the app's `/api/sb` and its publishable key, the imported tables' size against the Starter plan's 500 MB, each sign-in provider's identities, what it made again, the tables it made live, and what needs a person: the pg_cron jobs it could not carry over, each with why, Realtime tables it made no resource of, Vault secrets by name and pgmq queues where the database has no such extension (and partitioned queues), the columns pgsodium encrypts, still holding Supabase's ciphertext, whose values to export decrypted in Supabase before declaring them [`.encrypted()`](data-config.md#encrypted-fields) and running [`encryption apply`](#encryption-apply), the extensions it cannot create, hypertables and plv8, plls or plcoffee functions to convert, triggers that call out over HTTP without pg_net, the converted Edge Functions with the packages to install and the code the conversion left (`deno.land/x` imports, Deno's file, KV and WebSocket APIs, `Supabase.ai`), and the indexes [`doctor`](#doctor) finds missing. `NNN_supabase_accounts.sql` fills the user columns from what Supabase Auth recorded and leaves them empty where it recorded nothing: the name from `raw_user_meta_data`'s `full_name` or `name`, the picture from its `avatar_url` or `picture`, `email_verified` from `email_confirmed_at`, and a ban from `banned_until`, which Better Auth lifts once it has passed. It gives each password of a confirmed address, as Supabase hashed it, a `credential` account (a user who never confirmed theirs signs in by code or resets the password, since GoTrue refused their password sign-in too), and each identity of a social provider an account under Better Auth's name for it (`azure` is `microsoft`, keyed on the `oid` claim; `linkedin_oidc` is `linkedin`, `slack_oidc` is `slack`, `x` is `twitter`), unless its user has an address they never confirmed, since GoTrue refused that sign-in too. Email, phone, anonymous and single sign-on identities and sessions do not carry over. <gap>The import cannot carry over MFA factors because that is not implemented.</gap> <gap>The import cannot carry over passkeys because that is not implemented.</gap>
|
|
544
544
|
4. **Apply the migrations** again. Supabase's users then sign in with their own passwords, and with a social provider once it is in the `socialProviders` of the config's `auth`; new users get a UUID in `auth.users`, so the app's triggers there run for them.
|