|
Line 0
Link Here
|
|
|
1 |
use Modern::Perl; |
| 2 |
use Koha::Installer::Output qw(say_warning say_failure say_success say_info); |
| 3 |
|
| 4 |
return { |
| 5 |
bug_number => '37592', |
| 6 |
description => 'Add created_at, updated_at fields to bookings table', |
| 7 |
up => sub { |
| 8 |
my ($args) = @_; |
| 9 |
my ( $dbh, $out ) = @{$args}{qw(dbh out)}; |
| 10 |
|
| 11 |
my $columns_exist_query = <<~'SQL'; |
| 12 |
SELECT column_name |
| 13 |
FROM information_schema.COLUMNS |
| 14 |
WHERE table_name = 'bookings' |
| 15 |
AND column_name IN ('created_at', 'updated_at') |
| 16 |
SQL |
| 17 |
my $existing_columns = $dbh->selectcol_arrayref($columns_exist_query); |
| 18 |
if ( @{$existing_columns} == 2 ) { |
| 19 |
say_info( $out, q{Columns 'created_at' and 'updated_at' already exist in 'bookings' table. Skipping...} ); |
| 20 |
|
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
my $created_at_statement = <<~'SQL'; |
| 25 |
ALTER TABLE bookings ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'The timestamp for when a bookings was created' |
| 26 |
SQL |
| 27 |
my $updated_at_statement = <<~'SQL'; |
| 28 |
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' |
| 29 |
SQL |
| 30 |
if ( @{$existing_columns} == 0 ) { |
| 31 |
if ( $dbh->do("$created_at_statement AFTER `end_date`") ) { |
| 32 |
say_success( $out, q{Added column 'bookings.created_at'} ); |
| 33 |
} else { |
| 34 |
say_failure( $out, q{Failed to add column 'bookings.created_at': } . $dbh->errstr ); |
| 35 |
} |
| 36 |
|
| 37 |
if ( $dbh->do("$updated_at_statement AFTER `created_at`") ) { |
| 38 |
say_success( $out, q{Added column 'bookings.updated_at'} ); |
| 39 |
} else { |
| 40 |
say_failure( $out, q{Failed to add column 'bookings.updated_at': } . $dbh->errstr ); |
| 41 |
} |
| 42 |
|
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
if ( @{$existing_columns} == 1 ) { |
| 47 |
foreach my $column ( 'created_at', 'updated_at' ) { |
| 48 |
if ( column_exists( 'bookings', $column ) ) { |
| 49 |
next; |
| 50 |
} |
| 51 |
|
| 52 |
my $statement; |
| 53 |
if ( $column eq 'created_at' ) { |
| 54 |
$statement = "$created_at_statement AFTER `end_date`"; |
| 55 |
} |
| 56 |
|
| 57 |
if ( $column eq 'updated_at' ) { |
| 58 |
$statement = "$updated_at_statement AFTER `created_at`"; |
| 59 |
} |
| 60 |
|
| 61 |
if ( $dbh->do($statement) ) { |
| 62 |
say_success( $out, "Added column 'bookings.$column'" ); |
| 63 |
} else { |
| 64 |
say_failure( $out, "Failed to add column 'bookings.$column': " . $dbh->errstr ); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
}, |
| 69 |
}; |