diff --git a/core/src/sessions/db/operations.ts b/core/src/sessions/db/operations.ts index 71dce049..ef589729 100644 --- a/core/src/sessions/db/operations.ts +++ b/core/src/sessions/db/operations.ts @@ -43,28 +43,20 @@ export async function getConnectionOptionsFromUri( throw new Error(`Unsupported database URI: ${uri}`); } - if (uri === 'sqlite://:memory:') { + if (uri.startsWith('sqlite://')) { return { entities: ENTITIES, - dbName: ':memory:', + dbName: + uri === 'sqlite://:memory:' + ? ':memory:' + : uri.substring('sqlite://'.length), driver, } as MikroORMOptions; } - const {host, port, username, password, pathname} = new URL(uri); - const hostName = host.split(':')[0]; - - const dbName = uri.startsWith('sqlite://') - ? uri.substring('sqlite://'.length) - : pathname.slice(1); - return { entities: ENTITIES, - dbName, - host: hostName, - port: port ? parseInt(port) : undefined, - user: username, - password, + clientUrl: uri, driver, } as MikroORMOptions; } diff --git a/core/test/sessions/db/operations_test.ts b/core/test/sessions/db/operations_test.ts index 58f16d2d..ff84c6b7 100644 --- a/core/test/sessions/db/operations_test.ts +++ b/core/test/sessions/db/operations_test.ts @@ -38,20 +38,35 @@ describe('operations', () => { const options = await getConnectionOptionsFromUri( 'postgres://user:pass@localhost:5432/db', ); - expect(options.dbName).toBe('db'); - expect(options.host).toBe('localhost'); - expect(options.port).toBe(5432); - expect(options.user).toBe('user'); - expect(options.password).toBe('pass'); expect(options.driver).toBeDefined(); + expect(options.clientUrl).toBe('postgres://user:pass@localhost:5432/db'); + }); + + it('should parse postgresql URI with query params and preserve them in clientUrl', async () => { + const uri = 'postgres://user:pass@localhost:5432/db?sslmode=require'; + const options = await getConnectionOptionsFromUri(uri); + expect(options.clientUrl).toBe(uri); + }); + + it('should parse postgresql Unix-socket URI with percent-encoded host', async () => { + const uri = + 'postgresql://user:pass@%2Fcloudsql%2Fmy-project%3Aus-central1%3Amy-instance/mydb'; + const options = await getConnectionOptionsFromUri(uri); + expect(options.clientUrl).toBe(uri); + }); + + it('should parse postgresql Unix-socket URI with query param host', async () => { + const uri = + 'postgresql://user:pass@/mydb?host=/cloudsql/my-project:us-central1:my-instance'; + const options = await getConnectionOptionsFromUri(uri); + expect(options.clientUrl).toBe(uri); }); it('should parse mysql URI', async () => { - const options = await getConnectionOptionsFromUri( - 'mysql://user:pass@localhost:3306/db', - ); + const uri = 'mysql://user:pass@localhost:3306/db'; + const options = await getConnectionOptionsFromUri(uri); expect(options.driver).toBeDefined(); - expect(options.dbName).toBe('db'); + expect(options.clientUrl).toBe(uri); }); it('should parse mariadb URI', async () => { diff --git a/dev/src/cli/cli.ts b/dev/src/cli/cli.ts index 75b2baeb..ac5ec29f 100644 --- a/dev/src/cli/cli.ts +++ b/dev/src/cli/cli.ts @@ -119,10 +119,12 @@ const LOG_LEVEL_OPTION = new Option( 'Optional. The log level of the server', ).default('info'); const SESSION_SERVICE_URI_OPTION = new Option( - '--session_service_uri , Optional. The URI of the session service. Supported URIs: memory:// for in-memory session service.', + '--session_service_uri ', + 'Optional. The URI of the session service. Supported URIs: memory:// for in-memory session service.', ); const ARTIFACT_SERVICE_URI_OPTION = new Option( - '--artifact_service_uri , Optional. The URI of the artifact service. Supported URIs: gs:// for GCS artifact service.', + '--artifact_service_uri ', + 'Optional. The URI of the artifact service. Supported URIs: gs:// for GCS artifact service.', ); const OTEL_TO_CLOUD_OPTION = new Option( '--otel_to_cloud [boolean]',