Merge branch 'db_setup' of https://github.com/feuerrot/travelynx into feuerrot-db_setup
This commit is contained in:
commit
a77f836f4c
2 changed files with 59 additions and 41 deletions
|
@ -54,7 +54,7 @@ Debian 9 system, though setup on other distribution should be similar.
|
|||
* Create the database: `sudo -u postgres createdb -O travelynx travelynx`
|
||||
* Copy `examples/travelynx.conf` to the application root directory
|
||||
(the one in which `index.pl` resides) and configure it
|
||||
* Initialize the database: `perl index.pl database setup`
|
||||
* Initialize the database: `perl index.pl database migrate`
|
||||
|
||||
Your server also needs to be able to send mail. Set up your MTA of choice and
|
||||
make sure that the sendmail binary can be used for outgoing mails. Mail
|
||||
|
|
|
@ -9,10 +9,18 @@ has usage => sub { shift->extract_usage };
|
|||
|
||||
sub get_schema_version {
|
||||
my ($dbh) = @_;
|
||||
for my $entry (
|
||||
$dbh->selectall_array(qq{select version from schema_version}) )
|
||||
{
|
||||
return $entry->[0];
|
||||
my $schema_version
|
||||
= $dbh->selectall_arrayref(qq{select version from schema_version});
|
||||
|
||||
if ( not defined $schema_version ) {
|
||||
return undef;
|
||||
}
|
||||
elsif ( @{$schema_version} == 1 ) {
|
||||
return $schema_version->[0][0];
|
||||
}
|
||||
else {
|
||||
printf( "Found multiple schema versions: %s", @{$schema_version} );
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,7 +54,6 @@ sub initialize_db {
|
|||
action_id smallint not null,
|
||||
station_id int references stations (id),
|
||||
action_time timestamptz not null,
|
||||
edited smallint not null,
|
||||
train_type varchar(16),
|
||||
train_line varchar(16),
|
||||
train_no varchar(16),
|
||||
|
@ -67,7 +74,7 @@ sub initialize_db {
|
|||
token varchar(80) not null,
|
||||
primary key (user_id, type)
|
||||
);
|
||||
insert into schema_version values (2);
|
||||
insert into schema_version values (0);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -108,23 +115,21 @@ my @migrations = (
|
|||
},
|
||||
);
|
||||
|
||||
sub run {
|
||||
my ( $self, $command ) = @_;
|
||||
my $exit_status = 0;
|
||||
|
||||
my $dbh = $self->app->dbh;
|
||||
|
||||
if ( $command eq 'setup' ) {
|
||||
sub setup_db {
|
||||
my ($dbh) = @_;
|
||||
$dbh->begin_work;
|
||||
if ( initialize_db($dbh) ) {
|
||||
$dbh->commit;
|
||||
}
|
||||
else {
|
||||
$dbh->rollback;
|
||||
$exit_status = 1;
|
||||
printf( "Database initialization was not successful: %s",
|
||||
$DBI::errstr );
|
||||
}
|
||||
}
|
||||
elsif ( $command eq 'migrate' ) {
|
||||
}
|
||||
|
||||
sub migrate_db {
|
||||
my ($dbh) = @_;
|
||||
$dbh->begin_work;
|
||||
my $schema_version = get_schema_version($dbh);
|
||||
say "Found travelynx schema v${schema_version}";
|
||||
|
@ -136,13 +141,29 @@ sub run {
|
|||
if ( not $migrations[$i]($dbh) ) {
|
||||
say "Aborting migration; rollback to v${schema_version}";
|
||||
$dbh->rollback;
|
||||
$exit_status = 1;
|
||||
last;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if ( get_schema_version($dbh) == @migrations ) {
|
||||
$dbh->commit;
|
||||
}
|
||||
}
|
||||
|
||||
sub run {
|
||||
my ( $self, $command ) = @_;
|
||||
|
||||
my $dbh = $self->app->dbh;
|
||||
|
||||
if ( not defined $dbh ) {
|
||||
printf( "Can't connect to the database: %s\n", $DBI::errstr );
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if ( $command eq 'migrate' ) {
|
||||
if ( not defined get_schema_version($dbh) ) {
|
||||
setup_db($dbh);
|
||||
}
|
||||
migrate_db($dbh);
|
||||
}
|
||||
elsif ( $command eq 'has-current-schema' ) {
|
||||
if ( get_schema_version($dbh) == @migrations ) {
|
||||
|
@ -150,7 +171,7 @@ sub run {
|
|||
}
|
||||
else {
|
||||
say "no";
|
||||
$exit_status = 1;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -158,8 +179,6 @@ sub run {
|
|||
}
|
||||
|
||||
$dbh->disconnect;
|
||||
|
||||
exit($exit_status);
|
||||
}
|
||||
|
||||
1;
|
||||
|
@ -168,12 +187,11 @@ __END__
|
|||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
Usage: index.pl database <setup|migrate|has-current-schema>
|
||||
Usage: index.pl database <migrate|has-current-schema>
|
||||
|
||||
Upgrades the database layout to the latest schema.
|
||||
|
||||
Recommended workflow:
|
||||
> systemctl stop travelynx
|
||||
> TRAVELYNX_DB_HOST=... TRAVELYNX_DB_NAME=... TRAVELYNX_DB_USER=... \
|
||||
TRAVELYNX_DB_PASSWORD=... perl index.pl migrate
|
||||
> perl index.pl migrate
|
||||
> systemctl start travelynx
|
||||
|
|
Loading…
Reference in a new issue