From 2dec6cb6d1c34154956aa9e4baa410c0688d92c0 Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Wed, 7 Aug 2024 15:25:25 +0000 Subject: [PATCH] Bug 37592: Add created_at, updated_at fields to bookings table Signed-off-by: Owen Leonard Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- ...2-add_created_at_updated_at_to_bookings.pl | 69 +++++++++++++++++++ installer/data/mysql/kohastructure.sql | 2 + 2 files changed, 71 insertions(+) create mode 100755 installer/data/mysql/atomicupdate/bug_37592-add_created_at_updated_at_to_bookings.pl diff --git a/installer/data/mysql/atomicupdate/bug_37592-add_created_at_updated_at_to_bookings.pl b/installer/data/mysql/atomicupdate/bug_37592-add_created_at_updated_at_to_bookings.pl new file mode 100755 index 0000000000..66a6c7421e --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_37592-add_created_at_updated_at_to_bookings.pl @@ -0,0 +1,69 @@ +use Modern::Perl; +use Koha::Installer::Output qw(say_warning say_failure say_success say_info); + +return { + bug_number => '37592', + description => 'Add created_at, updated_at fields to bookings table', + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @{$args}{qw(dbh out)}; + + my $columns_exist_query = <<~'SQL'; + SELECT column_name + FROM information_schema.COLUMNS + WHERE table_name = 'bookings' + AND column_name IN ('created_at', 'updated_at') + SQL + my $existing_columns = $dbh->selectcol_arrayref($columns_exist_query); + if ( @{$existing_columns} == 2 ) { + say_info( $out, q{Columns 'created_at' and 'updated_at' already exist in 'bookings' table. Skipping...} ); + + return; + } + + my $created_at_statement = <<~'SQL'; + ALTER TABLE bookings ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'The timestamp for when a bookings was created' + SQL + my $updated_at_statement = <<~'SQL'; + ALTER TABLE bookings ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'The timestamp for when a booking has been updated' + SQL + if ( @{$existing_columns} == 0 ) { + if ( $dbh->do("$created_at_statement AFTER `end_date`") ) { + say_success( $out, q{Added column 'bookings.created_at'} ); + } else { + say_failure( $out, q{Failed to add column 'bookings.created_at': } . $dbh->errstr ); + } + + if ( $dbh->do("$updated_at_statement AFTER `created_at`") ) { + say_success( $out, q{Added column 'bookings.updated_at'} ); + } else { + say_failure( $out, q{Failed to add column 'bookings.updated_at': } . $dbh->errstr ); + } + + return; + } + + if ( @{$existing_columns} == 1 ) { + foreach my $column ( 'created_at', 'updated_at' ) { + if ( column_exists( 'bookings', $column ) ) { + next; + } + + my $statement; + if ( $column eq 'created_at' ) { + $statement = "$created_at_statement AFTER `end_date`"; + } + + if ( $column eq 'updated_at' ) { + $statement = "$updated_at_statement AFTER `created_at`"; + } + + if ( $dbh->do($statement) ) { + say_success( $out, "Added column 'bookings.$column'" ); + } else { + say_failure( $out, "Failed to add column 'bookings.$column': " . $dbh->errstr ); + } + } + } + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index f3dd950328..e829cacc62 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -1217,6 +1217,8 @@ CREATE TABLE `bookings` ( `pickup_library_id` varchar(10) NOT NULL COMMENT 'Identifier for booking pickup library', `start_date` datetime DEFAULT NULL COMMENT 'the start date of the booking', `end_date` datetime DEFAULT NULL COMMENT 'the end date of the booking', + `created_at` timestamp DEFAULT current_timestamp() COMMENT 'the timestamp for when a booking was created', + `updated_at` timestamp DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT 'the timestamp for when a booking has been updated', PRIMARY KEY (`booking_id`), KEY `patron_id` (`patron_id`), KEY `biblio_id` (`biblio_id`), -- 2.39.5