@@ -, +, @@ table --- ...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 --- a/installer/data/mysql/atomicupdate/bug_37592-add_created_at_updated_at_to_bookings.pl +++ a/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 ); + } + } + } + }, +}; --- a/installer/data/mysql/kohastructure.sql +++ a/installer/data/mysql/kohastructure.sql @@ -1216,6 +1216,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`), --