From 30a307671749ff7f1c60c60986e173c5baea86a5 Mon Sep 17 00:00:00 2001
From: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Date: Mon, 10 Feb 2025 10:07:36 +0000
Subject: [PATCH] Bug 34587: (follow-up) Fix DB inconsistencies

---
 .../atomicupdate/bug_34587-fix_timestamps.pl  | 33 +++++++++++++++++++
 installer/data/mysql/db_revs/230600056.pl     |  4 +--
 2 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/bug_34587-fix_timestamps.pl

diff --git a/installer/data/mysql/atomicupdate/bug_34587-fix_timestamps.pl b/installer/data/mysql/atomicupdate/bug_34587-fix_timestamps.pl
new file mode 100644
index 00000000000..d807cb6cd47
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_34587-fix_timestamps.pl
@@ -0,0 +1,33 @@
+use Modern::Perl;
+use Koha::Installer::Output qw(say_warning say_success say_info);
+
+return {
+    bug_number  => "34587",
+    description => "Follow up to fix incorrect default values for timestamps",
+    up          => sub {
+        my ($args) = @_;
+        my ( $dbh, $out ) = @$args{qw(dbh out)};
+
+        if ( TableExists('erm_counter_files') ) {
+            if ( column_exists( 'erm_counter_files', 'date_uploaded' ) ) {
+                $dbh->do(
+                    q{
+                    ALTER TABLE erm_counter_files CHANGE COLUMN date_uploaded date_uploaded timestamp DEFAULT current_timestamp() COMMENT 'counter file upload date'
+                }
+                );
+                say_success( $out, "Successfully corrected incorrect default value for 'erm_counter_files.date_uploaded" );
+            }
+        }
+
+        if ( TableExists('erm_counter_logs') ) {
+            if ( column_exists( 'erm_counter_logs', 'importdate' ) ) {
+                $dbh->do(
+                    q{
+                    ALTER TABLE erm_counter_logs CHANGE COLUMN importdate importdate timestamp DEFAULT current_timestamp() COMMENT 'counter file import date'
+                }
+                );
+                say_success( $out, "Successfully corrected incorrect default value for 'erm_counter_logs.importdate" );
+            }
+        }
+    },
+};
diff --git a/installer/data/mysql/db_revs/230600056.pl b/installer/data/mysql/db_revs/230600056.pl
index 89cb9029130..aed47cd5992 100755
--- a/installer/data/mysql/db_revs/230600056.pl
+++ b/installer/data/mysql/db_revs/230600056.pl
@@ -43,7 +43,7 @@ return {
                 `type` varchar(80) DEFAULT NULL COMMENT 'type of counter file',
                 `filename` varchar(80) DEFAULT NULL COMMENT 'name of the counter file',
                 `file_content` longblob DEFAULT NULL COMMENT 'content of the counter file',
-                `date_uploaded` timestamp DEFAULT NULL DEFAULT current_timestamp() COMMENT 'counter file upload date',
+                `date_uploaded` timestamp DEFAULT current_timestamp() COMMENT 'counter file upload date',
                 PRIMARY KEY (`erm_counter_files_id`),
                 CONSTRAINT `erm_counter_files_ibfk_1` FOREIGN KEY (`usage_data_provider_id`) REFERENCES `erm_usage_data_providers` (`erm_usage_data_provider_id`) ON DELETE CASCADE ON UPDATE CASCADE
                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
@@ -61,7 +61,7 @@ return {
                 `borrowernumber` int(11) DEFAULT NULL COMMENT 'foreign key to borrowers',
                 `counter_files_id` int(11) DEFAULT NULL COMMENT 'foreign key to erm_counter_files',
                 `usage_data_provider_id` int(11) DEFAULT NULL COMMENT 'foreign key to erm_usage_data_providers',
-                `importdate` timestamp DEFAULT NULL DEFAULT current_timestamp() COMMENT 'counter file import date',
+                `importdate` timestamp DEFAULT current_timestamp() COMMENT 'counter file import date',
                 `filename` varchar(80) DEFAULT NULL COMMENT 'name of the counter file',
                 `logdetails` longtext DEFAULT NULL COMMENT 'details from the counter log',
                 PRIMARY KEY (`erm_counter_log_id`),
-- 
2.48.1