JBAS-1554 Circ history delete untracked
authorBill Erickson <berickxx@gmail.com>
Tue, 15 Aug 2017 14:21:36 +0000 (10:21 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Script to find and delete circulation history rows for patrons who
disabled circ history after the initial migration but before the new
history delete code was deployed.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/sql/schema/tools/circ-history-delete-untracked.sql [new file with mode: 0644]

diff --git a/KCLS/sql/schema/tools/circ-history-delete-untracked.sql b/KCLS/sql/schema/tools/circ-history-delete-untracked.sql
new file mode 100644 (file)
index 0000000..6123346
--- /dev/null
@@ -0,0 +1,24 @@
+/**
+ * Delete circ history rows for patrons who disabled circ history
+ * tracking between the initial migration and when the updated
+ * circ history code was deployed.
+ */
+
+BEGIN;
+
+SET STATEMENT_TIMEOUT = 0;
+
+WITH delete_history_usrs AS (
+    SELECT DISTINCT(auch.usr) AS usr_id
+    FROM action.usr_circ_history auch
+    WHERE NOT EXISTS(
+        SELECT TRUE FROM actor.usr_setting set 
+        WHERE set.usr = auch.usr 
+            AND set.name ~ '^history.circ.retention_'
+    )
+) DELETE FROM action.usr_circ_history 
+    WHERE usr IN (SELECT usr_id FROM delete_history_usrs);
+
+--ROLLBACK;
+COMMIT;
+