2022-11-15 23:30:37 +01:00
|
|
|
package BurguillosInfo::DB;
|
|
|
|
|
|
|
|
use v5.34.1;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use DBI;
|
|
|
|
use DBD::Pg;
|
|
|
|
|
|
|
|
use BurguillosInfo::DB::Migrations;
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
my $dbh;
|
2023-08-08 19:39:13 +02:00
|
|
|
|
2022-11-15 23:30:37 +01:00
|
|
|
sub connect {
|
2023-08-08 19:39:13 +02:00
|
|
|
if ( defined $dbh ) {
|
|
|
|
return $dbh;
|
2022-11-15 23:30:37 +01:00
|
|
|
}
|
|
|
|
my $class = shift;
|
|
|
|
my $app = shift;
|
|
|
|
my $config = $app->config;
|
|
|
|
my $database = $config->{db}{database};
|
2023-08-08 19:39:13 +02:00
|
|
|
$dbh = DBI->connect(
|
2022-11-15 23:30:37 +01:00
|
|
|
"dbi:Pg:dbname=$database",
|
2023-08-08 19:39:13 +02:00
|
|
|
,
|
|
|
|
undef, undef,
|
|
|
|
{
|
|
|
|
RaiseError => 1,
|
|
|
|
Callbacks => {
|
|
|
|
connected => sub {
|
|
|
|
shift->do('set timezone = UTC');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-11-15 23:30:37 +01:00
|
|
|
);
|
|
|
|
$class->_migrate($dbh);
|
|
|
|
return $dbh;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _migrate {
|
|
|
|
my $class = shift;
|
|
|
|
my $dbh = shift;
|
|
|
|
local $dbh->{RaiseError} = 0;
|
|
|
|
local $dbh->{PrintError} = 0;
|
|
|
|
my @migrations = BurguillosInfo::DB::Migrations::MIGRATIONS();
|
|
|
|
if ( $class->get_current_migration($dbh) > @migrations ) {
|
|
|
|
warn "Something happened there, wrong migration number.";
|
|
|
|
}
|
|
|
|
if ( $class->get_current_migration($dbh) >= @migrations ) {
|
|
|
|
say STDERR "Migrations already applied.";
|
|
|
|
return;
|
|
|
|
}
|
2023-08-08 19:39:13 +02:00
|
|
|
$class->_apply_migrations( $dbh, \@migrations );
|
2022-11-15 23:30:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _apply_migrations {
|
2023-08-08 19:39:13 +02:00
|
|
|
my $class = shift;
|
|
|
|
my $dbh = shift;
|
2022-11-15 23:30:37 +01:00
|
|
|
my $migrations = shift;
|
|
|
|
for (
|
2023-08-08 19:39:13 +02:00
|
|
|
my $i = $class->get_current_migration($dbh) ;
|
2022-11-15 23:30:37 +01:00
|
|
|
$i < @$migrations ;
|
|
|
|
$i++
|
|
|
|
)
|
|
|
|
{
|
2023-08-08 19:39:13 +02:00
|
|
|
local $dbh->{RaiseError} = 1;
|
|
|
|
my $current_migration = $migrations->[$i];
|
|
|
|
my $migration_number = $i + 1;
|
|
|
|
$class->_apply_migration( $dbh, $current_migration, $migration_number );
|
2022-11-15 23:30:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _apply_migration {
|
2023-08-08 19:39:13 +02:00
|
|
|
my $class = shift;
|
|
|
|
my $dbh = shift;
|
|
|
|
my $current_migration = shift;
|
|
|
|
my $migration_number = shift;
|
|
|
|
{
|
|
|
|
if (ref $current_migration eq 'CODE') {
|
|
|
|
$current_migration->($dbh);
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
$dbh->do($current_migration);
|
|
|
|
}
|
|
|
|
$dbh->do( <<'EOF', undef, 'current_migration', $migration_number );
|
2022-11-15 23:30:37 +01:00
|
|
|
INSERT INTO options
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT (name) DO
|
|
|
|
UPDATE SET value = $2;
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_current_migration {
|
|
|
|
my $class = shift;
|
|
|
|
my $dbh = shift;
|
|
|
|
my $result = $dbh->selectrow_hashref( <<'EOF', undef, 'current_migration' );
|
|
|
|
select value from options where name = ?;
|
|
|
|
EOF
|
|
|
|
return int( $result->{value} // 0 );
|
|
|
|
}
|
|
|
|
1;
|