create-protobase 0.96.8 → 0.96.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/templates/automations/.env.example +8 -0
  2. package/dist/templates/automations/AGENTS.md +28 -0
  3. package/dist/templates/automations/_.gitignore +4 -0
  4. package/dist/templates/automations/_bunfig.toml +4 -0
  5. package/dist/templates/automations/config/index.ts +4 -0
  6. package/dist/templates/automations/config/user-menu.ts +4 -0
  7. package/dist/templates/automations/db/connection.ts +8 -0
  8. package/dist/templates/automations/db/migrate.ts +31 -0
  9. package/dist/templates/automations/db/migrations/.gitkeep +0 -0
  10. package/dist/templates/automations/db/migrations/001_auth.sql +36 -0
  11. package/dist/templates/automations/db/migrations/002_auth_snake_case.sql +109 -0
  12. package/dist/templates/automations/db/migrations/003_auth.sql +6 -0
  13. package/dist/templates/automations/db/migrations/004_auth.sql +4 -0
  14. package/dist/templates/automations/db/migrations/005_auth.sql +4 -0
  15. package/dist/templates/automations/db/migrations/006_auth.sql +319 -0
  16. package/dist/templates/automations/db/migrations/007_auth.sql +19 -0
  17. package/dist/templates/automations/db/migrations/008_auth.sql +20 -0
  18. package/dist/templates/automations/db/migrations/009_auth.sql +7 -0
  19. package/dist/templates/automations/db/migrations/010_jobs.sql +576 -0
  20. package/dist/templates/automations/db/migrations/011_workflows.sql +367 -0
  21. package/dist/templates/automations/node-types.d.ts +1 -0
  22. package/dist/templates/automations/package.json +39 -0
  23. package/dist/templates/automations/protobase.config.ts +15 -0
  24. package/dist/templates/automations/tsconfig.json +24 -0
  25. package/dist/templates/automations/vitest.config.ts +4 -0
  26. package/dist/templates/erp/package.json +6 -6
  27. package/dist/templates/property-portal/package.json +6 -6
  28. package/dist/templates/real-estate/package.json +6 -6
  29. package/dist/templates/scratch/package.json +6 -6
  30. package/package.json +2 -2
