From d0b6b7e0523cbee6c5902b53e993cfecc7cb5843 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 7 Apr 2019 16:55:35 +0200 Subject: Cache journey stats --- lib/Travelynx/Command/database.pm | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'lib/Travelynx/Command/database.pm') 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 { -- cgit v1.2.3