@otwdesign/cli 1.1.0 → 1.2.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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/src/migrate.ts +26 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@otwdesign/cli",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "The cms command for OTW CMS sites: generate, migrate, update and serve.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "OTW SRL",
@@ -41,9 +41,9 @@
41
41
  "@types/semver": "^7.7.0",
42
42
  "semver": "^7.8.0",
43
43
  "tsx": "^4.23.15",
44
- "@otwdesign/next": "1.1.0",
45
- "@otwdesign/core": "1.1.0",
46
- "@otwdesign/sdk": "1.1.0"
44
+ "@otwdesign/core": "1.2.0",
45
+ "@otwdesign/sdk": "1.2.0",
46
+ "@otwdesign/next": "1.2.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@opentelemetry/api": "~1.9.0",
package/src/migrate.ts CHANGED
@@ -4,7 +4,12 @@ import { dirname, join } from 'node:path'
4
4
  import { promisify } from 'node:util'
5
5
  import { migrateSite, type Env } from '@otwdesign/core'
6
6
  import type { Site } from '@otwdesign/next'
7
- import { activePluginsFromEnv, dataDir, openDatabase } from '@otwdesign/next/server'
7
+ import {
8
+ activePluginsFromEnv,
9
+ dataDir,
10
+ openDatabase,
11
+ supabasePoolerUrl,
12
+ } from '@otwdesign/next/server'
8
13
  import type { Logger } from '@otwdesign/sdk'
9
14
 
10
15
  const run = promisify(execFile)
@@ -34,9 +39,25 @@ async function pgDumpAvailable(): Promise<boolean> {
34
39
  }
35
40
 
36
41
  /**
37
- * Copies the embedded database's directory, or dumps Postgres with `pg_dump`. The password goes
38
- * through the environment, not the command line, where other users could read it.
42
+ * `DATABASE_URL` as `pg_dump` takes it, with the password apart, for the environment rather than
43
+ * the command line, where other users could read it. A dump runs in one session, so Supabase's
44
+ * pooler is dumped in session mode. node-postgres's own `uselibpqcompat` goes: libpq refuses
45
+ * parameters it doesn't know, and its own `sslmode` already means what that one asks for.
39
46
  */
47
+ export function pgDumpTarget(databaseUrl: string): { url: string; password: string } {
48
+ const url = new URL(supabasePoolerUrl(databaseUrl, 'session'))
49
+ const password = decodeURIComponent(url.password)
50
+ url.password = ''
51
+ // By hand rather than through searchParams, which would write the others' %20 as +.
52
+ const kept = url.search
53
+ .slice(1)
54
+ .split('&')
55
+ .filter((pair) => pair.split('=')[0] !== 'uselibpqcompat')
56
+ url.search = kept.join('&')
57
+ return { url: url.href, password }
58
+ }
59
+
60
+ /** Copies the embedded database's directory, or dumps Postgres with `pg_dump`. */
40
61
  export async function backUp(
41
62
  options: Pick<MigrateOptions, 'root' | 'env' | 'now' | 'hasPgDump'> & {
42
63
  /** The command that skips the backup, for the error when `pg_dump` is missing. */
@@ -52,12 +73,10 @@ export async function backUp(
52
73
  `pg_dump is not installed, so the database cannot be backed up first. Take a backup (many hosts do this for you), then run \`${options.withoutBackup ?? 'cms migrate --no-backup'}\`.`,
53
74
  )
54
75
  }
55
- const url = new URL(env.DATABASE_URL)
56
- const password = decodeURIComponent(url.password)
57
- url.password = ''
76
+ const { url, password } = pgDumpTarget(env.DATABASE_URL)
58
77
  const file = join(dir, `postgres-${stamp}.dump`)
59
78
  await mkdir(dir, { recursive: true })
60
- await run('pg_dump', ['--format=custom', `--file=${file}`, url.toString()], {
79
+ await run('pg_dump', ['--format=custom', `--file=${file}`, url], {
61
80
  env: { ...process.env, ...(password ? { PGPASSWORD: password } : {}) },
62
81
  })
63
82
  return file