Skip to content

Commit

Permalink
adapter-planetscale: handle NULL column type (prisma#4320)
Browse files Browse the repository at this point in the history
When a column type is NULL, mark it as `ColumnType.Int32` in JS to
eventually convert it to `quaint::ValueType::Int32(None)`, just like
quaint does in its own MySQL connector.

Fixes: prisma/prisma#21369
  • Loading branch information
aqrln authored Oct 6, 2023
1 parent 3aa8ec0 commit 2737bde
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chrono::{DateTime, FixedOffset};
use prisma_value::encode_bytes;
use query_tests_setup::{TestError, TestResult};

pub fn fmt_query_raw(query: &str, params: Vec<RawParam>) -> String {
pub fn fmt_query_raw(query: &str, params: impl IntoIterator<Item = RawParam>) -> String {
let params = params_to_json(params);
let params = serde_json::to_string(&params).unwrap();

Expand All @@ -13,7 +13,7 @@ pub fn fmt_query_raw(query: &str, params: Vec<RawParam>) -> String {
)
}

pub fn fmt_execute_raw(query: &str, params: Vec<RawParam>) -> String {
pub fn fmt_execute_raw(query: &str, params: impl IntoIterator<Item = RawParam>) -> String {
let params = params_to_json(params);
let params = serde_json::to_string(&params).unwrap();

Expand Down Expand Up @@ -66,7 +66,7 @@ impl RawParam {
}
}

fn params_to_json(params: Vec<RawParam>) -> Vec<serde_json::Value> {
fn params_to_json(params: impl IntoIterator<Item = RawParam>) -> Vec<serde_json::Value> {
params.into_iter().map(serde_json::Value::from).collect::<Vec<_>>()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod prisma_16760;
mod prisma_17103;
mod prisma_18517;
mod prisma_20799;
mod prisma_21369;
mod prisma_5952;
mod prisma_6173;
mod prisma_7010;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use query_engine_tests::*;

#[test_suite(schema(generic), exclude(MongoDb))]
mod prisma_21369 {
#[connector_test]
async fn select_null_works(runner: Runner) -> TestResult<()> {
let query = fmt_query_raw("SELECT NULL AS result", []);
let result = run_query!(runner, query);

assert_eq!(
result,
r#"{"data":{"queryRaw":[{"result":{"prisma__type":"null","prisma__value":null}}]}}"#
);

Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ColumnTypeEnum, type ColumnType } from '@prisma/driver-adapter-utils'

// See: https://github.com/planetscale/vitess-types/blob/06235e372d2050b4c0fff49972df8111e696c564/src/vitess/query/v16/query.proto#L108-L218
export type PlanetScaleColumnType
= 'NULL_TYPE' // unsupported
= 'NULL'
| 'INT8'
| 'UINT8'
| 'INT16'
Expand Down Expand Up @@ -89,6 +89,9 @@ export function fieldToColumnType(field: PlanetScaleColumnType): ColumnType {
case 'HEXVAL':
case 'GEOMETRY':
return ColumnTypeEnum.Bytes
case 'NULL':
// Fall back to Int32 for consistency with quaint.
return ColumnTypeEnum.Int32
default:
throw new Error(`Unsupported column type: ${field}`)
}
Expand Down

0 comments on commit 2737bde

Please sign in to comment.