From d4ad62bc92142570cfe2ca9bbf2db72d8773e558 Mon Sep 17 00:00:00 2001 From: Ola Okelola Date: Tue, 2 Aug 2022 09:59:22 -0700 Subject: [PATCH] cleanup --- ts/src/action/orchestrator.test.ts | 55 ++++++++++++++++++++++++------ ts/src/core/ent.ts | 11 +++++- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/ts/src/action/orchestrator.test.ts b/ts/src/action/orchestrator.test.ts index 9e6338b69..a97b6c3b5 100644 --- a/ts/src/action/orchestrator.test.ts +++ b/ts/src/action/orchestrator.test.ts @@ -59,7 +59,7 @@ import { Table, } from "../testutils/db/temp_db"; import { Dialect } from "../core/db"; -import { convertList } from "../core/convert"; +import { convertDate, convertList } from "../core/convert"; import { v4 } from "uuid"; jest.mock("pg"); @@ -294,6 +294,18 @@ const SensitiveValuesSchema = getBuilderSchemaFromFields( SensitiveUser, ); +class NullableEvent extends Event {} + +const SchemaWithNullFields = getBuilderSchemaFromFields( + { + startTime: TimestampType(), + endTime: TimestampType({ + nullable: true, + }), + }, + NullableEvent, +); + describe("postgres", () => { commonTests(); }); @@ -321,6 +333,7 @@ describe("sqlite", () => { CustomUserSchema, ContactEmailSchema, SensitiveValuesSchema, + SchemaWithNullFields, ].map((s) => tables.push(getSchemaTable(s, Dialect.SQLite))); return tables; }; @@ -516,14 +529,6 @@ function commonTests() { }); test("schema with null fields", async () => { - const SchemaWithNullFields = getBuilderSchemaFromFields( - { - startTime: TimestampType(), - endTime: TimestampType({ nullable: true }), - }, - User, - ); - const d = new Date(); const builder = new SimpleBuilder( new LoggedOutViewer(), @@ -539,12 +544,20 @@ function commonTests() { validateFieldsExist(fields, "id", "created_at", "updated_at"); validateFieldsDoNotExist(fields, "end_time"); + await builder.saveX(); + const evt = await builder.editedEntX(); + expect(convertDate(evt.data.start_time).toISOString()).toBe( + d.toISOString(), + ); + // undefined in fake postgres, null in sqlite + expect(evt.data.end_time).toBeFalsy(); + const builder2 = new SimpleBuilder( new LoggedOutViewer(), SchemaWithNullFields, new Map([ ["startTime", d], - ["endTime", null], + ["endTime", d], ]), WriteOperation.Insert, null, @@ -554,6 +567,28 @@ function commonTests() { expect(fields2["start_time"]).toEqual(d.toISOString()); validateFieldsExist(fields2, "id", "created_at", "updated_at"); + + await builder2.saveX(); + const evt2 = await builder2.editedEntX(); + expect(convertDate(evt2.data.start_time).toISOString()).toBe( + d.toISOString(), + ); + expect(convertDate(evt2.data.end_time).toISOString()).toBe(d.toISOString()); + + const builder3 = new SimpleBuilder( + new LoggedOutViewer(), + SchemaWithNullFields, + new Map([["endTime", null]]), + WriteOperation.Edit, + evt2, + ); + + await builder3.saveX(); + const evt3 = await builder3.editedEntX(); + expect(convertDate(evt3.data.start_time).toISOString()).toBe( + d.toISOString(), + ); + expect(evt3.data.end_time).toBeNull(); }); test("schema_with_overriden_storage_key", async () => { diff --git a/ts/src/core/ent.ts b/ts/src/core/ent.ts index 0512342d7..d57723374 100644 --- a/ts/src/core/ent.ts +++ b/ts/src/core/ent.ts @@ -1425,6 +1425,7 @@ export function buildUpdateQuery( let valsString: string[] = []; let values: any[] = []; let logValues: any[] = []; + const dialect = DB.getDialect(); let idx = 1; for (const key in options.fields) { @@ -1433,7 +1434,15 @@ export function buildUpdateQuery( if (options.fieldsToLog) { logValues.push(options.fieldsToLog[key]); } - valsString.push(clause.Eq(key, val).clause(idx)); + // TODO would be nice to use clause here. need update version of the queries so that + // we don't have to handle dialect specifics here + // can't use clause because of IS NULL + // valsString.push(clause.Eq(key, val).clause(idx)); + if (dialect === Dialect.Postgres) { + valsString.push(`${key} = $${idx}`); + } else { + valsString.push(`${key} = ?`); + } idx++; }