Database naming rule; seeding after launch touches named records only
- Rule (VERY IMPORTANT): databases are named <domain without dots>_prod /
_dev (intecion.com → intecioncom_prod / intecioncom_dev) on MongoDB and
Postgres — AGENT.md top rules, deployment.md §4.2a (Coolify), env.md,
ipal-setup architecture questions (derive, then confirm), AGENTS.md
template. ipalEnvRules({ databaseName }) warns (never fails) on a
mismatch, domain from NEXT_PUBLIC_SERVER_URL or { domain }, { warn }
overridable; helpers databaseNamesFor, databaseNameFromUri,
checkDatabaseName from the root and /env.
- Rule (VERY IMPORTANT): after launch never re-seed everything — AGENT.md,
seeding.md, ipal-site, ipal-form, ipal-reference. createSeeder gains
`only` / SEED_ONLY (pages:slug, forms:title, global:slug, collection:*;
the rest → "skip (not listed)") and refuses a run with no record list
against a production / non-local database unless IPAL_SEED_ALLOW_FULL=1
(guard.allowFull; dry runs allowed). Seed templates carry an ONLY list;
the form seed touches only its own form.
- Tests: seed only/full-run guard, database-name helpers and warning.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9ecab39783
commit
af7d96f0e3
Vendored
+44
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Database naming rule (docs/deployment.md): the site's domain with the dots
|
||||
* removed, plus `_prod` or `_dev` — intecion.com → intecioncom_prod / intecioncom_dev,
|
||||
* architektimprez.pl → architektimprezpl_prod / architektimprezpl_dev.
|
||||
* The same for MongoDB and Postgres; the name is the path of DATABASE_URI.
|
||||
*/
|
||||
|
||||
/** Hosts that are not a site domain (dev / Docker) — no expectation is derived from them. */
|
||||
const NOT_A_DOMAIN = /^(?:localhost|127\.0\.0\.1|\[?::1\]?|0\.0\.0\.0)$|^[^.]+$/
|
||||
|
||||
/** The two database names for a site domain or URL (`https://www.intecion.com/` → intecioncom_*). */
|
||||
export function databaseNamesFor(domainOrUrl: string): { dev: string; prod: string } {
|
||||
const host = domainOrUrl.trim().toLowerCase().replace(/^[a-z][a-z\d+.-]*:\/\//, '').replace(/[/:?#].*$/, '').replace(/^www\./, '')
|
||||
const base = host.replace(/\./g, '')
|
||||
return { dev: `${base}_dev`, prod: `${base}_prod` }
|
||||
}
|
||||
|
||||
/** The database name in a connection string (the URI path), or null (SQLite file, no name). */
|
||||
export function databaseNameFromUri(uri: null | string | undefined): null | string {
|
||||
const value = (uri ?? '').trim()
|
||||
if (!value || /^file:/i.test(value)) {return null}
|
||||
try {
|
||||
const name = decodeURIComponent(new URL(value).pathname.replace(/^\//, '').split('/')[0] ?? '')
|
||||
return name || null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A warning when the database in `databaseUri` breaks the naming rule, else null.
|
||||
* `domain` is the site's production domain; without one (or for localhost) nothing is checked.
|
||||
*/
|
||||
export function checkDatabaseName({ databaseUri, domain }: { databaseUri?: null | string; domain?: null | string }): null | string {
|
||||
if (!domain) {return null}
|
||||
const host = domain.trim().toLowerCase().replace(/^[a-z][a-z\d+.-]*:\/\//, '').replace(/[/:?#].*$/, '')
|
||||
if (NOT_A_DOMAIN.test(host)) {return null}
|
||||
const name = databaseNameFromUri(databaseUri)
|
||||
if (!name) {return null}
|
||||
const { dev, prod } = databaseNamesFor(host)
|
||||
return name === prod || name === dev
|
||||
? null
|
||||
: `database "${name}" does not follow the naming rule for ${host.replace(/^www\./, '')}: expected "${prod}" (production) or "${dev}" (development) — docs/deployment.md`
|
||||
}
|
||||
Vendored
+1
@@ -1,4 +1,5 @@
|
||||
export { assertEnv } from './assertEnv.js'
|
||||
export type { AssertEnvOptions, EnvKind, EnvRule } from './assertEnv.js'
|
||||
export { checkDatabaseName, databaseNameFromUri, databaseNamesFor } from './databaseName.js'
|
||||
export { ipalEnvRules } from './ipalEnvRules.js'
|
||||
export type { IpalEnvFeatures } from './ipalEnvRules.js'
|
||||
|
||||
Vendored
+25
@@ -2,12 +2,21 @@ import type { EnvRule } from './assertEnv.js'
|
||||
|
||||
type TurnstileNames = { secretVar?: string; siteKeyVar?: string }
|
||||
|
||||
import { checkDatabaseName } from './databaseName.js'
|
||||
|
||||
export type IpalEnvFeatures = {
|
||||
/**
|
||||
* Cookie crawl (cookieScanner.crawl): the scanner service URL and the shared
|
||||
* token (at least 16 characters). Required in production only.
|
||||
*/
|
||||
cookieCrawl?: boolean
|
||||
/**
|
||||
* Warn (never fail) when the database name in DATABASE_URI breaks the naming rule
|
||||
* `<domain without dots>_prod` / `_dev` (docs/deployment.md). `true` takes the domain from
|
||||
* NEXT_PUBLIC_SERVER_URL (skipped for localhost); `{ domain }` sets it; `{ warn }` replaces
|
||||
* the console.warn. Default off.
|
||||
*/
|
||||
databaseName?: { domain?: string; warn?: (message: string) => void } | boolean
|
||||
/** Override the database var. Default 'DATABASE_URI'. */
|
||||
databaseVar?: string
|
||||
/** Microsoft Graph e-mail (graphAdapter). Adds the GRAPH_* vars. */
|
||||
@@ -62,6 +71,7 @@ export type IpalEnvFeatures = {
|
||||
export function ipalEnvRules(features: IpalEnvFeatures = {}): EnvRule[] {
|
||||
const {
|
||||
cookieCrawl = false,
|
||||
databaseName = false,
|
||||
databaseVar = 'DATABASE_URI',
|
||||
emailGraph = false,
|
||||
ipHeader = false,
|
||||
@@ -158,5 +168,20 @@ export function ipalEnvRules(features: IpalEnvFeatures = {}): EnvRule[] {
|
||||
)
|
||||
}
|
||||
|
||||
if (databaseName) {
|
||||
const { domain, warn = (message: string) => console.warn(`[ipal] ${message}`) } = databaseName === true ? {} : databaseName
|
||||
rules.push({
|
||||
name: databaseVar,
|
||||
description: 'Database name: <domain without dots>_prod / _dev (warning only)',
|
||||
required: false,
|
||||
// A warning, never an error: the project may have a reason (an old database, a shared server).
|
||||
validate: (value) => {
|
||||
const message = checkDatabaseName({ databaseUri: value, domain: domain ?? process.env[serverUrlVar] })
|
||||
if (message) {warn(`${databaseVar}: ${message}`)}
|
||||
return true
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return rules
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user