This repository was archived by the owner on Apr 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Add Cloud SQL connector DB mode switch with DB schema search_path + CI-safe DB init for auth-provider #46
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8bfc69d
feat(auth-provider): add DB connection mode switch with Cloud SQL con…
Copilot 053fa2e
chore(auth-provider): wire connector env vars in app hosting and secr…
Copilot d790be9
fix(auth-provider): harden DB init failure handling and connector val…
Copilot 0b18c9a
fix(auth-provider): add DB_SCHEMA search_path support and lazy DB poo…
Copilot bd3ae10
chore(auth-provider): clean up DB_SCHEMA variable naming
Copilot 8334ae8
fix(auth-provider): avoid duplicate search_path override in direct DB…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,150 @@ | ||
| import { drizzle } from 'drizzle-orm/node-postgres'; | ||
| import { Pool } from 'pg'; | ||
| import { Connector, IpAddressTypes } from '@google-cloud/cloud-sql-connector'; | ||
| import * as schema from './schema'; | ||
|
|
||
| const pool = new Pool({ | ||
| connectionString: process.env.DATABASE_URL, | ||
| let connector: Connector | null = null; | ||
| let poolPromise: Promise<Pool> | null = null; | ||
|
|
||
| function getSearchPath(): string { | ||
| const raw = process.env.DB_SCHEMA ?? 'public'; | ||
| const schemaNames = raw | ||
| .split(',') | ||
| .map(schemaName => schemaName.trim()) | ||
| .filter(Boolean); | ||
|
|
||
| if (schemaNames.length === 0) { | ||
| throw new Error('DB_SCHEMA must include at least one schema name.'); | ||
| } | ||
|
|
||
| const validSchemaName = /^[A-Za-z_][A-Za-z0-9_]*$/; | ||
| for (const schemaName of schemaNames) { | ||
| if (!validSchemaName.test(schemaName)) { | ||
| throw new Error( | ||
| 'DB_SCHEMA contains invalid schema names. Use comma-separated schema names with letters, numbers, and underscores only.' | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return schemaNames.map(schemaName => `"${schemaName}"`).join(','); | ||
| } | ||
|
|
||
| function hasSearchPathInConnectionString(connectionString: string): boolean { | ||
| try { | ||
| const parsed = new URL(connectionString); | ||
| if (parsed.searchParams.has('search_path') || parsed.searchParams.has('currentSchema')) { | ||
| return true; | ||
| } | ||
|
|
||
| const options = parsed.searchParams.get('options'); | ||
| return options ? /\bsearch_path\s*=/.test(decodeURIComponent(options)) : false; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| async function createCloudSqlPool(): Promise<Pool> { | ||
| const instanceConnectionName = process.env.CLOUD_SQL_CONNECTION_NAME; | ||
| const dbUser = process.env.DB_USER; | ||
| const dbPassword = process.env.DB_PASSWORD; | ||
| const dbName = process.env.DB_NAME; | ||
|
|
||
| if (!instanceConnectionName || !dbUser || !dbPassword || !dbName) { | ||
| throw new Error( | ||
| 'Cloud SQL Connector requires CLOUD_SQL_CONNECTION_NAME, DB_USER, DB_PASSWORD, and DB_NAME.' | ||
| ); | ||
| } | ||
|
|
||
| const validTypes = Object.values(IpAddressTypes) as string[]; | ||
| const ipAddressType = validTypes.includes(process.env.CLOUD_SQL_IP_TYPE ?? '') | ||
| ? (process.env.CLOUD_SQL_IP_TYPE as IpAddressTypes) | ||
| : IpAddressTypes.PUBLIC; | ||
|
|
||
| connector = new Connector(); | ||
| const clientOpts = await connector.getOptions({ | ||
| instanceConnectionName, | ||
| ipType: ipAddressType, | ||
| }); | ||
| const searchPath = getSearchPath(); | ||
|
|
||
| return new Pool({ | ||
| ...clientOpts, | ||
| user: dbUser, | ||
| password: dbPassword, | ||
| database: dbName, | ||
| options: `-c search_path=${searchPath}`, | ||
| }); | ||
| } | ||
|
|
||
| function createDirectPool(): Pool { | ||
| const connectionString = process.env.DATABASE_URL; | ||
|
|
||
| if (!connectionString) { | ||
| throw new Error('DATABASE_URL is missing. Cannot connect to the database.'); | ||
| } | ||
|
|
||
| const searchPath = getSearchPath(); | ||
| if (hasSearchPathInConnectionString(connectionString)) { | ||
| return new Pool({ connectionString }); | ||
| } | ||
|
|
||
| return new Pool({ connectionString, options: `-c search_path=${searchPath}` }); | ||
| } | ||
|
|
||
|
Comment on lines
+79
to
+93
Contributor
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. @copilot DATABASE_URL already has a search path in it... will this break by adding it again in the options argument of the Pool constructor?
Contributor
Author
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. Good catch — I updated this in |
||
| async function createPool(): Promise<Pool> { | ||
| const mode = process.env.DB_CONNECTION_MODE ?? 'direct'; | ||
| return mode === 'connector' ? createCloudSqlPool() : createDirectPool(); | ||
| } | ||
|
|
||
| async function getPool(): Promise<Pool> { | ||
| if (!poolPromise) { | ||
| poolPromise = createPool(); | ||
| poolPromise | ||
| .then(pool => { | ||
| pool.on('error', err => { | ||
| console.error('Unexpected error on idle PostgreSQL client:', err); | ||
| }); | ||
| }) | ||
| .catch(err => { | ||
| console.error('Failed to initialize PostgreSQL pool:', err); | ||
| }); | ||
| } | ||
|
|
||
| return poolPromise; | ||
| } | ||
|
|
||
| const poolProxy = { | ||
| async query(...args: Parameters<Pool['query']>) { | ||
| const pool = await getPool(); | ||
| return pool.query(...args); | ||
| }, | ||
| async connect(...args: Parameters<Pool['connect']>) { | ||
| const pool = await getPool(); | ||
| return pool.connect(...args); | ||
| }, | ||
| async end(...args: Parameters<Pool['end']>) { | ||
| const pool = await getPool(); | ||
| return pool.end(...args); | ||
| }, | ||
| } as unknown as Pool; | ||
|
|
||
| async function closeConnector() { | ||
| if (connector) { | ||
| await connector.close(); | ||
| connector = null; | ||
| } | ||
| } | ||
|
|
||
| process.once('SIGTERM', () => { | ||
| void closeConnector(); | ||
| }); | ||
|
|
||
| process.once('SIGINT', () => { | ||
| void closeConnector(); | ||
| }); | ||
|
|
||
| // Use drizzle to wrap the PG pool with schema types | ||
| export const db = drizzle(pool, { schema }); | ||
| export const db = drizzle(poolProxy, { schema }); | ||
|
|
||
| // Export the database type for use in adapters | ||
| export type DB = typeof db; | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.