-
Notifications
You must be signed in to change notification settings - Fork 1
feat(discovery): support configured db schema #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,14 +7,31 @@ const DEV_FALLBACK = 'postgresql://fides:fides@localhost:5432/fides' | |
|
|
||
| function getConnectionString(): string { | ||
| const url = process.env.DATABASE_URL | ||
| if (url) return url | ||
| const schemaName = process.env.DISCOVERY_DB_SCHEMA | ||
| if (url) return withSearchPath(url, schemaName) | ||
|
|
||
| if (process.env.NODE_ENV === 'production') { | ||
| throw new Error('DATABASE_URL must be set in production') | ||
| } | ||
|
|
||
| console.warn('DATABASE_URL not set — using development fallback') | ||
| return DEV_FALLBACK | ||
| return withSearchPath(DEV_FALLBACK, schemaName) | ||
| } | ||
|
|
||
| function withSearchPath(connectionString: string, schemaName?: string): string { | ||
| if (!schemaName) return connectionString | ||
|
|
||
| assertValidSchemaName(schemaName) | ||
|
|
||
| const url = new URL(connectionString) | ||
| url.searchParams.set('options', `-c search_path=${schemaName},public`) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| return url.toString() | ||
| } | ||
|
|
||
| function assertValidSchemaName(schemaName: string): void { | ||
| if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(schemaName)) { | ||
| throw new Error('DISCOVERY_DB_SCHEMA must be a simple Postgres identifier') | ||
| } | ||
| } | ||
|
|
||
| const poolConfig = { | ||
|
|
@@ -28,6 +45,10 @@ const connectionString = getConnectionString() | |
| export const sql = postgres(connectionString, poolConfig) | ||
| export const db = drizzle(sql, { schema }) | ||
|
|
||
| export function createRawClient(): postgres.Sql { | ||
| return postgres(getConnectionString(), poolConfig) | ||
| } | ||
|
|
||
| export const DISCOVERY_MIGRATIONS = [ | ||
| { | ||
| id: '001_initial', | ||
|
|
@@ -108,6 +129,7 @@ export async function runDiscoveryMigrations(client: postgres.Sql): Promise<void | |
| await client`SELECT pg_advisory_lock(hashtext('discovery_migrations'))` | ||
|
|
||
| try { | ||
| await ensureConfiguredSchema(client) | ||
| await ensureDiscoveryMigrationLedger(client) | ||
| for (const migration of DISCOVERY_MIGRATIONS) { | ||
| const checksum = discoveryMigrationChecksum(migration) | ||
|
|
@@ -143,6 +165,14 @@ export async function runDiscoveryMigrations(client: postgres.Sql): Promise<void | |
| } | ||
| } | ||
|
|
||
| async function ensureConfiguredSchema(client: postgres.Sql): Promise<void> { | ||
| const schemaName = process.env.DISCOVERY_DB_SCHEMA | ||
| if (!schemaName) return | ||
|
|
||
| assertValidSchemaName(schemaName) | ||
| await client.unsafe(`CREATE SCHEMA IF NOT EXISTS "${schemaName}"`) | ||
| } | ||
|
|
||
| async function ensureDiscoveryMigrationLedger(client: postgres.Sql): Promise<void> { | ||
| await client` | ||
| CREATE TABLE IF NOT EXISTS discovery_schema_migrations ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For existing Docker Compose deployments with a persisted Postgres volume and no
DISCOVERY_DB_SCHEMAin.env, this new default silently moves discovery from the previously usedpublicschema to a freshdiscoveryschema. On upgrade,runDiscoveryMigrationswill create empty tables there, so registered identities/agents appear to be lost until operators discover and override the variable or manually migrate data. Make the schema opt-in for Compose upgrades or provide an explicit migration/compatibility path.Useful? React with 👍 / 👎.