Add PostgreSQL persistence layer#16
Merged
Merged
Conversation
- SqlDeploymentRepository for persistent storage - Database schema with tables for deployments, versions, state, runs, audit - Migration runner (src/database/migrate.ts) - Server auto-detects DATABASE_URL and uses PostgreSQL or in-memory - Fix import statements to use explicit .ts extensions - npm install postgres dependency
There was a problem hiding this comment.
Pull request overview
Adds an optional PostgreSQL-backed persistence layer for deployments, intended to be auto-selected via DATABASE_URL with an in-memory fallback for local/dev usage.
Changes:
- Introduces a new
src/database/module (schema, migration runner, SQL repository, docs). - Updates the admin UI server to choose PostgreSQL vs in-memory repository based on
DATABASE_URLand run migrations on startup. - Adds an npm package setup (with
postgresdependency) and updates several ESM imports to use explicit.tsextensions.
Reviewed changes
Copilot reviewed 11 out of 12 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| package.json | Adds npm scripts and the postgres dependency for running/tests/migrations. |
| .gitignore | Ignores Node build artifacts and environment files. |
| src/database/schema.sql | Defines initial PostgreSQL schema + indexes for persistence. |
| src/database/sql-repository.ts | Implements DeploymentRepository using PostgreSQL queries. |
| src/database/migrate.ts | Adds a migration runner that initializes the schema. |
| src/database/index.ts | Exposes the database module’s public API. |
| src/database/README.md | Documents local Postgres + fly.io setup. |
| src/admin-ui/server.ts | Auto-detects DATABASE_URL and selects repository implementation. |
| src/admin-ui/README.md | Updates server startup docs for in-memory vs Postgres mode. |
| src/sync/index.ts | Updates ESM imports/exports to include explicit .ts extensions. |
| src/infrastructure/index.test.ts | Updates ESM import to include explicit .ts extension. |
| src/sync/index.test.ts | Updates ESM import to include explicit .ts extension. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+24
to
+28
| CREATE TABLE IF NOT EXISTS desired_state_snapshots ( | ||
| id UUID PRIMARY KEY, | ||
| version_id UUID NOT NULL, | ||
| keycloak_config JSONB NOT NULL, | ||
| auth_providers JSONB NOT NULL, |
Comment on lines
+119
to
+127
| async recordDesiredState(snapshot: DesiredStateSnapshot): Promise<void> { | ||
| await this.sql` | ||
| INSERT INTO desired_state_snapshots | ||
| (id, version_id, keycloak_config, auth_providers, created_at) | ||
| VALUES | ||
| (${snapshot.id}, ${snapshot.versionId}, | ||
| ${JSON.stringify(snapshot.keycloakConfig)}, | ||
| ${JSON.stringify(snapshot.authProviders)}, | ||
| ${snapshot.createdAt}) |
Comment on lines
+94
to
+105
| async saveDeployment(deployment: Deployment): Promise<void> { | ||
| await this.sql` | ||
| INSERT INTO deployments | ||
| (id, customer_id, name, target, status, created_at, updated_at) | ||
| VALUES | ||
| (${deployment.id}, ${deployment.customerId}, ${deployment.name}, | ||
| ${JSON.stringify(deployment.target)}, ${deployment.status}, | ||
| ${deployment.createdAt}, ${deployment.updatedAt}) | ||
| ON CONFLICT (id) DO UPDATE SET | ||
| status = EXCLUDED.status, | ||
| updated_at = EXCLUDED.updated_at | ||
| `; |
Comment on lines
+132
to
+140
| async recordReconciliationRun(run: ReconciliationRun): Promise<void> { | ||
| await this.sql` | ||
| INSERT INTO reconciliation_runs | ||
| (id, deployment_id, started_at, completed_at, status, drift_findings, error) | ||
| VALUES | ||
| (${run.id}, ${run.deploymentId}, ${run.startedAt}, | ||
| ${run.completedAt ?? null}, ${run.status}, | ||
| ${run.driftFindings ? JSON.stringify(run.driftFindings) : null}, | ||
| ${run.error ?? null}) |
Comment on lines
+145
to
+152
| async appendAuditEvent(event: AuditEvent): Promise<void> { | ||
| await this.sql` | ||
| INSERT INTO audit_events | ||
| (id, deployment_id, event_type, data, created_at) | ||
| VALUES | ||
| (${event.id}, ${event.deploymentId}, ${event.eventType}, | ||
| ${event.data ? JSON.stringify(event.data) : null}, ${event.createdAt}) | ||
| `; |
Comment on lines
+35
to
+41
| if (import.meta.url === `file://${process.argv[1]}`) { | ||
| const connectionString = process.env.DATABASE_URL || 'postgresql://localhost/kloak'; | ||
| runMigrations(connectionString).catch((error) => { | ||
| console.error('Migration failed:', error); | ||
| process.exit(1); | ||
| }); | ||
| } |
Comment on lines
+108
to
+116
| async saveDeploymentVersion(version: DeploymentVersion): Promise<void> { | ||
| await this.sql` | ||
| INSERT INTO deployment_versions | ||
| (id, deployment_id, number, created_by, notes, created_at) | ||
| VALUES | ||
| (${version.id}, ${version.deploymentId}, ${version.number}, | ||
| ${version.createdBy}, ${version.notes ?? null}, ${version.createdAt}) | ||
| ON CONFLICT (deployment_id, number) DO NOTHING | ||
| `; |
Comment on lines
+9
to
+13
| "scripts": { | ||
| "test": "node --experimental-transform-types --test src/**/*.test.ts", | ||
| "dev": "PORT=3000 node --experimental-transform-types src/admin-ui/cli.ts", | ||
| "migrate": "node --experimental-transform-types src/database/migrate.ts" | ||
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes: #15
Summary
Implements persistent storage using PostgreSQL for Kloak deployments. The server auto-detects DATABASE_URL and uses PostgreSQL when available, falling back to in-memory storage for local development.
Architecture
Changes Made
Testing
All existing tests pass, and the database module compiles without errors.
Deployment
Local Development (in-memory)
npm run dev # Server uses in-memory repositoryLocal Development (PostgreSQL)
docker run --name kloak-postgres -e POSTGRES_DB=kloak -e POSTGRES_PASSWORD=dev -p 5432:5432 -d postgres:16 DATABASE_URL=postgresql://postgres:dev@localhost/kloak npm run dev # Server runs migrations and uses PostgreSQL repositoryfly.io Deployment
fly postgres create --name kloak-db fly postgres attach kloak-db --app kloak # DATABASE_URL injected automatically; migrations run on app startFiles Modified