From 96c47dc7b8f9392c5d770bd8efff7ab5175495a0 Mon Sep 17 00:00:00 2001
From: Jason Stephenson <jason@sigio.com>
Date: Thu, 29 Aug 2019 11:06:56 -0400
Subject: [PATCH] Lp 1839002: Default circulation auto_renewal field to FALSE

Change the definition of the action.circulation table so that the
auto_renewal field is not null, default false.

Add an upgrade script to update the field values and alter the field
definition in both the action.circulation and action.aged_circulation
tables.

Signed-off-by: Jason Stephenson <jason@sigio.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
---
 Open-ILS/src/sql/Pg/090.schema.action.sql            |  2 +-
 ...action_circulation-auto_renewal-default-false.sql | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 Open-ILS/src/sql/Pg/upgrade/XXXX.schema.action_circulation-auto_renewal-default-false.sql

diff --git a/Open-ILS/src/sql/Pg/090.schema.action.sql b/Open-ILS/src/sql/Pg/090.schema.action.sql
index e6fe8cd9c4..3fb3b4a782 100644
--- a/Open-ILS/src/sql/Pg/090.schema.action.sql
+++ b/Open-ILS/src/sql/Pg/090.schema.action.sql
@@ -148,7 +148,7 @@ CREATE TABLE action.circulation (
 								   DEFERRABLE INITIALLY DEFERRED,
 	copy_location	INT				NOT NULL DEFAULT 1 REFERENCES asset.copy_location (id) DEFERRABLE INITIALLY DEFERRED,
 	checkin_scan_time   TIMESTAMP WITH TIME ZONE,
-    auto_renewal            BOOLEAN,
+    auto_renewal            BOOLEAN	NOT NULL DEFAULT FALSE,
     auto_renewal_remaining  INTEGER
 ) INHERITS (money.billable_xact);
 ALTER TABLE action.circulation ADD PRIMARY KEY (id);
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.action_circulation-auto_renewal-default-false.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.action_circulation-auto_renewal-default-false.sql
new file mode 100644
index 0000000000..e5fc738084
--- /dev/null
+++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.action_circulation-auto_renewal-default-false.sql
@@ -0,0 +1,20 @@
+BEGIN;
+
+--SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
+
+UPDATE action.circulation SET auto_renewal = FALSE WHERE auto_renewal IS NULL;
+
+UPDATE action.aged_circulation SET auto_renewal = FALSE WHERE auto_renewal IS NULL;
+
+COMMIT;
+
+-- The following two changes cannot occur in a transaction with the
+-- above updates because we will get an error about not being able to
+-- alter a table with pending transactions.  They also need to occur
+-- after the above updates or the SET NOT NULL change will fail.
+
+ALTER TABLE action.circulation ALTER COLUMN auto_renewal SET DEFAULT FALSE;
+ALTER TABLE action.circulation ALTER COLUMN auto_renewal SET NOT NULL;
+
+ALTER TABLE action.aged_circulation ALTER COLUMN auto_renewal SET DEFAULT FALSE;
+ALTER TABLE action.aged_circulation ALTER COLUMN auto_renewal SET NOT NULL;
-- 
2.11.0