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:
Radosław Smoliński
2026-10-29 13:27:46 +01:00
co-authored by Claude Opus 5.5
parent 9ecab39783
commit af7d96f0e3
19 changed files with 331 additions and 46 deletions
+38
View File
@@ -1,6 +1,7 @@
import { afterEach, describe, expect, test, vi } from 'vitest'
import { assertEnv } from '../src/modules/env/assertEnv.js'
import { checkDatabaseName, databaseNameFromUri, databaseNamesFor } from '../src/modules/env/databaseName.js'
import { ipalEnvRules } from '../src/modules/env/ipalEnvRules.js'
const base = { DATABASE_URI: 'mongodb://db/x', NEXT_PUBLIC_SERVER_URL: 'http://localhost:3000', PAYLOAD_SECRET: 's' }
@@ -52,3 +53,40 @@ describe('ipalEnvRules({ storage })', () => {
expect(check({ ...base, NODE_ENV: 'development', R2_BUCKET: 'b' })).toThrow(/R2_ACCESS_KEY_ID is required/)
})
})
describe('database naming rule', () => {
test('names come from the domain without dots', () => {
expect(databaseNamesFor('intecion.com')).toEqual({ dev: 'intecioncom_dev', prod: 'intecioncom_prod' })
expect(databaseNamesFor('https://www.architektimprez.pl/')).toEqual({ dev: 'architektimprezpl_dev', prod: 'architektimprezpl_prod' })
})
test('the name is read from Mongo and Postgres URIs', () => {
expect(databaseNameFromUri('mongodb://u:p@mongo:27017/intecioncom_prod?authSource=admin')).toBe('intecioncom_prod')
expect(databaseNameFromUri('mongodb+srv://u:[email protected]/intecioncom_dev')).toBe('intecioncom_dev')
expect(databaseNameFromUri('postgres://u:p@db:5432/intecioncom_prod')).toBe('intecioncom_prod')
expect(databaseNameFromUri('file:./dev.db')).toBeNull()
expect(databaseNameFromUri('postgres://u:p@db:5432')).toBeNull()
})
test('checkDatabaseName warns on a mismatch and skips local or unknown domains', () => {
expect(checkDatabaseName({ databaseUri: 'postgres://db/intecioncom_prod', domain: 'https://intecion.com' })).toBeNull()
expect(checkDatabaseName({ databaseUri: 'postgres://db/payload', domain: 'intecion.com' })).toMatch(/expected "intecioncom_prod"/)
expect(checkDatabaseName({ databaseUri: 'postgres://db/payload', domain: 'http://localhost:3000' })).toBeNull()
expect(checkDatabaseName({ databaseUri: 'postgres://db/payload' })).toBeNull()
})
test('ipalEnvRules({ databaseName }) only warns — assertEnv still passes', () => {
const warn = vi.fn()
const env = { DATABASE_URI: 'mongodb://mongo/payload', NEXT_PUBLIC_SERVER_URL: 'https://intecion.com', PAYLOAD_SECRET: 's' }
expect(() => assertEnv(ipalEnvRules({ databaseName: { domain: 'intecion.com', warn } }), { env })).not.toThrow()
expect(warn).toHaveBeenCalledWith(expect.stringMatching(/DATABASE_URI: database "payload"[\s\S]*intecioncom_prod/))
warn.mockClear()
assertEnv(ipalEnvRules({ databaseName: { domain: 'www.intecion.com', warn } }), { env: { ...env, DATABASE_URI: 'mongodb://mongo/intecioncom_dev' } })
expect(warn).not.toHaveBeenCalled()
// true: the domain comes from NEXT_PUBLIC_SERVER_URL in process.env
vi.stubEnv('NEXT_PUBLIC_SERVER_URL', 'https://architektimprez.pl')
assertEnv(ipalEnvRules({ databaseName: { warn } }), { env: { ...env, DATABASE_URI: 'postgres://db/architektimprez_prod' } })
expect(warn).toHaveBeenCalledWith(expect.stringMatching(/expected "architektimprezpl_prod"/))
vi.unstubAllEnvs()
})
})
+43 -7
View File
@@ -165,7 +165,7 @@ describe('createSeeder', () => {
const again = seeder()
expect((await again.upsertBySlug('pages', PAGES)).map((r) => r.status)).toEqual(['skip (exists)', 'skip (exists)'])
expect(again.summary()).toEqual({ created: 0, errors: 0, skipped: 2, skippedEdited: 0, updated: 0 })
expect(again.summary()).toEqual({ created: 0, errors: 0, notListed: 0, skipped: 2, skippedEdited: 0, updated: 0 })
expect(lines.join('\n')).toMatch(/skip \(exists\)\s+pages "start"/)
})
@@ -243,19 +243,55 @@ describe('createSeeder', () => {
test('production guard: update refused on production or a non-local database unless IPAL_SEED_ALLOW_PROD_UPDATE=1', () => {
vi.stubEnv('NODE_ENV', 'production')
expect(() => seeder({ update: true })).toThrow(/update refused: NODE_ENV=production[\s\S]*Back up the database/)
expect(() => seeder()).not.toThrow() // create-only is always allowed
const only = ['pages:kontakt']
expect(() => seeder({ only, update: true })).toThrow(/update refused: NODE_ENV=production[\s\S]*Back up the database/)
expect(() => seeder({ only })).not.toThrow() // create-only of listed records is always allowed
vi.stubEnv('IPAL_SEED_ALLOW_PROD_UPDATE', '1')
expect(seeder({ update: true }).update).toBe(true)
expect(seeder({ only, update: true }).update).toBe(true)
expect(lines.join('\n')).toMatch(/Did you back it up/)
vi.unstubAllEnvs()
vi.stubEnv('DATABASE_URI', 'postgres://u:p@db:5432/app')
expect(() => seeder({ update: true })).toThrow(/the database is not local/)
expect(() => seeder({ only, update: true })).toThrow(/update refused: the database is not local/)
expect(() => seeder({ guard: { localHosts: ['db'] }, update: true })).not.toThrow()
expect(() => seeder({ guard: false, update: true })).not.toThrow()
vi.stubEnv('IPAL_SEED_ALLOW_PROD_UPDATE', '1')
expect(() => seeder({ force: true, update: true })).toThrow(/force refused/)
expect(() => seeder({ force: true, only, update: true })).toThrow(/force refused/)
})
test('after launch: a full seed of a live database is refused without a record list', () => {
vi.stubEnv('NODE_ENV', 'production')
expect(() => seeder()).toThrow(/full seed refused: NODE_ENV=production[\s\S]*SEED_ONLY/)
expect(() => seeder({ dryRun: true })).not.toThrow() // a dry run only prints the plan
expect(() => seeder({ only: ['global:header'] })).not.toThrow()
vi.stubEnv('SEED_ONLY', 'pages:kontakt, global:header')
expect(seeder().only).toEqual(['pages:kontakt', 'global:header'])
vi.unstubAllEnvs()
vi.stubEnv('DATABASE_URI', 'postgres://u:p@db:5432/app')
expect(() => seeder()).toThrow(/full seed refused: the database is not local/)
expect(() => seeder({ guard: { allowFull: true } })).not.toThrow()
vi.stubEnv('IPAL_SEED_ALLOW_FULL', '1')
expect(() => seeder()).not.toThrow()
expect(() => seeder({ guard: false })).not.toThrow()
})
test('only: records off the list are reported and never touched', async () => {
const seed = seeder({ only: ['posts:listed', 'global:header'] })
const [listed, other] = await seed.upsertBySlug('posts', [
{ slug: 'listed', title: 'Listed' },
{ slug: 'not-listed', title: 'Not listed' },
])
expect(listed.status).toBe('create')
expect(other.status).toBe('skip (not listed)')
expect((await seed.upsertBy('posts', { slug: 'other' }, { title: 'X' })).status).toBe('skip (not listed)')
expect((await seed.fillGlobal('header', { phone: '+48 2' })).status).not.toBe('skip (not listed)')
const { docs } = await payload.find({ collection: 'posts', where: { slug: { in: ['not-listed', 'other'] } } })
expect(docs).toHaveLength(0)
expect(seed.summary().notListed).toBe(2)
const whole = seeder({ only: ['posts:*'] })
expect((await whole.upsertBySlug('posts', { slug: 'any-post', title: 'Any' })).status).toBe('create')
expect((await whole.fillGlobal('header', { tagline: 'x' })).status).toBe('skip (not listed)')
})
test('env defaults: SEED_DRY_RUN, SEED_UPDATE', () => {
@@ -273,7 +309,7 @@ describe('createSeeder', () => {
vi.stubEnv('NEXT_PUBLIC_SERVER_URL', 'https://acme.pl')
vi.stubEnv('INDEXNOW_KEY', 'k123')
const seed = seeder({ update: true })
const seed = seeder({ only: ['pages:*', 'global:header'], update: true })
await seed.upsertBySlug('pages', [
{ slug: { en: 'news', pl: 'aktualnosci' }, _status: 'published', title: { en: 'News', pl: 'Aktualności' } },
{ slug: { pl: 'oferta' }, _status: 'published', title: { pl: 'Oferta po aktualizacji' } },