This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: repair crash on sqlite/mysql/sqlserver connection strings
the previous version was hardcoded to only support postgresql strings.
- Loading branch information
Showing
9 changed files
with
188 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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
9 changes: 9 additions & 0 deletions
9
packages/generator/src/__tests__/__snapshots__/getConnectionString.ts.snap
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getConnectionString given a mysql database connector should transform the connection string 1`] = `"Host=host;Database=initial_db;Username=user;Password=password"`; | ||
|
||
exports[`getConnectionString given a postgresql database connector should transform the connection string 1`] = `"Host=host;Database=initial_db;Username=user;Password=password"`; | ||
|
||
exports[`getConnectionString given a sqlite3 database connector should transform the connection string 1`] = `"Data Source=./test.db;"`; | ||
|
||
exports[`getConnectionString given a sqlserver database connector should transform the connection string 1`] = `"Host=host:5432;Database=initial_db;Username=user;Password=password;Encrypt=true"`; |
This file contains 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 |
---|---|---|
|
@@ -13,4 +13,5 @@ describe('generateDbContext', () => { | |
}; | ||
expect(generateDbContext(args)).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { getConnectionString } from '../helpers/getConnectionString'; | ||
|
||
describe('getConnectionString', () => { | ||
describe('given a sqlite3 database connector', () => { | ||
it('should transform the connection string', () => { | ||
const connection_string = getConnectionString({ | ||
activeProvider: 'sqlite', | ||
config: {}, | ||
name: '', | ||
provider: 'sqlite', | ||
url: { | ||
fromEnvVar: null, | ||
value: 'file:./test.db' | ||
} | ||
}); | ||
expect(connection_string).toMatchSnapshot(); | ||
}); | ||
}); | ||
describe('given a postgresql database connector', () => { | ||
it('should transform the connection string', () => { | ||
const connection_string = getConnectionString({ | ||
activeProvider: 'postgresql', | ||
config: {}, | ||
name: '', | ||
provider: 'postgresql', | ||
url: { | ||
fromEnvVar: null, | ||
value: 'postgresql://user:password@host:5432/initial_db' | ||
} | ||
}); | ||
expect(connection_string).toMatchSnapshot(); | ||
}); | ||
}); | ||
describe('given a mysql database connector', () => { | ||
it('should transform the connection string', () => { | ||
const connection_string = getConnectionString({ | ||
activeProvider: 'mysql', | ||
config: {}, | ||
name: '', | ||
provider: 'mysql', | ||
url: { | ||
fromEnvVar: null, | ||
value: 'mysql://user:password@host:5432/initial_db' | ||
} | ||
}); | ||
expect(connection_string).toMatchSnapshot(); | ||
}); | ||
}); | ||
describe('given a sqlserver database connector', () => { | ||
it('should transform the connection string', () => { | ||
const connection_string = getConnectionString({ | ||
activeProvider: 'sqlserver', | ||
config: {}, | ||
name: '', | ||
provider: 'sqlserver', | ||
url: { | ||
fromEnvVar: null, | ||
value: 'sqlserver://host:5432;Database=initial_db;user=user;PASSWORD=password;encrypt=true' | ||
} | ||
}); | ||
expect(connection_string).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { DataSource } from '@prisma/generator-helper'; | ||
import { logger } from '@prisma/sdk'; | ||
import { join } from 'path'; | ||
|
||
export function getConnectionString(datasource: DataSource): string { | ||
if (!datasource) { | ||
throw new Error('No datasource specified. I need a datasource in order to construct the DbContext. The DbContext will need to setup the connection in the `OnConfiguring` life cycle hook.'); | ||
} | ||
switch (datasource.provider) { | ||
case 'mysql': | ||
case 'postgres' as any: | ||
case 'postgresql': | ||
return getMysqlOrPostgresqlConnectionString(datasource); | ||
case 'sqlite': | ||
return getSqliteConnectionString(datasource); | ||
case 'sqlserver': | ||
return getSqlServerConnectionString(datasource); | ||
default: | ||
throw new Error(`Unrecognized or unsupported datasource provider ${datasource.provider}`); | ||
} | ||
} | ||
|
||
function getMysqlOrPostgresqlConnectionString(datasource: DataSource): string { | ||
const conn_str_fmt: RegExp = /\w+:\/\/((?<user>.*)\:(?<password>.*)@)?(?<host>.*)\:(?<port>\d{4,5})(\/(?<initial_db>.*))?/; | ||
const raw_conn_str = getUrl(datasource); | ||
const matches = conn_str_fmt.exec(raw_conn_str); | ||
if (!matches) { | ||
throw new Error(`Connection string could not be parsed: ${raw_conn_str}`); | ||
} | ||
return `Host=${matches!.groups!.host!};Database=${matches!.groups!.initial_db!};Username=${matches!.groups!.user!};Password=${matches!.groups!.password!}`; | ||
} | ||
|
||
function getSqliteConnectionString(datasource: DataSource): string { | ||
const conn_str_fmt: RegExp = /file\:(?<db_path>.*)/; | ||
const raw_conn_str = getUrl(datasource); | ||
const matches = conn_str_fmt.exec(raw_conn_str); | ||
if (!matches) { | ||
throw new Error(`Connection string could not be parsed: ${raw_conn_str}`); | ||
} | ||
return `Data Source=${matches!.groups!.db_path!};`; | ||
} | ||
|
||
function getSqlServerConnectionString(datasource: DataSource): string { | ||
const host_fmt: RegExp = /^sqlserver:\/\/(?<host>.*)(\:(?<port>\d{4,5}))?\;(database|Database|DATABASE)\=(?<initial_db>.*)\;(user|User|USER)\=(?<user>.*)\;(password|Password|PASSWORD)\=(?<password>.*)\;(encrypt|Encrypt|ENCRYPT)\=(?<encrypted>(true|false))(\;)?$/; | ||
const raw_conn_str = getUrl(datasource); | ||
const matches = host_fmt.exec(raw_conn_str); | ||
if (!matches) { | ||
throw new Error(`Connection string could not be parsed: ${raw_conn_str}`); | ||
} | ||
return `Host=${matches!.groups!.host!};Database=${matches!.groups!.initial_db!};Username=${matches!.groups!.user!};Password=${matches!.groups!.password!};Encrypt=${matches!.groups!.encrypted!}`; | ||
} | ||
|
||
function getUrl(datasource: DataSource): string { | ||
return (datasource.url.fromEnvVar) | ||
? process.env[datasource.url.fromEnvVar]! | ||
: datasource.url.value | ||
; | ||
} |
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.