Skip to content

Commit

Permalink
fix(js-connectors): datetime with optional microseconds (#4180)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkomyno committed Aug 30, 2023
1 parent 6405302 commit 5fbae57
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ INSERT INTO type_test (
'2023-07-24', -- date
'23:59:59', -- time
2023, -- year
'2023-07-24 23:59:59', -- datetime
'2023-07-24 23:59:59.415', -- datetime
'2023-07-24 23:59:59', -- timestamp
'{"key": "value"}', -- json
'value3', -- enum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ model type_test {
time_column_null DateTime? @db.Time(0)
year_column Int @db.Year
year_column_null Int? @db.Year
datetime_column DateTime @db.DateTime(0)
datetime_column_null DateTime? @db.DateTime(0)
datetime_column DateTime @db.DateTime(3)
datetime_column_null DateTime? @db.DateTime(3)
timestamp_column DateTime @db.Timestamp(0)
timestamp_column_null DateTime? @db.Timestamp(0)
json_column Json
Expand All @@ -59,6 +59,13 @@ model type_test {
set_column_null String?
}

// This will eventually supersede type_test
model type_test_2 {
id String @id @default(cuid())
datetime_column DateTime @default(now()) @db.DateTime(3)
datetime_column_null DateTime? @db.DateTime(3)
}

enum type_test_enum_column {
value1
value2
Expand Down Expand Up @@ -93,7 +100,7 @@ model Parent {
}

model Author {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
firstName String
lastName String
age Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ INSERT INTO type_test (
'This is a long text...', -- text
'2023-07-24', -- date
'23:59:59', -- time
'2023-07-24 23:59:59', -- datetime
'2023-07-24 23:59:59.415', -- datetime
'2023-07-24 23:59:59', -- timestamp
'{"key": "value"}', -- json
'value3' -- enum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ model type_test {
date_column_null DateTime? @db.Date
time_column DateTime @db.Time(0)
time_column_null DateTime? @db.Time(0)
datetime_column DateTime
datetime_column_null DateTime?
datetime_column DateTime @db.Timestamp(3)
datetime_column_null DateTime? @db.Timestamp(3)
timestamp_column DateTime @db.Timestamp(0)
timestamp_column_null DateTime? @db.Timestamp(0)
json_column Json
Expand All @@ -43,6 +43,13 @@ model type_test {
enum_column_null type_test_enum_column_null?
}

// This will eventually supersede type_test
model type_test_2 {
id String @id @default(cuid())
datetime_column DateTime @default(now()) @db.Timestamp(3)
datetime_column_null DateTime? @db.Timestamp(3)
}

model Child {
c String @unique
c_1 String
Expand Down
58 changes: 56 additions & 2 deletions query-engine/js-connectors/js/smoke-test-js/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type { Connector } from '@jkomyno/prisma-js-connector-utils'
import type { QueryEngineInstance } from './engines/types/Library'
import { initQueryEngine } from './util'

type Flavor = Connector['flavour']

export async function smokeTest(db: Connector, prismaSchemaRelativePath: string) {
// wait for the database pool to be initialized
await setImmediate(0)
Expand All @@ -19,6 +17,7 @@ export async function smokeTest(db: Connector, prismaSchemaRelativePath: string)

const test = new SmokeTest(engine, db.flavour)

await test.testTypeTest2()
await test.testFindManyTypeTest()
await test.createAutoIncrement()
await test.testCreateAndDeleteChildParent()
Expand Down Expand Up @@ -48,6 +47,61 @@ export async function smokeTest(db: Connector, prismaSchemaRelativePath: string)
class SmokeTest {
constructor(private readonly engine: QueryEngineInstance, readonly flavour: Connector['flavour']) {}

async testTypeTest2() {
const create = await this.engine.query(`
{
"action": "createOne",
"modelName": "type_test_2",
"query": {
"arguments": {
"data": {}
},
"selection": {
"id": true,
"datetime_column": true,
"datetime_column_null": true
}
}
}
`, 'trace', undefined)

console.log('[nodejs] create', JSON.stringify(JSON.parse(create), null, 2))

const findMany = await this.engine.query(`
{
"action": "findMany",
"modelName": "type_test_2",
"query": {
"selection": {
"id": true,
"datetime_column": true,
"datetime_column_null": true
},
"arguments": {
"where": {}
}
}
}
`, 'trace', undefined)

console.log('[nodejs] findMany', JSON.stringify(JSON.parse(findMany), null, 2))

await this.engine.query(`
{
"action": "deleteMany",
"modelName": "type_test_2",
"query": {
"arguments": {
"where": {}
},
"selection": {
"count": true
}
}
}
`, 'trace', undefined)
}

async testFindManyTypeTest() {
await this.testFindManyTypeTestMySQL()
await this.testFindManyTypeTestPostgres()
Expand Down
26 changes: 24 additions & 2 deletions query-engine/js-connectors/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn js_value_to_quaint(json_value: serde_json::Value, column_type: ColumnType) ->
},
ColumnType::DateTime => match json_value {
serde_json::Value::String(s) => {
let datetime = chrono::NaiveDateTime::parse_from_str(&s, "%Y-%m-%d %H:%M:%S")
let datetime = chrono::NaiveDateTime::parse_from_str(&s, "%Y-%m-%d %H:%M:%S%.f")
.unwrap_or_else(|_| panic!("Expected a datetime string, found {:?}", &s));
let datetime: DateTime<Utc> = DateTime::from_utc(datetime, Utc);
QuaintValue::datetime(datetime)
Expand Down Expand Up @@ -617,13 +617,35 @@ mod proxy_test {
// null
test_null(QuaintValue::DateTime(None), column_type);

let s = "2023-01-01 23:59:59.415";
let json_value = serde_json::Value::String(s.to_string());
let quaint_value = js_value_to_quaint(json_value, column_type);

let datetime = NaiveDate::from_ymd_opt(2023, 1, 1)
.unwrap()
.and_hms_milli_opt(23, 59, 59, 415)
.unwrap();
let datetime = DateTime::from_utc(datetime, Utc);
assert_eq!(quaint_value, QuaintValue::DateTime(Some(datetime)));

let s = "2023-01-01 23:59:59.123456";
let json_value = serde_json::Value::String(s.to_string());
let quaint_value = js_value_to_quaint(json_value, column_type);

let datetime = NaiveDate::from_ymd_opt(2023, 1, 1)
.unwrap()
.and_hms_micro_opt(23, 59, 59, 123_456)
.unwrap();
let datetime = DateTime::from_utc(datetime, Utc);
assert_eq!(quaint_value, QuaintValue::DateTime(Some(datetime)));

let s = "2023-01-01 23:59:59";
let json_value = serde_json::Value::String(s.to_string());
let quaint_value = js_value_to_quaint(json_value, column_type);

let datetime = NaiveDate::from_ymd_opt(2023, 1, 1)
.unwrap()
.and_hms_opt(23, 59, 59)
.and_hms_milli_opt(23, 59, 59, 0)
.unwrap();
let datetime = DateTime::from_utc(datetime, Utc);
assert_eq!(quaint_value, QuaintValue::DateTime(Some(datetime)));
Expand Down

0 comments on commit 5fbae57

Please sign in to comment.