summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Command
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2019-05-26 17:28:21 +0200
committerDaniel Friesel <derf@finalrewind.org>2019-05-26 17:28:21 +0200
commit7fe95532c1bc559a9ad2c4b96ec37bd0e30f8598 (patch)
treec78cb5abf2833c107ac018ac98886e619231b450 /lib/Travelynx/Command
parent45a4089431002ee01b11880d463dd3513b0657ca (diff)
Use JSON for messages and route storage, prepare for extended route data
Diffstat (limited to 'lib/Travelynx/Command')
-rw-r--r--lib/Travelynx/Command/database.pm113
-rw-r--r--lib/Travelynx/Command/work.pm34
2 files changed, 131 insertions, 16 deletions
diff --git a/lib/Travelynx/Command/database.pm b/lib/Travelynx/Command/database.pm
index 99fcc61..fab5fa7 100644
--- a/lib/Travelynx/Command/database.pm
+++ b/lib/Travelynx/Command/database.pm
@@ -546,6 +546,119 @@ my @migrations = (
}
);
},
+
+ # v13 -> v14
+ sub {
+ my ($db) = @_;
+ $db->query(
+ qq{
+ alter table journeys add column route_new jsonb,
+ add column messages_new jsonb;
+ alter table in_transit add column route_new jsonb,
+ add column messages_new jsonb;
+ }
+ );
+ my $res = $db->select( 'journeys', [ 'id', 'messages', 'route' ] );
+ my $json = JSON->new;
+
+ for my $journey ( $res->hashes->each ) {
+ my $id = $journey->{id};
+ my @messages;
+ for my $message ( split( qr{[|]}, $journey->{messages} // '' ) ) {
+ my ( $ts, $msg ) = split( qr{:}, $message );
+ push( @messages, [ $ts, $msg ] );
+ }
+ my @route = map { [$_] }
+ split( qr{[|]}, $journey->{route} // '' );
+
+ $db->update(
+ 'journeys',
+ {
+ messages_new => $json->encode( [@messages] ),
+ route_new => $json->encode( [@route] ),
+ },
+ { id => $id }
+ );
+ }
+
+ $res = $db->select( 'in_transit', [ 'user_id', 'messages', 'route' ] );
+ for my $journey ( $res->hashes->each ) {
+ my $id = $journey->{user_id};
+ my @messages;
+ for my $message ( split( qr{[|]}, $journey->{messages} // '' ) ) {
+ my ( $ts, $msg ) = split( qr{:}, $message );
+ push( @messages, [ $ts, $msg ] );
+ }
+ my @route = map { [$_] }
+ split( qr{[|]}, $journey->{route} // '' );
+
+ $db->update(
+ 'in_transit',
+ {
+ messages_new => $json->encode( [@messages] ),
+ route_new => $json->encode( [@route] ),
+ },
+ { user_id => $id }
+ );
+ }
+
+ $db->query(
+ qq{
+ drop view journeys_str;
+ alter table journeys drop column messages;
+ alter table journeys drop column route;
+ alter table journeys rename column messages_new to messages;
+ alter table journeys rename column route_new to route;
+
+ drop view in_transit_str;
+ alter table in_transit drop column messages;
+ alter table in_transit drop column route;
+ alter table in_transit rename column messages_new to messages;
+ alter table in_transit rename column route_new to route;
+
+ create view journeys_str as select
+ journeys.id as journey_id, user_id,
+ train_type, train_line, train_no, train_id,
+ extract(epoch from checkin_time) as checkin_ts,
+ extract(epoch from sched_departure) as sched_dep_ts,
+ extract(epoch from real_departure) as real_dep_ts,
+ dep_stations.ds100 as dep_ds100,
+ dep_stations.name as dep_name,
+ extract(epoch from checkout_time) as checkout_ts,
+ extract(epoch from sched_arrival) as sched_arr_ts,
+ extract(epoch from real_arrival) as real_arr_ts,
+ arr_stations.ds100 as arr_ds100,
+ arr_stations.name as arr_name,
+ cancelled, edited, route, messages,
+ dep_platform, arr_platform
+ from journeys
+ join stations as dep_stations on dep_stations.id = checkin_station_id
+ join stations as arr_stations on arr_stations.id = checkout_station_id
+ ;
+ create view in_transit_str as select
+ user_id,
+ train_type, train_line, train_no, train_id,
+ extract(epoch from checkin_time) as checkin_ts,
+ extract(epoch from sched_departure) as sched_dep_ts,
+ extract(epoch from real_departure) as real_dep_ts,
+ dep_stations.ds100 as dep_ds100,
+ dep_stations.name as dep_name,
+ extract(epoch from checkout_time) as checkout_ts,
+ extract(epoch from sched_arrival) as sched_arr_ts,
+ extract(epoch from real_arrival) as real_arr_ts,
+ arr_stations.ds100 as arr_ds100,
+ arr_stations.name as arr_name,
+ cancelled, route, messages,
+ dep_platform, arr_platform
+ from in_transit
+ join stations as dep_stations on dep_stations.id = checkin_station_id
+ left join stations as arr_stations on arr_stations.id = checkout_station_id
+ ;
+
+ update schema_version set version = 14;
+ }
+ );
+ },
);
sub setup_db {
diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm
index b24e4ad..46f2213 100644
--- a/lib/Travelynx/Command/work.pm
+++ b/lib/Travelynx/Command/work.pm
@@ -2,6 +2,7 @@ package Travelynx::Command::work;
use Mojo::Base 'Mojolicious::Command';
use DateTime;
+use JSON;
use List::Util qw(first);
has description =>
@@ -13,6 +14,7 @@ sub run {
my ($self) = @_;
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
+ my $json = JSON->new;
my $db = $self->app->pg->db;
@@ -47,14 +49,14 @@ sub run {
{
dep_platform => $train->platform,
real_departure => $train->departure,
- route => join( '|', $train->route ),
- messages => join(
- '|',
- map {
- ( $_->[0] ? $_->[0]->epoch : q{} ) . ':'
- . $_->[1]
- } $train->messages
- )
+ route =>
+ $json->encode( [ map { [$_] } $train->route ] ),
+ messages => $json->encode(
+ [
+ map { [ $_->[0]->epoch, $_->[1] ] }
+ $train->messages
+ ]
+ ),
},
{ user_id => $uid }
);
@@ -99,14 +101,14 @@ sub run {
arr_platform => $train->platform,
sched_arrival => $train->sched_arrival,
real_arrival => $train->arrival,
- route => join( '|', $train->route ),
- messages => join(
- '|',
- map {
- ( $_->[0] ? $_->[0]->epoch : q{} ) . ':'
- . $_->[1]
- } $train->messages
- )
+ route =>
+ $json->encode( [ map { [$_] } $train->route ] ),
+ messages => $json->encode(
+ [
+ map { [ $_->[0]->epoch, $_->[1] ] }
+ $train->messages
+ ]
+ ),
},
{ user_id => $uid }
);