@@ -0,0 +1,8 @@
1
+ # The app's settings for `protobase dev`, `protobase serve` and the db:* scripts; a deployed app gets them from its host.
2
+ DATABASE_URL=postgres://protobase:protobase@localhost:55432/automations
3
+
4
+ # Admin store (Better Auth users, sessions, signing keys): the `auth` schema of DATABASE_URL, created by `db:migrate`.
5
+ PROTOBASE_URL=http://localhost:5173
6
+ # Signs cookies and encrypts the token keys. Generate your own: openssl rand -base64 32
7
+ PROTOBASE_SECRET=change-me-generate-with-openssl-rand-base64-32
8
+ # PROTOBASE_TRUSTED_ORIGINS=
@@ -0,0 +1,28 @@
1
+ # Working on this app
2
+
3
+ A Protobase app: each resource's data config in `config/<name>/data.ts` and its screens in `config/<name>/ui.ts`, all
4
+ exported from `config/index.ts`, over a Postgres database. Reference: https://docs.protobase.net. It starts with
5
+ workflows on (`workflows: {}` in `protobase.config.ts`, https://docs.protobase.net/reference/workflows/) and no
6
+ resources: the first change adds its first tables and resources.
7
+
8
+ ## Database changes
9
+
10
+ - Every schema change is a new migration file in `db/migrations/`, numbered after the last one (`012_customers.sql`
11
+ after the auth, jobs and workflows migrations). Never edit or remove a migration that exists: it has already run on
12
+ the live database.
13
+ - Expand only, where possible: add tables, columns and indexes, and keep what the running app still reads. Rename or
14
+ drop only when asked to, once nothing uses it.
15
+
16
+ ## Never touch
17
+
18
+ - `auth/` and the `auth` schema: sign-in and accounts.
19
+ - `.github/workflows/`.
20
+
21
+ ## Scripts
22
+
23
+ The platform runs `db:migrate` (the auth schema's migrations included), `typecheck` and `test`, with their settings in
24
+ the environment: keep them working. Run `typecheck` and `test` before you finish.
25
+
26
+ `bun run dev` serves the app locally. Nobody can sign in until a user exists in the database it points at: its startup
27
+ message prints the exact command while there are none, `bun run protobase users create you@example.com
28
+ --generate-password`. A database with users already, such as the hosted one, needs a new email for a local user.
@@ -0,0 +1,4 @@
1
+ node_modules
2
+ dist
3
+ .env
4
+ data
@@ -0,0 +1,4 @@
1
+ # `bun run` runs scripts and package bins on Bun even when Node is on PATH, whose `#!/usr/bin/env node` would otherwise
2
+ # start Node.
3
+ [run]
4
+ bun = true
@@ -0,0 +1,4 @@
1
+ // Every resource, view and page of the app, exported from here: `export * from './orders/data'`. A new app has no
2
+ // resources yet, only the Users page, for admins; it is deployed with its first change.
3
+ export { usersPage } from '@protobase/layout'
4
+ export { accountMenu } from './user-menu'
@@ -0,0 +1,4 @@
1
+ import { userMenu } from '@protobase/schema'
2
+
3
+ /** The profile menu's own pages: Users, for admins. */
4
+ export const accountMenu = userMenu((m) => [m.page('accounts', { label: 'Users', icon: 'users' })])
@@ -0,0 +1,8 @@
1
+ import postgres from 'postgres'
2
+
3
+ const url = process.env.DATABASE_URL
4
+ if (!url) throw new Error('DATABASE_URL is not set; put it in .env')
5
+
6
+ export const databaseUrl = url
7
+
8
+ export const connect = (options: postgres.Options<{}> = {}) => postgres(databaseUrl, { onnotice: () => {}, ...options })
@@ -0,0 +1,31 @@
1
+ import { readdir, readFile } from 'node:fs/promises'
2
+ import { fileURLToPath } from 'node:url'
3
+ import { connect } from './connection'
4
+
5
+ // Applies the migrations not applied yet, the app's and the auth schema's alike, in file name order and in one
6
+ // transaction: when one fails, none is applied and the database stays as the running version expects it. Run it before
7
+ // a new version serves, while nothing else writes. A statement Postgres runs only outside a transaction, such as
8
+ // `create index concurrently`, does not belong in these files.
9
+
10
+ const directory = fileURLToPath(new URL('./migrations/', import.meta.url))
11
+ // Any constant shared by every run of this script: two runs at once take turns.
12
+ const lockKey = 7_311_946_202
13
+
14
+ const sql = connect({ max: 1 })
15
+ const files = (await readdir(directory)).filter((file) => file.endsWith('.sql')).sort()
16
+
17
+ const applied = await sql.begin(async (tx) => {
18
+ await tx`select pg_advisory_xact_lock(${lockKey})`
19
+ await tx`create schema if not exists migrations`
20
+ await tx`create table if not exists migrations.applied (name text primary key, applied_at timestamptz not null default now())`
21
+ const done = new Set((await tx`select name from migrations.applied`).map((row) => row.name))
22
+ const pending = files.filter((file) => !done.has(file))
23
+ for (const file of pending) {
24
+ await tx.unsafe(await readFile(directory + file, 'utf8'))
25
+ await tx`insert into migrations.applied (name) values (${file})`
26
+ }
27
+ return pending
28
+ })
29
+ for (const file of applied) console.log(`applied ${file}`)
30
+
31
+ await sql.end()
@@ -0,0 +1,36 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ create schema if not exists "auth";
5
+
6
+ create table if not exists "auth"."user" ("id" text not null primary key, "name" text not null, "email" text not null unique, "email_verified" boolean not null, "image" text, "created_at" timestamptz default CURRENT_TIMESTAMP not null, "updated_at" timestamptz default CURRENT_TIMESTAMP not null, "role" text, "banned" boolean, "ban_reason" text, "ban_expires" timestamptz, "two_factor_enabled" boolean);
7
+
8
+ create table if not exists "auth"."session" ("id" text not null primary key, "expires_at" timestamptz not null, "token" text not null unique, "created_at" timestamptz default CURRENT_TIMESTAMP not null, "updated_at" timestamptz not null, "ip_address" text, "user_agent" text, "user_id" text not null references "auth"."user" ("id") on delete cascade, "impersonated_by" text);
9
+
10
+ create table if not exists "auth"."account" ("id" text not null primary key, "account_id" text not null, "provider_id" text not null, "user_id" text not null references "auth"."user" ("id") on delete cascade, "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamptz, "refresh_token_expires_at" timestamptz, "scope" text, "password" text, "created_at" timestamptz default CURRENT_TIMESTAMP not null, "updated_at" timestamptz not null);
11
+
12
+ create table if not exists "auth"."verification" ("id" text not null primary key, "identifier" text not null, "value" text not null, "expires_at" timestamptz not null, "created_at" timestamptz default CURRENT_TIMESTAMP not null, "updated_at" timestamptz default CURRENT_TIMESTAMP not null);
13
+
14
+ create table if not exists "auth"."jwks" ("id" text not null primary key, "public_key" text not null, "private_key" text not null, "created_at" timestamptz not null, "expires_at" timestamptz, "alg" text, "crv" text);
15
+
16
+ create table if not exists "auth"."two_factor" ("id" text not null primary key, "secret" text not null, "backup_codes" text not null, "user_id" text not null references "auth"."user" ("id") on delete cascade, "verified" boolean, "failed_verification_count" integer, "locked_until" timestamptz);
17
+
18
+ create table if not exists "auth"."passkey" ("id" text not null primary key, "name" text, "public_key" text not null, "user_id" text not null references "auth"."user" ("id") on delete cascade, "credential_id" text not null, "counter" integer not null, "device_type" text not null, "backed_up" boolean not null, "transports" text, "created_at" timestamptz, "aaguid" text);
19
+
20
+ create table if not exists "auth"."sign_in_policy" ("id" text not null primary key, "password" text not null, "email_code" text not null, "passkey" text not null, "two_factor" text not null, "staff_access" text not null, "platform_sign_in" text not null, "created_at" timestamptz not null, "created_by" text);
21
+
22
+ create table if not exists "auth"."staff_sign_in" ("id" text not null primary key, "user_id" text not null, "user_email" text not null, "staff" text not null, "staff_name" text, "staff_subject" text not null, "issuer" text not null, "reason" text not null, "session_id" text not null, "started_at" timestamptz not null, "expires_at" timestamptz not null, "ended_at" timestamptz);
23
+
24
+ create index if not exists "session_user_id_idx" on "auth"."session" ("user_id");
25
+
26
+ create index if not exists "account_user_id_idx" on "auth"."account" ("user_id");
27
+
28
+ create index if not exists "verification_identifier_idx" on "auth"."verification" ("identifier");
29
+
30
+ create index if not exists "two_factor_secret_idx" on "auth"."two_factor" ("secret");
31
+
32
+ create index if not exists "two_factor_user_id_idx" on "auth"."two_factor" ("user_id");
33
+
34
+ create index if not exists "passkey_user_id_idx" on "auth"."passkey" ("user_id");
35
+
36
+ create index if not exists "passkey_credential_id_idx" on "auth"."passkey" ("credential_id");
@@ -0,0 +1,109 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ -- Renames the auth store of Protobase 0.6 to snake_case. A store set up with the snake_case names has no "emailVerified"
5
+ -- and is left as it is.
6
+ do $$
7
+ declare
8
+ constraint_row record;
9
+ begin
10
+ if not exists (select from information_schema.columns where table_schema = 'auth' and table_name = 'user' and column_name = 'emailVerified') then
11
+ return;
12
+ end if;
13
+
14
+ alter table "auth"."twoFactor" rename to two_factor;
15
+ alter table "auth"."signInPolicy" rename to sign_in_policy;
16
+ alter table "auth"."staffSignIn" rename to staff_sign_in;
17
+
18
+ alter table "auth"."user" rename column "emailVerified" to email_verified;
19
+ alter table "auth"."user" rename column "createdAt" to created_at;
20
+ alter table "auth"."user" rename column "updatedAt" to updated_at;
21
+ alter table "auth"."user" rename column "banReason" to ban_reason;
22
+ alter table "auth"."user" rename column "banExpires" to ban_expires;
23
+ alter table "auth"."user" rename column "twoFactorEnabled" to two_factor_enabled;
24
+
25
+ alter table "auth".session rename column "expiresAt" to expires_at;
26
+ alter table "auth".session rename column "createdAt" to created_at;
27
+ alter table "auth".session rename column "updatedAt" to updated_at;
28
+ alter table "auth".session rename column "ipAddress" to ip_address;
29
+ alter table "auth".session rename column "userAgent" to user_agent;
30
+ alter table "auth".session rename column "userId" to user_id;
31
+ alter table "auth".session rename column "impersonatedBy" to impersonated_by;
32
+
33
+ alter table "auth".account rename column "accountId" to account_id;
34
+ alter table "auth".account rename column "providerId" to provider_id;
35
+ alter table "auth".account rename column "userId" to user_id;
36
+ alter table "auth".account rename column "accessToken" to access_token;
37
+ alter table "auth".account rename column "refreshToken" to refresh_token;
38
+ alter table "auth".account rename column "idToken" to id_token;
39
+ alter table "auth".account rename column "accessTokenExpiresAt" to access_token_expires_at;
40
+ alter table "auth".account rename column "refreshTokenExpiresAt" to refresh_token_expires_at;
41
+ alter table "auth".account rename column "createdAt" to created_at;
42
+ alter table "auth".account rename column "updatedAt" to updated_at;
43
+
44
+ alter table "auth".verification rename column "expiresAt" to expires_at;
45
+ alter table "auth".verification rename column "createdAt" to created_at;
46
+ alter table "auth".verification rename column "updatedAt" to updated_at;
47
+
48
+ alter table "auth".jwks rename column "publicKey" to public_key;
49
+ alter table "auth".jwks rename column "privateKey" to private_key;
50
+ alter table "auth".jwks rename column "createdAt" to created_at;
51
+ alter table "auth".jwks rename column "expiresAt" to expires_at;
52
+
53
+ alter table "auth".two_factor rename column "backupCodes" to backup_codes;
54
+ alter table "auth".two_factor rename column "userId" to user_id;
55
+ alter table "auth".two_factor rename column "failedVerificationCount" to failed_verification_count;
56
+ alter table "auth".two_factor rename column "lockedUntil" to locked_until;
57
+
58
+ alter table "auth".passkey rename column "publicKey" to public_key;
59
+ alter table "auth".passkey rename column "userId" to user_id;
60
+ alter table "auth".passkey rename column "credentialID" to credential_id;
61
+ alter table "auth".passkey rename column "deviceType" to device_type;
62
+ alter table "auth".passkey rename column "backedUp" to backed_up;
63
+ alter table "auth".passkey rename column "createdAt" to created_at;
64
+
65
+ alter table "auth".sign_in_policy rename column "emailCode" to email_code;
66
+ alter table "auth".sign_in_policy rename column "twoFactor" to two_factor;
67
+ alter table "auth".sign_in_policy rename column "staffAccess" to staff_access;
68
+ alter table "auth".sign_in_policy rename column "platformSignIn" to platform_sign_in;
69
+ alter table "auth".sign_in_policy rename column "createdAt" to created_at;
70
+ alter table "auth".sign_in_policy rename column "createdBy" to created_by;
71
+
72
+ alter table "auth".staff_sign_in rename column "userId" to user_id;
73
+ alter table "auth".staff_sign_in rename column "userEmail" to user_email;
74
+ alter table "auth".staff_sign_in rename column "staffName" to staff_name;
75
+ alter table "auth".staff_sign_in rename column "staffSubject" to staff_subject;
76
+ alter table "auth".staff_sign_in rename column "sessionId" to session_id;
77
+ alter table "auth".staff_sign_in rename column "startedAt" to started_at;
78
+ alter table "auth".staff_sign_in rename column "expiresAt" to expires_at;
79
+ alter table "auth".staff_sign_in rename column "endedAt" to ended_at;
80
+
81
+ -- A primary key's index takes the new name with it.
82
+ alter table "auth".two_factor rename constraint "twoFactor_pkey" to two_factor_pkey;
83
+ alter table "auth".sign_in_policy rename constraint "signInPolicy_pkey" to sign_in_policy_pkey;
84
+ alter table "auth".staff_sign_in rename constraint "staffSignIn_pkey" to staff_sign_in_pkey;
85
+ alter table "auth".session rename constraint "session_userId_fkey" to session_user_id_fkey;
86
+ alter table "auth".account rename constraint "account_userId_fkey" to account_user_id_fkey;
87
+ alter table "auth".two_factor rename constraint "twoFactor_userId_fkey" to two_factor_user_id_fkey;
88
+ alter table "auth".passkey rename constraint "passkey_userId_fkey" to passkey_user_id_fkey;
89
+
90
+ alter index "auth"."session_userId_idx" rename to session_user_id_idx;
91
+ alter index "auth"."account_userId_idx" rename to account_user_id_idx;
92
+ alter index "auth"."twoFactor_secret_idx" rename to two_factor_secret_idx;
93
+ alter index "auth"."twoFactor_userId_idx" rename to two_factor_user_id_idx;
94
+ alter index "auth"."passkey_userId_idx" rename to passkey_user_id_idx;
95
+ alter index "auth"."passkey_credentialID_idx" rename to passkey_credential_id_idx;
96
+
97
+ -- Postgres 18 names each not-null constraint after its table and column, as "user_emailVerified_not_null"; earlier
98
+ -- versions keep no such constraints, so there is nothing to rename there.
99
+ for constraint_row in
100
+ select c.conrelid::regclass as table_name, c.conname as old_name, t.relname || '_' || a.attname || '_not_null' as new_name
101
+ from pg_constraint c
102
+ join pg_class t on t.oid = c.conrelid
103
+ join pg_attribute a on a.attrelid = c.conrelid and a.attnum = c.conkey[1]
104
+ where c.contype = 'n' and t.relnamespace = (select oid from pg_namespace where nspname = 'auth') and c.conname ~ '[A-Z]'
105
+ loop
106
+ execute format('alter table %s rename constraint %I to %I', constraint_row.table_name, constraint_row.old_name, constraint_row.new_name);
107
+ end loop;
108
+ end
109
+ $$;
@@ -0,0 +1,6 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ alter table "auth"."user" add column if not exists "last_role" text;
5
+
6
+ alter table "auth"."session" add column if not exists "active_role" text;
@@ -0,0 +1,4 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ alter table "auth"."passkey" add column if not exists "rp_id" text;
@@ -0,0 +1,4 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ alter table "auth"."user" add column if not exists "email_confirmed_at" timestamptz;
@@ -0,0 +1,319 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ -- The record of the auth store's migrations.
5
+ create schema if not exists "auth";
6
+
7
+ create table "auth"."auth_migrations" (
8
+ version integer primary key,
9
+ name text not null,
10
+ applied_at timestamptz default current_timestamp not null
11
+ );
12
+
13
+ -- The auth store an earlier Protobase made, without a record of its migrations, adopted: 1 core, 2 two_factor, 3 passkeys, 4 sign_in_policy, 5 staff_sign_in
14
+ -- recorded as applied, once every table and column they make is there.
15
+ do $adopt$
16
+ declare
17
+ missing text;
18
+ begin
19
+ select string_agg(expected.table_name || '.' || expected.column_name, ', ' order by expected.table_name, expected.column_name) into missing
20
+ from (values ('account', 'id', 'text'),
21
+ ('account', 'account_id', 'text'),
22
+ ('account', 'provider_id', 'text'),
23
+ ('account', 'user_id', 'text'),
24
+ ('account', 'access_token', 'text'),
25
+ ('account', 'refresh_token', 'text'),
26
+ ('account', 'id_token', 'text'),
27
+ ('account', 'access_token_expires_at', 'timestamptz'),
28
+ ('account', 'refresh_token_expires_at', 'timestamptz'),
29
+ ('account', 'scope', 'text'),
30
+ ('account', 'password', 'text'),
31
+ ('account', 'created_at', 'timestamptz'),
32
+ ('account', 'updated_at', 'timestamptz'),
33
+ ('jwks', 'id', 'text'),
34
+ ('jwks', 'public_key', 'text'),
35
+ ('jwks', 'private_key', 'text'),
36
+ ('jwks', 'created_at', 'timestamptz'),
37
+ ('jwks', 'expires_at', 'timestamptz'),
38
+ ('jwks', 'alg', 'text'),
39
+ ('jwks', 'crv', 'text'),
40
+ ('session', 'id', 'text'),
41
+ ('session', 'expires_at', 'timestamptz'),
42
+ ('session', 'token', 'text'),
43
+ ('session', 'created_at', 'timestamptz'),
44
+ ('session', 'updated_at', 'timestamptz'),
45
+ ('session', 'ip_address', 'text'),
46
+ ('session', 'user_agent', 'text'),
47
+ ('session', 'user_id', 'text'),
48
+ ('session', 'impersonated_by', 'text'),
49
+ ('session', 'active_role', 'text'),
50
+ ('user', 'id', 'text'),
51
+ ('user', 'name', 'text'),
52
+ ('user', 'email', 'text'),
53
+ ('user', 'email_verified', 'bool'),
54
+ ('user', 'image', 'text'),
55
+ ('user', 'created_at', 'timestamptz'),
56
+ ('user', 'updated_at', 'timestamptz'),
57
+ ('user', 'role', 'text'),
58
+ ('user', 'banned', 'bool'),
59
+ ('user', 'ban_reason', 'text'),
60
+ ('user', 'ban_expires', 'timestamptz'),
61
+ ('user', 'last_role', 'text'),
62
+ ('user', 'email_confirmed_at', 'timestamptz'),
63
+ ('verification', 'id', 'text'),
64
+ ('verification', 'identifier', 'text'),
65
+ ('verification', 'value', 'text'),
66
+ ('verification', 'expires_at', 'timestamptz'),
67
+ ('verification', 'created_at', 'timestamptz'),
68
+ ('verification', 'updated_at', 'timestamptz'),
69
+ ('two_factor', 'id', 'text'),
70
+ ('two_factor', 'secret', 'text'),
71
+ ('two_factor', 'backup_codes', 'text'),
72
+ ('two_factor', 'user_id', 'text'),
73
+ ('two_factor', 'verified', 'bool'),
74
+ ('two_factor', 'failed_verification_count', 'int4'),
75
+ ('two_factor', 'locked_until', 'timestamptz'),
76
+ ('user', 'two_factor_enabled', 'bool'),
77
+ ('passkey', 'id', 'text'),
78
+ ('passkey', 'name', 'text'),
79
+ ('passkey', 'public_key', 'text'),
80
+ ('passkey', 'user_id', 'text'),
81
+ ('passkey', 'credential_id', 'text'),
82
+ ('passkey', 'counter', 'int4'),
83
+ ('passkey', 'device_type', 'text'),
84
+ ('passkey', 'backed_up', 'bool'),
85
+ ('passkey', 'transports', 'text'),
86
+ ('passkey', 'created_at', 'timestamptz'),
87
+ ('passkey', 'aaguid', 'text'),
88
+ ('passkey', 'rp_id', 'text'),
89
+ ('sign_in_policy', 'id', 'text'),
90
+ ('sign_in_policy', 'password', 'text'),
91
+ ('sign_in_policy', 'email_code', 'text'),
92
+ ('sign_in_policy', 'passkey', 'text'),
93
+ ('sign_in_policy', 'two_factor', 'text'),
94
+ ('sign_in_policy', 'staff_access', 'text'),
95
+ ('sign_in_policy', 'platform_sign_in', 'text'),
96
+ ('sign_in_policy', 'created_at', 'timestamptz'),
97
+ ('sign_in_policy', 'created_by', 'text'),
98
+ ('staff_sign_in', 'id', 'text'),
99
+ ('staff_sign_in', 'user_id', 'text'),
100
+ ('staff_sign_in', 'user_email', 'text'),
101
+ ('staff_sign_in', 'staff', 'text'),
102
+ ('staff_sign_in', 'staff_name', 'text'),
103
+ ('staff_sign_in', 'staff_subject', 'text'),
104
+ ('staff_sign_in', 'issuer', 'text'),
105
+ ('staff_sign_in', 'reason', 'text'),
106
+ ('staff_sign_in', 'session_id', 'text'),
107
+ ('staff_sign_in', 'started_at', 'timestamptz'),
108
+ ('staff_sign_in', 'expires_at', 'timestamptz'),
109
+ ('staff_sign_in', 'ended_at', 'timestamptz')) as expected (table_name, column_name, udt_name)
110
+ where not exists (
111
+ select from information_schema.columns found
112
+ where found.table_schema = 'auth' and found.table_name = expected.table_name and found.column_name = expected.column_name and found.udt_name = expected.udt_name
113
+ );
114
+ if missing is not null then
115
+ raise exception 'The auth store does not match the migrations this file adopts: it lacks %, or has them as other types', missing;
116
+ end if;
117
+ end
118
+ $adopt$;
119
+
120
+ insert into "auth"."auth_migrations" (version, name) values (1, 'core'), (2, 'two_factor'), (3, 'passkeys'), (4, 'sign_in_policy'), (5, 'staff_sign_in');
121
+
122
+ -- 6 organizations: Organizations, their members and their invitations.
123
+ alter table "auth"."user" add column if not exists "last_organization_id" text;
124
+
125
+ alter table "auth"."session" add column active_organization_id text;
126
+
127
+ create table "auth"."organization" (
128
+ id text primary key,
129
+ name text not null,
130
+ slug text not null unique,
131
+ logo text,
132
+ created_at timestamptz not null,
133
+ metadata text
134
+ );
135
+
136
+ create table "auth"."member" (
137
+ id text primary key,
138
+ organization_id text not null references "auth"."organization" (id) on delete cascade,
139
+ user_id text not null references "auth"."user" (id) on delete cascade,
140
+ role text not null,
141
+ created_at timestamptz not null,
142
+ app_roles jsonb
143
+ );
144
+
145
+ create index member_organization_id_idx on "auth"."member" (organization_id);
146
+
147
+ create index member_user_id_idx on "auth"."member" (user_id);
148
+
149
+ create table "auth"."invitation" (
150
+ id text primary key,
151
+ organization_id text not null references "auth"."organization" (id) on delete cascade,
152
+ email text not null,
153
+ role text,
154
+ status text not null,
155
+ expires_at timestamptz not null,
156
+ created_at timestamptz default current_timestamp not null,
157
+ inviter_id text not null references "auth"."user" (id) on delete cascade,
158
+ app_roles jsonb
159
+ );
160
+
161
+ create index invitation_organization_id_idx on "auth"."invitation" (organization_id);
162
+
163
+ create index invitation_email_idx on "auth"."invitation" (email);
164
+
165
+ insert into "auth"."auth_migrations" (version, name) values (6, 'organizations');
166
+
167
+ -- 7 mcp: The clients, resources, tokens and consents of the OAuth provider for MCP.
168
+ create table "auth"."oauth_client" (
169
+ id text primary key,
170
+ client_id text not null unique,
171
+ client_secret text,
172
+ client_discovery_id text,
173
+ disabled boolean,
174
+ skip_consent boolean,
175
+ enable_end_session boolean,
176
+ subject_type text,
177
+ scopes jsonb,
178
+ client_credentials_scopes jsonb,
179
+ user_id text references "auth"."user" (id) on delete cascade,
180
+ created_at timestamptz,
181
+ updated_at timestamptz,
182
+ name text,
183
+ uri text,
184
+ icon text,
185
+ contacts jsonb,
186
+ tos text,
187
+ policy text,
188
+ software_id text,
189
+ software_version text,
190
+ software_statement text,
191
+ redirect_uris jsonb not null,
192
+ post_logout_redirect_uris jsonb,
193
+ backchannel_logout_uri text,
194
+ backchannel_logout_session_required boolean,
195
+ token_endpoint_auth_method text,
196
+ application_type text,
197
+ jwks text,
198
+ jwks_uri text,
199
+ grant_types jsonb,
200
+ response_types jsonb,
201
+ require_pkce boolean,
202
+ dpop_bound_access_tokens boolean,
203
+ reference_id text,
204
+ metadata jsonb
205
+ );
206
+
207
+ create index oauth_client_user_id_idx on "auth"."oauth_client" (user_id);
208
+
209
+ create table "auth"."oauth_resource" (
210
+ id text primary key,
211
+ identifier text not null unique,
212
+ name text not null,
213
+ access_token_ttl integer,
214
+ refresh_token_ttl integer,
215
+ signing_algorithm text,
216
+ signing_key_id text,
217
+ allowed_scopes jsonb,
218
+ custom_claims jsonb,
219
+ dpop_bound_access_tokens_required boolean,
220
+ disabled boolean,
221
+ created_at timestamptz,
222
+ updated_at timestamptz,
223
+ policy_version integer,
224
+ metadata jsonb
225
+ );
226
+
227
+ create table "auth"."oauth_client_resource" (
228
+ id text primary key,
229
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
230
+ resource_id text not null references "auth"."oauth_resource" (identifier) on delete cascade,
231
+ metadata jsonb,
232
+ created_at timestamptz
233
+ );
234
+
235
+ create index oauth_client_resource_client_id_idx on "auth"."oauth_client_resource" (client_id);
236
+
237
+ create index oauth_client_resource_resource_id_idx on "auth"."oauth_client_resource" (resource_id);
238
+
239
+ create unique index oauth_client_resource_client_id_resource_id_uidx on "auth"."oauth_client_resource" (client_id, resource_id);
240
+
241
+ create table "auth"."oauth_refresh_token" (
242
+ id text primary key,
243
+ token text not null unique,
244
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
245
+ session_id text references "auth"."session" (id) on delete set null,
246
+ user_id text not null references "auth"."user" (id) on delete cascade,
247
+ reference_id text,
248
+ authorization_code_id text,
249
+ resources jsonb,
250
+ requested_user_info_claims jsonb,
251
+ expires_at timestamptz not null,
252
+ created_at timestamptz not null,
253
+ revoked timestamptz,
254
+ rotated_at timestamptz,
255
+ rotation_replay_response text,
256
+ rotation_replay_expires_at timestamptz,
257
+ auth_time timestamptz,
258
+ confirmation jsonb,
259
+ scopes jsonb not null
260
+ );
261
+
262
+ create index oauth_refresh_token_client_id_idx on "auth"."oauth_refresh_token" (client_id);
263
+
264
+ create index oauth_refresh_token_session_id_idx on "auth"."oauth_refresh_token" (session_id);
265
+
266
+ create index oauth_refresh_token_user_id_idx on "auth"."oauth_refresh_token" (user_id);
267
+
268
+ create index oauth_refresh_token_authorization_code_id_idx on "auth"."oauth_refresh_token" (authorization_code_id);
269
+
270
+ create table "auth"."oauth_access_token" (
271
+ id text primary key,
272
+ token text not null unique,
273
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
274
+ session_id text references "auth"."session" (id) on delete set null,
275
+ user_id text references "auth"."user" (id) on delete cascade,
276
+ reference_id text,
277
+ authorization_code_id text,
278
+ resources jsonb,
279
+ requested_user_info_claims jsonb,
280
+ refresh_id text references "auth"."oauth_refresh_token" (id) on delete cascade,
281
+ expires_at timestamptz not null,
282
+ created_at timestamptz not null,
283
+ revoked timestamptz,
284
+ confirmation jsonb,
285
+ scopes jsonb not null
286
+ );
287
+
288
+ create index oauth_access_token_client_id_idx on "auth"."oauth_access_token" (client_id);
289
+
290
+ create index oauth_access_token_session_id_idx on "auth"."oauth_access_token" (session_id);
291
+
292
+ create index oauth_access_token_user_id_idx on "auth"."oauth_access_token" (user_id);
293
+
294
+ create index oauth_access_token_authorization_code_id_idx on "auth"."oauth_access_token" (authorization_code_id);
295
+
296
+ create index oauth_access_token_refresh_id_idx on "auth"."oauth_access_token" (refresh_id);
297
+
298
+ create table "auth"."oauth_consent" (
299
+ id text primary key,
300
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
301
+ user_id text references "auth"."user" (id) on delete cascade,
302
+ reference_id text,
303
+ resources jsonb,
304
+ requested_user_info_claims jsonb,
305
+ scopes jsonb not null,
306
+ created_at timestamptz not null,
307
+ updated_at timestamptz not null
308
+ );
309
+
310
+ create index oauth_consent_client_id_idx on "auth"."oauth_consent" (client_id);
311
+
312
+ create index oauth_consent_user_id_idx on "auth"."oauth_consent" (user_id);
313
+
314
+ create table "auth"."oauth_client_assertion" (
315
+ id text primary key,
316
+ expires_at timestamptz not null
317
+ );
318
+
319
+ insert into "auth"."auth_migrations" (version, name) values (7, 'mcp');
@@ -0,0 +1,19 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ -- 8 personal_access_tokens: The personal access tokens people make for their scripts, by their tokens' hashes.
5
+ create table "auth"."personal_access_token" (
6
+ id text primary key,
7
+ user_id text not null references "auth"."user" (id) on delete cascade,
8
+ name text not null,
9
+ token_hash text not null unique,
10
+ role text not null,
11
+ organization_id text references "auth"."organization" (id) on delete cascade,
12
+ created_at timestamptz not null,
13
+ expires_at timestamptz,
14
+ last_used_at timestamptz
15
+ );
16
+
17
+ create index personal_access_token_user_id_idx on "auth"."personal_access_token" (user_id);
18
+
19
+ insert into "auth"."auth_migrations" (version, name) values (8, 'personal_access_tokens');
@@ -0,0 +1,20 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ -- 9 mail_senders: The From addresses people send mail from through the mailbox they linked.
5
+ create table "auth"."mail_sender" (
6
+ id text primary key,
7
+ user_id text not null references "auth"."user" (id) on delete cascade,
8
+ address text not null,
9
+ shared boolean not null,
10
+ signature text,
11
+ is_default boolean not null,
12
+ created_at timestamptz not null,
13
+ unique (user_id, address)
14
+ );
15
+
16
+ create unique index mail_sender_default_idx on "auth"."mail_sender" (user_id) where is_default;
17
+
18
+ create index mail_sender_shared_idx on "auth"."mail_sender" (address) where shared;
19
+
20
+ insert into "auth"."auth_migrations" (version, name) values (9, 'mail_senders');
@@ -0,0 +1,7 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ -- 10 mail_sender_names: The display names of the From addresses people send mail from.
5
+ alter table "auth"."mail_sender" add column name text;
6
+
7
+ insert into "auth"."auth_migrations" (version, name) values (10, 'mail_sender_names');