changeset 4748:47922c1a088d

Added a 'changed' column to the import.imports table. Changed is update each time the state changes (TODO: solve this with a trigger). The update script takes as approximation the time of the last log entry connected with the particular import.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 18 Oct 2019 15:34:34 +0200
parents b1428b44e43f
children fd9f171b87e4
files pkg/controllers/importqueue.go pkg/imports/queue.go pkg/models/import.go schema/gemma.sql schema/updates/1310/01.import-changed.sql schema/version.sql
diffstat 6 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/importqueue.go	Fri Oct 18 15:17:31 2019 +0200
+++ b/pkg/controllers/importqueue.go	Fri Oct 18 15:34:34 2019 +0200
@@ -45,6 +45,7 @@
   imports.id AS id,
   state::varchar,
   enqueued,
+  changed,
   kind,
   username,
   signer,
@@ -273,10 +274,12 @@
 	for rows.Next() {
 		var it models.Import
 		var enqueued time.Time
+		var changed time.Time
 		if err = rows.Scan(
 			&it.ID,
 			&it.State,
 			&enqueued,
+			&changed,
 			&it.Kind,
 			&it.User,
 			&signer,
@@ -290,6 +293,7 @@
 			it.Signer = signer.String
 		}
 		it.Enqueued = models.ImportTime{Time: enqueued.UTC()}
+		it.Changed = models.ImportTime{Time: changed.UTC()}
 		imports = append(imports, &it)
 	}
 
@@ -446,6 +450,7 @@
 	reviewSQL = `
 UPDATE import.imports SET
   state = $1::import_state,
+  changed = CURRENT_TIMESTAMP,
   signer = $2
 WHERE id = $3`
 
--- a/pkg/imports/queue.go	Fri Oct 18 15:17:31 2019 +0200
+++ b/pkg/imports/queue.go	Fri Oct 18 15:34:34 2019 +0200
@@ -138,7 +138,9 @@
 	queueUser = "sys_admin"
 
 	reEnqueueRunningSQL = `
-UPDATE import.imports SET state = 'queued'::import_state
+UPDATE import.imports SET
+  state = 'queued'::import_state,
+  changed = CURRENT_TIMESTAMP
 WHERE state = 'running'::import_state`
 
 	insertJobSQL = `
@@ -180,12 +182,15 @@
 LIMIT 1`
 
 	updateStateSQL = `
-UPDATE import.imports SET state = $1::import_state
+UPDATE import.imports SET
+  state = $1::import_state,
+  changed = CURRENT_TIMESTAMP
 WHERE id = $2`
 
 	updateStateSummarySQL = `
 UPDATE import.imports SET
    state = $1::import_state,
+   changed = CURRENT_TIMESTAMP,
    summary = $2
 WHERE id = $3`
 
--- a/pkg/models/import.go	Fri Oct 18 15:17:31 2019 +0200
+++ b/pkg/models/import.go	Fri Oct 18 15:34:34 2019 +0200
@@ -28,6 +28,7 @@
 		ID       int64      `json:"id"`
 		State    string     `json:"state"`
 		Enqueued ImportTime `json:"enqueued"`
+		Changed  ImportTime `json:"changed"`
 		Kind     string     `json:"kind"`
 		User     string     `json:"user"`
 		Signer   string     `json:"signer,omitempty"`
--- a/schema/gemma.sql	Fri Oct 18 15:17:31 2019 +0200
+++ b/schema/gemma.sql	Fri Oct 18 15:34:34 2019 +0200
@@ -864,6 +864,7 @@
         state      import_state NOT NULL DEFAULT 'queued',
         kind       varchar   NOT NULL,
         enqueued   timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
+        changed    timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
         due        timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
         retry_wait interval
             CHECK(retry_wait IS NULL
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/schema/updates/1310/01.import-changed.sql	Fri Oct 18 15:34:34 2019 +0200
@@ -0,0 +1,6 @@
+ALTER TABLE import.imports ADD COLUMN changed timestamp WITH time zone NOT NULL DEFAULT CURRENT_TIMESTAMP;
+
+UPDATE import.imports imp SET
+  changed = (SELECT coalesce(max(time), imp.changed)
+             FROM import.import_logs
+             WHERE import_id = imp.id);
--- a/schema/version.sql	Fri Oct 18 15:17:31 2019 +0200
+++ b/schema/version.sql	Fri Oct 18 15:34:34 2019 +0200
@@ -1,1 +1,1 @@
-INSERT INTO gemma_schema_version(version) VALUES (1309);
+INSERT INTO gemma_schema_version(version) VALUES (1310);