diff options
author | Daniel Friesel <derf@finalrewind.org> | 2019-04-07 16:55:35 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2019-04-07 16:55:35 +0200 |
commit | d0b6b7e0523cbee6c5902b53e993cfecc7cb5843 (patch) | |
tree | 27a2d6d7f118b81757d2f9a7dc39cee6bc2971b3 /lib/Travelynx/Command/database.pm | |
parent | 9225a82c669079f09b29f68296f273630f274ec9 (diff) |
Cache journey stats
Diffstat (limited to 'lib/Travelynx/Command/database.pm')
-rw-r--r-- | lib/Travelynx/Command/database.pm | 29 |
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 { |