database: refactor schema/iris version check

This commit is contained in:
Daniel Friesel 2022-12-11 20:10:58 +01:00
parent a325c23ff9
commit 5de6dfb4e1
No known key found for this signature in database
GPG key ID: 100D5BFB5166E005

View file

@ -14,27 +14,13 @@ has description => 'Initialize or upgrade database layout';
has usage => sub { shift->extract_usage }; has usage => sub { shift->extract_usage };
sub get_iris_version {
my ($db) = @_;
my $version;
eval { $version = $db->select( 'schema_version', ['iris'] )->hash->{iris}; };
if ($@) {
# If it failed, the version table does not exist -> run setup first.
return undef;
}
return $version;
}
sub get_schema_version { sub get_schema_version {
my ($db) = @_; my ( $db, $key ) = @_;
my $version; my $version;
eval { $key //= 'version';
$version
= $db->select( 'schema_version', ['version'] )->hash->{version}; eval { $version = $db->select( 'schema_version', [$key] )->hash->{$key}; };
};
if ($@) { if ($@) {
# If it failed, the version table does not exist -> run setup first. # If it failed, the version table does not exist -> run setup first.
@ -1430,7 +1416,7 @@ sub migrate_db {
exit(1); exit(1);
} }
my $iris_version = get_iris_version($db); my $iris_version = get_schema_version( $db, 'iris' );
say "Found IRIS station database v${iris_version}"; say "Found IRIS station database v${iris_version}";
if ( $iris_version eq $Travel::Status::DE::IRIS::Stations::VERSION ) { if ( $iris_version eq $Travel::Status::DE::IRIS::Stations::VERSION ) {
say 'Station database is up-to-date'; say 'Station database is up-to-date';