Skip to content

Commit 9955958

Browse files
Merge pull request #300 from yetanalytics/timestamp_updates
Timestamp updates
2 parents 7dfc2cd + c22c561 commit 9955958

File tree

6 files changed

+124
-6
lines changed

6 files changed

+124
-6
lines changed

src/db/postgres/lrsql/postgres/data.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@
5050
IPersistentMap
5151
(set-parameter [^IPersistentMap m ^PreparedStatement stmt ^long i]
5252
(.setObject stmt i (json->pg-object m)))))
53+
54+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55+
;; Timezone Input
56+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57+
58+
(def local-tz-input
59+
"Returns a properly formatted hug input map to inject a timezone id into a
60+
query needing a timezone id"
61+
{:tz-id (str "'" u/local-zone-id "'")})

src/db/postgres/lrsql/postgres/record.clj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@
5959
(migrate-xapi-statement-timestamps! tx))
6060
(when-not (some? (query-xapi-statement-stored-exists tx))
6161
(alter-xapi-statement-add-stored! tx)
62-
(migrate-xapi-statement-stored-times! tx)))
62+
(migrate-xapi-statement-stored-times! tx))
63+
(when-not (some? (query-state-document-last-modified-is-timestamptz tx))
64+
(migrate-state-document-last-modified! tx pd/local-tz-input)
65+
(migrate-activity-profile-document-last-modified! tx pd/local-tz-input)
66+
(migrate-agent-profile-document-last-modified! tx pd/local-tz-input)))
6367

6468
bp/BackendUtil
6569
(-txn-retry? [_ ex]

src/db/postgres/lrsql/postgres/sql/ddl.sql

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ CREATE TYPE scope_enum AS ENUM (
282282
ALTER TABLE IF EXISTS credential_to_scope ALTER COLUMN scope TYPE scope_enum USING (scope::scope_enum);
283283

284284

285-
/* Migration 2022-05-08-00 - Add timestamp to xapi_statement */
285+
/* Migration 2023-05-08-00 - Add timestamp to xapi_statement */
286286

287287
-- :name query-xapi-statement-timestamp-exists
288288
-- :command :query
@@ -300,7 +300,7 @@ ALTER TABLE xapi_statement ADD COLUMN timestamp TIMESTAMPTZ
300300
-- :doc Backfill `xapi_statement.timestamp` with the values from the payload
301301
UPDATE xapi_statement SET timestamp = (payload->>'timestamp')::timestamptz WHERE timestamp IS NULL;
302302

303-
/* Migration 2022-05-08-01 - Add stored to xapi_statement */
303+
/* Migration 2023-05-08-01 - Add stored to xapi_statement */
304304

305305
-- :name query-xapi-statement-stored-exists
306306
-- :command :query
@@ -317,3 +317,30 @@ ALTER TABLE xapi_statement ADD COLUMN stored TIMESTAMPTZ
317317
-- :command :execute
318318
-- :doc Backfill `xapi_statement.stored` with the values from the payload
319319
UPDATE xapi_statement SET stored = (payload->>'stored')::timestamptz WHERE stored IS NULL;
320+
321+
/* Migration 2023-05-11-00 - Convert timestamps for consistency */
322+
323+
-- :name query-state-document-last-modified-is-timestamptz
324+
-- :command :query
325+
-- :result :one
326+
-- :doc Query to see if `state_document.last_modified` is a timestamp.
327+
SELECT * FROM information_schema.columns
328+
WHERE table_name = 'state_document' AND column_name = 'last_modified' AND data_type = 'timestamp with time zone';
329+
330+
-- :name migrate-state-document-last-modified!
331+
-- :command :execute
332+
-- :doc Migrate the `state_document.last_modified` to have a timezone, and use the provided timezone to work backwards to Zulu
333+
ALTER TABLE state_document ALTER COLUMN last_modified TYPE TIMESTAMP WITH TIME ZONE
334+
USING last_modified AT TIME ZONE :sql:tz-id;
335+
336+
-- :name migrate-activity-profile-document-last-modified!
337+
-- :command :execute
338+
-- :doc Migrate the `activity_profile_document.last_modified` to have a timezone, and use the provided timezone to work backwards to Zulu
339+
ALTER TABLE activity_profile_document ALTER COLUMN last_modified TYPE TIMESTAMP WITH TIME ZONE
340+
USING last_modified AT TIME ZONE :sql:tz-id;
341+
342+
-- :name migrate-agent-profile-document-last-modified!
343+
-- :command :execute
344+
-- :doc Migrate the `agent_profile_document.last_modified` to have a timezone, and use the provided timezone to work backwards to Zulu
345+
ALTER TABLE agent_profile_document ALTER COLUMN last_modified TYPE TIMESTAMP WITH TIME ZONE
346+
USING last_modified AT TIME ZONE :sql:tz-id;

src/db/sqlite/lrsql/sqlite/record.clj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@
8989
(when-not (some? (query-xapi-statement-stored-exists tx))
9090
(alter-xapi-statement-add-stored! tx)
9191
(migrate-xapi-statement-stored-times! tx))
92+
(when-not (some? (query-state-document-last-modified-is-timestamp tx))
93+
(migrate-timestamps-state-01! tx)
94+
(migrate-timestamps-state-02! tx)
95+
(migrate-timestamps-state-03! tx)
96+
(migrate-timestamps-state-04! tx)
97+
(migrate-timestamps-agent-profile-01! tx)
98+
(migrate-timestamps-agent-profile-02! tx)
99+
(migrate-timestamps-agent-profile-03! tx)
100+
(migrate-timestamps-agent-profile-04! tx)
101+
(migrate-timestamps-activity-profile-01! tx)
102+
(migrate-timestamps-activity-profile-02! tx)
103+
(migrate-timestamps-activity-profile-03! tx)
104+
(migrate-timestamps-activity-profile-04! tx))
92105
(update-schema-simple! tx alter-credential-to-scope-scope-datatype!)
93106
(log/infof "sqlite schema_version: %d"
94107
(:schema_version (query-schema-version tx))))

src/db/sqlite/lrsql/sqlite/sql/ddl.sql

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ SET sql = 'CREATE TABLE credential_to_scope (
329329
)'
330330
WHERE type = 'table' AND name = 'credential_to_scope'
331331

