summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Command
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2019-04-07 16:55:35 +0200
committerDaniel Friesel <derf@finalrewind.org>2019-04-07 16:55:35 +0200
commitd0b6b7e0523cbee6c5902b53e993cfecc7cb5843 (patch)
tree27a2d6d7f118b81757d2f9a7dc39cee6bc2971b3 /lib/Travelynx/Command
parent9225a82c669079f09b29f68296f273630f274ec9 (diff)
Cache journey stats
Diffstat (limited to 'lib/Travelynx/Command')
-rw-r--r--lib/Travelynx/Command/database.pm29
1 files changed, 25 insertions, 4 deletions
diff --git a/lib/Travelynx/Command/database.pm b/lib/Travelynx/Command/database.pm
index 7a714b1..4175d58 100644
--- a/lib/Travelynx/Command/database.pm
+++ b/lib/Travelynx/Command/database.pm
@@ -71,7 +71,28 @@ sub initialize_db {
);
}
-my @migrations = ();
+my @migrations = (
+
+ # v0 -> v1
+ sub {
+ my ($dbh) = @_;
+ return $dbh->do(
+ qq{
+ alter table user_actions
+ add column edited smallint;
+ drop table if exists monthly_stats;
+ create table journey_stats (
+ user_id integer not null references users (id),
+ year smallint not null,
+ month smallint not null,
+ data jsonb not null,
+ primary key (user_id, year, month)
+ );
+ update schema_version set version = 1;
+ }
+ );
+ },
+);
sub run {
my ( $self, $command ) = @_;
@@ -96,18 +117,18 @@ sub run {
}
for my $i ( $schema_version .. $#migrations ) {
printf( "Updating to v%d ...\n", $i + 1 );
- if ( not $migrations[$i]() ) {
+ if ( not $migrations[$i]($dbh) ) {
say "Aborting migration; rollback to v${schema_version}";
$dbh->rollback;
last;
}
}
- if ( get_schema_version($dbh) == $#migrations ) {
+ if ( get_schema_version($dbh) == @migrations ) {
$dbh->commit;
}
}
elsif ( $command eq 'has-current-schema' ) {
- if ( get_schema_version($dbh) == $#migrations ) {
+ if ( get_schema_version($dbh) == @migrations ) {
say "yes";
}
else {