From 5026e3963cde8a19a908481651bd0c5b2862b67c Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sat, 18 May 2019 12:18:35 -0500 Subject: [PATCH] Update API to handle the multiple formats of datetime Postgres might use. --- api/database-prod.json | 5 +++++ api/personal_measure_api.nimble | 2 +- .../main/nim/personal_measure_apipkg/db_util.nim | 14 ++++++++++++-- .../20190214122514-initial-schema-up.sql | 8 ++++---- 4 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 api/database-prod.json diff --git a/api/database-prod.json b/api/database-prod.json new file mode 100644 index 0000000..4b0762a --- /dev/null +++ b/api/database-prod.json @@ -0,0 +1,5 @@ +{ + "driver": "postgres", + "connectionString": "host=localhost port=5999 dbname=personal_measure user=postgres", + "sqlDir": "src/main/sql/migrations" +} diff --git a/api/personal_measure_api.nimble b/api/personal_measure_api.nimble index b861016..413d79e 100644 --- a/api/personal_measure_api.nimble +++ b/api/personal_measure_api.nimble @@ -17,4 +17,4 @@ requires @["nim >= 0.19.4", "bcrypt", "docopt >= 0.6.8", "isaac >= 0.1.3", "jester >= 0.4.1", "jwt", "tempfile", "uuids >= 0.1.10" ] requires "https://git.jdb-labs.com/jdb/nim-cli-utils.git >= 0.6.3" -requires "https://git.jdb-labs.com/jdb/nim-time-utils.git >= 0.4.0" +requires "https://git.jdb-labs.com/jdb/nim-time-utils.git >= 0.5.0" diff --git a/api/src/main/nim/personal_measure_apipkg/db_util.nim b/api/src/main/nim/personal_measure_apipkg/db_util.nim index 8ef9b18..31ac93b 100644 --- a/api/src/main/nim/personal_measure_apipkg/db_util.nim +++ b/api/src/main/nim/personal_measure_apipkg/db_util.nim @@ -2,7 +2,10 @@ import json, macros, options, sequtils, strutils, times, timeutils, unicode, uuids const UNDERSCORE_RUNE = "_".toRunes[0] -const PG_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:sszz" +const PG_TIMESTAMP_FORMATS = [ + "yyyy-MM-dd HH:mm:sszz", + "yyyy-MM-dd HH:mm:ss'.'fffzz" +] type MutateClauses* = object @@ -62,6 +65,13 @@ proc dbFormat*[T](item: T): string = return $item type DbArrayParseState = enum expectStart, inQuote, inVal, expectEnd +proc parsePGDatetime*(val: string): DateTime = + var errStr = "" + for df in PG_TIMESTAMP_FORMATS: + try: return val.parse(df) + except: errStr &= "\n" & getCurrentExceptionMsg() + raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr) + proc parseDbArray*(val: string): seq[string] = result = newSeq[string]() @@ -129,7 +139,7 @@ proc createParseStmt*(t, value: NimNode): NimNode = result = quote do: parseUUID(`value`) elif t.getType == DateTime.getType: - result = quote do: `value`.parse(PG_TIMESTAMP_FORMAT) + result = quote do: parsePGDatetime(`value`) elif t.getTypeInst == Option.getType: let innerType = t.getTypeImpl[2][0][0][1] diff --git a/api/src/main/sql/migrations/20190214122514-initial-schema-up.sql b/api/src/main/sql/migrations/20190214122514-initial-schema-up.sql index f32a57e..cda96f3 100644 --- a/api/src/main/sql/migrations/20190214122514-initial-schema-up.sql +++ b/api/src/main/sql/migrations/20190214122514-initial-schema-up.sql @@ -14,8 +14,8 @@ create table "api_tokens" ( id uuid default uuid_generate_v4() primary key, user_id uuid not null references users (id) on delete cascade on update cascade, name varchar not null, - created timestamp with time zone not null default current_timestamp, - expires timestamp with time zone default null, + created timestamp(0) with time zone not null default current_timestamp, + expires timestamp(0) with time zone default null, hashed_token varchar not null ); @@ -33,7 +33,7 @@ create table "measurements" ( id uuid default uuid_generate_v4() primary key, measure_id uuid not null references measures (id) on delete cascade on update cascade, value integer not null, - "timestamp" timestamp with time zone not null default current_timestamp, + "timestamp" timestamp(0) with time zone not null default current_timestamp, ext_data jsonb not null default '{}'::json ); @@ -44,7 +44,7 @@ create table client_log_entries ( "scope" varchar not null, message varchar not null, stacktrace varchar not null, - "timestamp" timestamp with time zone not null default current_timestamp + "timestamp" timestamp(0) with time zone not null default current_timestamp ); create index client_log_entries_by_level on client_log_entries ("level");