feat(infra): PostgreSQL in the compose stacks, automatic pre-deploy backups (MITHOME-136)

- staging/prod: postgres:16-alpine per the llmdev Docker spec (healthcheck,
  log and memory limits, no host port); the app builds DATABASE_URI from
  POSTGRES_PASSWORD and waits for a healthy database; MongoDB kept for one
  release (rollback path, transfer source)
- dev: PostgreSQL replaces MongoDB and Mongo Express (host port 5433, checked
  and registered); integration test rewritten with pg
- deploy.sh: pg_dump (and, during the switch, mongodump) before the build
- pg/@types/pg pinned to the adapter's 8.20.0 (one copy, no type clash)
- docs: CLAUDE.md, architecture/testing steering, deploy workflow, DOCKER.md;
  staging switch runbook in the design

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Do Siki
2026-09-29 22:35:56 +02:00
co-authored by Claude Opus 5.5
parent a68fb94b61
commit 0ec5714676
17 changed files with 263 additions and 183 deletions
+2
View File
@@ -30,6 +30,7 @@
"@testing-library/user-event": "^14.5",
"@types/jest": "^29.5",
"@types/node": "^20",
"@types/pg": "8.20.0",
"@types/react": "^19",
"@types/react-dom": "^19",
"@types/winston": "^2.4",
@@ -37,6 +38,7 @@
"eslint-config-next": "^16.3.4",
"jest": "^29.7",
"jest-environment-jsdom": "^29.7",
"pg": "8.20.0",
"tailwindcss": "^4",
"tsx": "^4.23.13",
"typescript": "^5",
+2
View File
@@ -61,6 +61,7 @@
"@testing-library/user-event": "^14.5",
"@types/jest": "^29.5",
"@types/node": "^20",
"@types/pg": "8.20.0",
"@types/react": "^19",
"@types/react-dom": "^19",
"@types/winston": "^2.4",
@@ -68,6 +69,7 @@
"eslint-config-next": "^16.3.4",
"jest": "^29.7",
"jest-environment-jsdom": "^29.7",
"pg": "8.20.0",
"tailwindcss": "^4",
"tsx": "^4.23.13",
"typescript": "^5",
+36 -67
View File
@@ -9,21 +9,31 @@
* Mongoose collections. Assumes the Docker dev DB already has content
* migrated (`npm run migrate:content`) — see docs/backups for a snapshot if
* you need to reseed a throwaway environment.
*
* MITHOME-136: the stack runs PostgreSQL (dev host port 5433, registered);
* MITHOME-137: grafana moved to 3012 (3001 is taken on llmdev).
*/
import { MongoClient, ObjectId } from 'mongodb'
import { Client } from 'pg'
const DOCKER_SERVICES = {
app: 'http://localhost:8080',
mongoExpress: 'http://localhost:8081',
grafana: 'http://localhost:3001',
grafana: 'http://localhost:3012',
loki: 'http://localhost:3100',
mongodb: 'mongodb://admin:password123@localhost:27018/mozdit?authSource=admin'
postgres: 'postgresql://mozdit_web:devpassword@localhost:5433/mozdit_web',
}
async function query<T extends Record<string, unknown>>(sql: string, params: unknown[] = []): Promise<T[]> {
const client = new Client({ connectionString: DOCKER_SERVICES.postgres })
await client.connect()
try {
return (await client.query<T>(sql, params)).rows
} finally {
await client.end()
}
}
describe('Docker Environment Integration Tests', () => {
let mongoClient: MongoClient | null = null
beforeAll(async () => {
// Skip integration tests if not in Docker environment
if (process.env.NODE_ENV === 'test' && !process.env.INTEGRATION_TESTS) {
@@ -32,12 +42,6 @@ describe('Docker Environment Integration Tests', () => {
}
})
afterAll(async () => {
if (mongoClient) {
await mongoClient.close()
}
})
describe('Service Health Checks', () => {
it('should connect to Next.js app', async () => {
if (process.env.NODE_ENV === 'test' && !process.env.INTEGRATION_TESTS) {
@@ -55,40 +59,28 @@ describe('Docker Environment Integration Tests', () => {
expect(data).toHaveProperty('environment')
})
it('should connect to MongoDB directly', async () => {
it('should connect to PostgreSQL directly', async () => {
if (process.env.NODE_ENV === 'test' && !process.env.INTEGRATION_TESTS) {
return
}
mongoClient = new MongoClient(DOCKER_SERVICES.mongodb)
await mongoClient.connect()
const adminDb = mongoClient.db('admin')
const result = await adminDb.admin().ping()
expect(result).toEqual({ ok: 1 })
const [row] = await query<{ ok: number }>('select 1 as ok')
expect(row.ok).toBe(1)
})
it('should verify MongoDB initialization', async () => {
it('should verify the schema migrations ran', async () => {
if (process.env.NODE_ENV === 'test' && !process.env.INTEGRATION_TESTS) {
return
}
if (!mongoClient) {
mongoClient = new MongoClient(DOCKER_SERVICES.mongodb)
await mongoClient.connect()
const tables = (await query<{ table_name: string }>(
"select table_name from information_schema.tables where table_schema = 'public'",
)).map((t) => t.table_name)
for (const table of ['home', 'legal_pages', 'partners', 'contact_submissions', 'users', 'products', 'payload_migrations']) {
expect(tables).toContain(table)
}
const mozditDb = mongoClient.db('mozdit')
const collections = await mozditDb.listCollections().toArray()
const collectionNames = collections.map(c => c.name)
// Payload Globals (Home/About/Services/Contact/Common) live in a
// single `globals` collection, one document per global.
expect(collectionNames).toContain('globals')
expect(collectionNames).toContain('legal-pages')
expect(collectionNames).toContain('partners')
expect(collectionNames).toContain('contact-submissions')
expect(collectionNames).toContain('users')
const migrations = await query<{ name: string }>('select name from payload_migrations')
expect(migrations.map((m) => m.name)).toEqual(expect.arrayContaining([expect.stringMatching(/_initial$/)]))
})
it('should verify the Home global exists and is published', async () => {
@@ -96,30 +88,11 @@ describe('Docker Environment Integration Tests', () => {
return
}
if (!mongoClient) {
mongoClient = new MongoClient(DOCKER_SERVICES.mongodb)
await mongoClient.connect()
}
const mozditDb = mongoClient.db('mozdit')
const home = await mozditDb.collection('globals').findOne({ globalType: 'home' })
if (!home) throw new Error('Home global document not found')
expect(home).toHaveProperty('_status', 'published')
expect(home).toHaveProperty('hero')
expect(home.hero).toHaveProperty('title')
})
it('should access Mongo Express UI', async () => {
if (process.env.NODE_ENV === 'test' && !process.env.INTEGRATION_TESTS) {
return
}
const response = await fetch(DOCKER_SERVICES.mongoExpress)
expect(response.status).toBe(200)
const html = await response.text()
expect(html).toContain('Mongo Express')
const [home] = await query<{ _status: string }>('select _status from home limit 1')
if (!home) throw new Error('Home global row not found')
expect(home._status).toBe('published')
const titles = await query<{ hero_title: string }>("select hero_title from home_locales where _locale = 'hu'")
expect(titles[0]?.hero_title).toBeTruthy()
})
it('should access Grafana UI', async () => {
@@ -182,14 +155,10 @@ describe('Docker Environment Integration Tests', () => {
expect(result).toHaveProperty('timestamp')
expect(result).toHaveProperty('submissionId')
if (!mongoClient) {
mongoClient = new MongoClient(DOCKER_SERVICES.mongodb)
await mongoClient.connect()
}
const savedSubmission = await mongoClient
.db('mozdit')
.collection('contact-submissions')
.findOne({ _id: new ObjectId(result.submissionId) })
const [savedSubmission] = await query(
'select name, email, subject, message, gdpr_consent as "gdprConsent", status from contact_submissions where id = $1',
[result.submissionId],
)
expect(savedSubmission).toEqual(expect.objectContaining({
name: contactData.name,
email: contactData.email,