Skip to content

Commit

Permalink
driver(adapters): More test fixes focused on neon/pg (#4301)
Browse files Browse the repository at this point in the history
* fix(driver-adapter): add support for UUID type

* Add conversion of UUID to neon

* Add conversion of OID to neon and pg

Fixes writes::data_types::native_types::postgres::postgres::native_int_types

* Prepare adapter-side transformations

* Fix money

* Isolate quaint conversion to their own set of modules

* polymorphic, per flavor conversion. (prep refactoring)

* Revert "polymorphic, per flavor conversion. (prep refactoring)"

This reverts commit 07bddd3.

* Revert "Isolate quaint conversion to their own set of modules"

This reverts commit 2f8439d.

* Update query-engine/driver-adapters/src/proxy.rs

Co-authored-by: Alexey Orlenko <alex@aqrln.net>

* Update query-engine/driver-adapters/src/proxy.rs

Co-authored-by: Alexey Orlenko <alex@aqrln.net>

* Remove unused conversions

* Update query-engine/driver-adapters/src/proxy.rs

Co-authored-by: Alexey Orlenko <alex@aqrln.net>

* fixup! Remove unused conversions

* Update driver-adapter-utils version

* Update drivers versions

* Fix typos in proxy.rs

* formatting

---------

Co-authored-by: jkomyno <skiabo97@gmail.com>
Co-authored-by: Alexey Orlenko <alex@aqrln.net>
  • Loading branch information
3 people committed Sep 29, 2023
1 parent fa67c78 commit a437f71
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions query-engine/driver-adapters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ psl.workspace = true
tracing = "0.1"
tracing-core = "0.1"
metrics = "0.18"
uuid = { version = "1", features = ["v4"] }

# Note: these deps are temporarily specified here to avoid importing them from tiberius (the SQL server driver).
# They will be imported from quaint-core instead in a future PR.
Expand Down
2 changes: 1 addition & 1 deletion query-engine/driver-adapters/js/adapter-neon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prisma/adapter-neon",
"version": "0.4.2",
"version": "0.5.0",
"description": "Prisma's driver adapter for \"@neondatabase/serverless\"",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
18 changes: 13 additions & 5 deletions query-engine/driver-adapters/js/adapter-neon/src/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ export function fieldToColumnType(fieldTypeId: number): ColumnType {
case NeonColumnType['TIMESTAMP']:
return ColumnTypeEnum.DateTime
case NeonColumnType['NUMERIC']:
case NeonColumnType['MONEY']:
return ColumnTypeEnum.Numeric
case NeonColumnType['JSONB']:
return ColumnTypeEnum.Json
case NeonColumnType['UUID']:
return ColumnTypeEnum.Uuid
case NeonColumnType['OID']:
return ColumnTypeEnum.Int64
case NeonColumnType['BPCHAR']:
return ColumnTypeEnum.Char
case NeonColumnType['TEXT']:
case NeonColumnType['VARCHAR']:
case NeonColumnType['BIT']:
case NeonColumnType['VARBIT']:
case NeonColumnType['INET']:
case NeonColumnType['CIDR']:
return ColumnTypeEnum.Text
case NeonColumnType['JSONB']:
return ColumnTypeEnum.Json
default:
if (fieldTypeId >= 10000) {
// Postgres Custom Types
Expand All @@ -60,9 +68,9 @@ function convertJson(json: string): unknown {
}

// return string instead of JavaScript Date object
types.setTypeParser(NeonColumnType.DATE, date => date)
types.setTypeParser(NeonColumnType.TIME, date => date)
types.setTypeParser(NeonColumnType.DATE, date => date)
types.setTypeParser(NeonColumnType.TIMESTAMP, date => date)

types.setTypeParser(NeonColumnType.JSONB, convertJson)
types.setTypeParser(NeonColumnType.JSON, convertJson)
types.setTypeParser(NeonColumnType.MONEY, (money: string) => money.slice(1))
4 changes: 3 additions & 1 deletion query-engine/driver-adapters/js/adapter-neon/src/neon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ abstract class NeonQueryable implements Queryable {

return (await this.performIO(query)).map(({ fields, rows }) => {
const columns = fields.map((field) => field.name)
const columnTypes = fields.map((field) => fieldToColumnType(field.dataTypeID))

return {
columnNames: columns,
columnTypes: fields.map((field) => fieldToColumnType(field.dataTypeID)),
columnTypes,
rows,
}
})
Expand Down
2 changes: 1 addition & 1 deletion query-engine/driver-adapters/js/adapter-pg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prisma/adapter-pg",
"version": "0.4.2",
"version": "0.5.0",
"description": "Prisma's driver adapter for \"pg\"",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
18 changes: 13 additions & 5 deletions query-engine/driver-adapters/js/adapter-pg/src/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ export function fieldToColumnType(fieldTypeId: number): ColumnType {
case PgColumnType['TIMESTAMP']:
return ColumnTypeEnum.DateTime
case PgColumnType['NUMERIC']:
case PgColumnType['MONEY']:
return ColumnTypeEnum.Numeric
case PgColumnType['JSONB']:
return ColumnTypeEnum.Json
case PgColumnType['UUID']:
return ColumnTypeEnum.Uuid
case PgColumnType['OID']:
return ColumnTypeEnum.Int64
case PgColumnType['BPCHAR']:
return ColumnTypeEnum.Char
case PgColumnType['TEXT']:
case PgColumnType['VARCHAR']:
case PgColumnType['BIT']:
case PgColumnType['VARBIT']:
case PgColumnType['INET']:
case PgColumnType['CIDR']:
return ColumnTypeEnum.Text
case PgColumnType['JSONB']:
return ColumnTypeEnum.Json
default:
if (fieldTypeId >= 10000) {
// Postgres Custom Types
Expand All @@ -60,9 +68,9 @@ function convertJson(json: string): unknown {
}

// return string instead of JavaScript Date object
types.setTypeParser(PgColumnType.DATE, date => date)
types.setTypeParser(PgColumnType.TIME, date => date)
types.setTypeParser(PgColumnType.DATE, date => date)
types.setTypeParser(PgColumnType.TIMESTAMP, date => date)

types.setTypeParser(PgColumnType.JSONB, convertJson)
types.setTypeParser(PgColumnType.JSON, convertJson)
types.setTypeParser(PgColumnType.MONEY, (money: string) => money.slice(1))
1 change: 1 addition & 0 deletions query-engine/driver-adapters/js/adapter-pg/src/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PgQueryable<ClientT extends StdClient | TransactionClient> implements Quer

const columns = fields.map((field) => field.name)
const columnTypes = fields.map((field) => fieldToColumnType(field.dataTypeID))

const resultSet: ResultSet = {
columnNames: columns,
columnTypes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prisma/driver-adapter-utils",
"version": "0.7.0",
"version": "0.8.0",
"description": "Internal set of utilities and types for Prisma's driver adapters.",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const ColumnTypeEnum = {
'Json': 11,
'Enum': 12,
'Bytes': 13,
// 'Set': 14,
// 'Array': 15,
'Set': 14,
'Uuid': 15,
// ...
'UnknownNumber': 128
} as const
Expand Down
18 changes: 16 additions & 2 deletions query-engine/driver-adapters/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,13 @@ pub enum ColumnType {
/// This is currently unhandled, and will panic if encountered.
Set = 14,

// Below there are custom types that don't have a 1:1 translation with a quaint::Value.
// enum variant.
/// UUID from postgres-flavored driver adapters is mapped to this type.
Uuid = 15,

/*
* Below there are custom types that don't have a 1:1 translation with a quaint::Value.
* enum variant.
*/
/// UnknownNumber is used when the type of the column is a number but of unknown particular type
/// and precision.
///
Expand Down Expand Up @@ -363,6 +368,15 @@ fn js_value_to_quaint(
"expected a string or an array in column {column_name}, found {mismatch}",
)),
},
ColumnType::Uuid => match json_value {
serde_json::Value::String(s) => uuid::Uuid::parse_str(&s)
.map(QuaintValue::uuid)
.map_err(|_| conversion_error!("Expected a UUID string")),
serde_json::Value::Null => Ok(QuaintValue::Bytes(None)),
mismatch => Err(conversion_error!(
"Expected a UUID string in column {column_name}, found {mismatch}"
)),
},
ColumnType::UnknownNumber => match json_value {
serde_json::Value::Number(n) => n
.as_i64()
Expand Down

0 comments on commit a437f71

Please sign in to comment.