332-
/* Migration 2022-05-08-00 - Add timestamp to xapi_statement */
332+
/* Migration 2023-05-08-00 - Add timestamp to xapi_statement */
333333

334334
-- :name query-xapi-statement-timestamp-exists
335335
-- :command :query
@@ -348,7 +348,7 @@ ALTER TABLE xapi_statement ADD COLUMN timestamp TIMESTAMP
348348
UPDATE xapi_statement SET timestamp = strftime('%Y-%m-%dT%H:%M:%f000000Z', json_extract(payload, '$.timestamp'))
349349
WHERE timestamp IS NULL;
350350

351-
/* Migration 2022-05-08-01 - Add stored to xapi_statement */
351+
/* Migration 2023-05-08-01 - Add stored to xapi_statement */
352352

353353
-- :name query-xapi-statement-stored-exists
354354
-- :command :query
@@ -366,3 +366,63 @@ ALTER TABLE xapi_statement ADD COLUMN stored TIMESTAMP
366366
-- :doc Backfill `xapi_statement.stored` with the values from the payload
367367
UPDATE xapi_statement SET stored = strftime('%Y-%m-%dT%H:%M:%f000000Z', json_extract(payload, '$.stored'))
368368
WHERE stored IS NULL;
369+
370+
371+
/* Migration 2023-05-11-00 - Convert timestamps for consistency */
372+
373+
-- :name query-state-document-last-modified-is-timestamp
374+
-- :command :query
375+
-- :result :one
376+
-- :doc Query to see if `state_document.last_modified` is a timestamp.
377+
SELECT 1 FROM pragma_table_info('state_document') WHERE name = 'last_modified' AND type = 'TIMESTAMP';
378+
379+
-- :name migrate-timestamps-state-01!
380+
-- :command :execute
381+
-- :doc Convert `state_document.last_modified` to timestamp - 01
382+
ALTER TABLE state_document ADD COLUMN last_modified_tmp TIMESTAMP;
383+
-- :name migrate-timestamps-state-02!
384+
-- :command :execute
385+
-- :doc Convert `state_document.last_modified` to timestamp - 02
386+
UPDATE state_document SET last_modified_tmp = last_modified WHERE last_modified_tmp IS NULL;
387+
-- :name migrate-timestamps-state-03!
388+
-- :command :execute
389+
-- :doc Convert `state_document.last_modified` to timestamp - 03
390+
ALTER TABLE state_document DROP COLUMN last_modified;
391+
-- :name migrate-timestamps-state-04!
392+
-- :command :execute
393+
-- :doc Convert `state_document.last_modified` to timestamp - 04
394+
ALTER TABLE state_document RENAME COLUMN last_modified_tmp TO last_modified;
395+
396+
-- :name migrate-timestamps-agent-profile-01!
397+
-- :command :execute
398+
-- :doc Convert `agent_profile_document.last_modified` to timestamp - 01
399+
ALTER TABLE agent_profile_document ADD COLUMN last_modified_tmp TIMESTAMP;
400+
-- :name migrate-timestamps-agent-profile-02!
401+
-- :command :execute
402+
-- :doc Convert `agent_profile_document.last_modified` to timestamp - 02
403+
UPDATE agent_profile_document SET last_modified_tmp = last_modified WHERE last_modified_tmp IS NULL;
404+
-- :name migrate-timestamps-agent-profile-03!
405+
-- :command :execute
406+
-- :doc Convert `agent_profile_document.last_modified` to timestamp - 03
407+
ALTER TABLE agent_profile_document DROP COLUMN last_modified;
408+
-- :name migrate-timestamps-agent-profile-04!
409+
-- :command :execute
410+
-- :doc Convert `agent_profile_document.last_modified` to timestamp - 04
411+
ALTER TABLE agent_profile_document RENAME COLUMN last_modified_tmp TO last_modified;
412+
413+
-- :name migrate-timestamps-activity-profile-01!
414+
-- :command :execute
415+
-- :doc Convert `activity_profile_document.last_modified` to timestamp - 01
416+
ALTER TABLE activity_profile_document ADD COLUMN last_modified_tmp TIMESTAMP;
417+
-- :name migrate-timestamps-activity-profile-02!
418+
-- :command :execute
419+
-- :doc Convert `activity_profile_document.last_modified` to timestamp - 02
420+
UPDATE activity_profile_document SET last_modified_tmp = last_modified WHERE last_modified_tmp IS NULL;
421+
-- :name migrate-timestamps-activity-profile-03!
422+
-- :command :execute
423+
-- :doc Convert `activity_profile_document.last_modified` to timestamp - 03
424+
ALTER TABLE activity_profile_document DROP COLUMN last_modified;
425+
-- :name migrate-timestamps-activity-profile-04!
426+
-- :command :execute
427+
-- :doc Convert `activity_profile_document.last_modified` to timestamp - 04
428+
ALTER TABLE activity_profile_document RENAME COLUMN last_modified_tmp TO last_modified;

src/main/lrsql/util.clj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
[com.yetanalytics.lrs.xapi.document :refer [json-bytes-gen-fn]]
1212
[com.yetanalytics.lrs.xapi.statements.timestamp :refer [normalize]]
1313
[lrsql.spec.common :as cs :refer [instant-spec]])
14-
(:import [java.util UUID]
14+
(:import [java.util UUID Calendar]
1515
[java.time Instant]
1616
[java.io StringReader PushbackReader ByteArrayOutputStream]
1717
[java.nio.charset Charset]))
@@ -120,6 +120,11 @@
120120
str-length
121121
"0000-01-01T00:00:00.000000000Z")))))
122122

123+
(def local-zone-id
124+
"Returns the string zoneId for the local calendar's timezone. For use with
125+
migrations from zoneless local time."
126+
(.getID (.getTimeZone (Calendar/getInstance))))
127+
123128
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124129
;; UUIDs
125130
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

0 commit comments

Comments
 (0)