More Mojo::Pg migrations

This commit is contained in:
Daniel Friesel 2019-04-22 09:58:39 +02:00
parent eaf3f451c5
commit fb3fda9736
2 changed files with 143 additions and 229 deletions

View file

@ -148,89 +148,6 @@ sub startup {
} }
); );
$self->attr(
add_station_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{
insert into stations (ds100, name) values (?, ?)
}
);
}
);
$self->attr(
add_user_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{
insert into users (
name, status, public_level, email, token, password,
registered_at, last_login
) values (?, 0, 0, ?, ?, ?, to_timestamp(?), to_timestamp(?));
}
);
}
);
$self->attr(
set_email_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{
update users set email = ?, token = ? where id = ?;
}
);
}
);
$self->attr(
set_password_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{
update users set password = ? where id = ?;
}
);
}
);
$self->attr(
add_mail_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{
insert into pending_mails (
email, num_tries, last_try
) values (?, ?, to_timestamp(?));
}
);
}
);
$self->attr(
mark_for_deletion_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{
update users set deletion_requested = to_timestamp(?) where id = ?;
}
);
}
);
$self->attr(
get_stats_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{
select data from journey_stats
where user_id = ? and year = ? and month = ?
}
);
}
);
$self->attr( $self->attr(
dbh => sub { dbh => sub {
my ($self) = @_; my ($self) = @_;
@ -331,45 +248,6 @@ sub startup {
); );
} }
); );
$self->attr(
get_userid_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{select id from users where name = ?});
}
);
$self->attr(
get_pending_mails_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{select id from users where email = ? and status = 0;});
}
);
$self->attr(
get_listed_mails_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{select * from pending_mails where email = ? and num_tries > 1;}
);
}
);
$self->attr(
get_user_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{
select
id, name, status, public_level, email,
extract(epoch from registered_at), extract(epoch from last_login), extract(epoch from deletion_requested)
from users where id = ?
}
);
}
);
$self->attr( $self->attr(
get_api_tokens_query => sub { get_api_tokens_query => sub {
my ($self) = @_; my ($self) = @_;
@ -423,35 +301,6 @@ qq{select * from pending_mails where email = ? and num_tries > 1;}
); );
} }
); );
$self->attr(
get_password_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{
select
id, name, status, password
from users where name = ?
}
);
}
);
$self->attr(
get_stationid_by_ds100_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{select id from stations where ds100 = ?});
}
);
$self->attr(
get_stationid_by_name_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{select id from stations where name = ?});
}
);
$self->helper( $self->helper(
sendmail => sub { sendmail => sub {
@ -989,21 +838,27 @@ qq{select * from pending_mails where email = ? and num_tries > 1;}
'get_station_id' => sub { 'get_station_id' => sub {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;
$self->app->get_stationid_by_ds100_query->execute( $opt{ds100} ); my $res = $self->pg->db->select( 'stations', ['id'],
my $rows { ds100 => $opt{ds100} } );
= $self->app->get_stationid_by_ds100_query->fetchall_arrayref; my $res_h = $res->hash;
if ( @{$rows} ) {
return $rows->[0][0]; if ($res_h) {
} $res->finish;
else { return $res_h->{id};
$self->app->add_station_query->execute( $opt{ds100},
$opt{name} );
$self->app->get_stationid_by_ds100_query->execute(
$opt{ds100} );
my $rows
= $self->app->get_stationid_by_ds100_query->fetchall_arrayref;
return $rows->[0][0];
} }
$self->pg->db->insert(
'stations',
{
ds100 => $opt{ds100},
name => $opt{name},
}
);
$res = $self->pg->db->select( 'stations', ['id'],
{ ds100 => $opt{ds100} } );
my $id = $res->hash->{id};
$res->finish;
return $id;
} }
); );
@ -1032,28 +887,34 @@ qq{select * from pending_mails where email = ? and num_tries > 1;}
my ( $self, $uid ) = @_; my ( $self, $uid ) = @_;
$uid //= $self->current_user->{id}; $uid //= $self->current_user->{id};
my $query = $self->app->get_user_query;
$query->execute($uid); my $user_data = $self->pg->db->select(
my $rows = $query->fetchall_arrayref; 'users',
if ( @{$rows} ) { 'id, name, status, public_level, email, '
my @row = @{ $rows->[0] }; . 'extract(epoch from registered_at) as registered_at_ts, '
. 'extract(epoch from last_login) as last_login_ts, '
. 'extract(epoch from deletion_requested) as deletion_requested_ts',
{ id => $uid }
)->hash;
if ($user_data) {
return { return {
id => $row[0], id => $user_data->{id},
name => $row[1], name => $user_data->{name},
status => $row[2], status => $user_data->{status},
is_public => $row[3], is_public => $user_data->{public_level},
email => $row[4], email => $user_data->{email},
registered_at => DateTime->from_epoch( registered_at => DateTime->from_epoch(
epoch => $row[5], epoch => $user_data->{registered_at_ts},
time_zone => 'Europe/Berlin' time_zone => 'Europe/Berlin'
), ),
last_seen => DateTime->from_epoch( last_seen => DateTime->from_epoch(
epoch => $row[6], epoch => $user_data->{last_login_ts},
time_zone => 'Europe/Berlin' time_zone => 'Europe/Berlin'
), ),
deletion_requested => $row[7] deletion_requested => $user_data->{deletion_requested_ts}
? DateTime->from_epoch( ? DateTime->from_epoch(
epoch => $row[7], epoch => $user_data->{deletion_requested_ts},
time_zone => 'Europe/Berlin' time_zone => 'Europe/Berlin'
) )
: undef, : undef,
@ -1081,19 +942,14 @@ qq{select * from pending_mails where email = ? and num_tries > 1;}
$self->helper( $self->helper(
'get_user_password' => sub { 'get_user_password' => sub {
my ( $self, $name ) = @_; my ( $self, $name ) = @_;
my $query = $self->app->get_password_query;
$query->execute($name); my $res_h = $self->pg->db->select(
my $rows = $query->fetchall_arrayref; 'users',
if ( @{$rows} ) { 'id, name, status, password as password_hash',
my @row = @{ $rows->[0] }; { name => $name }
return { )->hash;
id => $row[0],
name => $row[1], return $res_h;
status => $row[2],
password_hash => $row[3],
};
}
return;
} }
); );
@ -1101,36 +957,68 @@ qq{select * from pending_mails where email = ? and num_tries > 1;}
'add_user' => sub { 'add_user' => sub {
my ( $self, $user_name, $email, $token, $password ) = @_; my ( $self, $user_name, $email, $token, $password ) = @_;
$self->app->get_userid_query->execute($user_name); my $now = DateTime->now( time_zone => 'Europe/Berlin' );
my $rows = $self->app->get_userid_query->fetchall_arrayref;
if ( @{$rows} ) { my $res = $self->pg->db->insert(
my $id = $rows->[0][0]; 'users',
{
name => $user_name,
status => 0,
public_level => 0,
email => $email,
token => $token,
password => $password,
registered_at => $now,
last_login => $now,
},
{ returning => 'id' }
);
# transition code for closed beta account -> normal account return $res->hash->{id};
if ($email) { }
$self->app->set_email_query->execute( $email, $token, $id ); );
$self->helper(
'flag_user_deletion' => sub {
my ( $self, $uid ) = @_;
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
$self->pg->db->update(
'users',
{ deletion_requested => $now },
{
id => $uid,
} }
if ($password) { );
$self->app->set_password_query->execute( $password, $id ); }
);
$self->helper(
'unflag_user_deletion' => sub {
my ( $self, $uid ) = @_;
$self->pg->db->update(
'users',
{
deletion_requested => undef,
},
{
id => $uid,
} }
return $id; );
}
else {
my $now = DateTime->now( time_zone => 'Europe/Berlin' )->epoch;
$self->app->add_user_query->execute( $user_name, $email, $token,
$password, $now, $now );
$self->app->get_userid_query->execute($user_name);
$rows = $self->app->get_userid_query->fetchall_arrayref;
return $rows->[0][0];
}
} }
); );
$self->helper( $self->helper(
'set_user_password' => sub { 'set_user_password' => sub {
my ( $self, $uid, $password ) = @_; my ( $self, $uid, $password ) = @_;
$self->app->set_password_query->execute( $password, $uid );
$self->pg->db->update(
'users',
{ password => $password },
{ id => $uid }
);
} }
); );
@ -1138,10 +1026,13 @@ qq{select * from pending_mails where email = ? and num_tries > 1;}
'check_if_user_name_exists' => sub { 'check_if_user_name_exists' => sub {
my ( $self, $user_name ) = @_; my ( $self, $user_name ) = @_;
$self->app->get_userid_query->execute($user_name); my $count = $self->pg->db->select(
my $rows = $self->app->get_userid_query->fetchall_arrayref; 'users',
'count(*) as count',
{ name => $user_name }
)->hash->{count};
if ( @{$rows} ) { if ($count) {
return 1; return 1;
} }
return 0; return 0;
@ -1152,12 +1043,29 @@ qq{select * from pending_mails where email = ? and num_tries > 1;}
'check_if_mail_is_blacklisted' => sub { 'check_if_mail_is_blacklisted' => sub {
my ( $self, $mail ) = @_; my ( $self, $mail ) = @_;
$self->app->get_pending_mails_query->execute($mail); my $count = $self->pg->db->select(
if ( @{ $self->app->get_pending_mails_query->fetchall_arrayref } ) { 'users',
'count(*) as count',
{
email => $mail,
status => 0,
}
)->hash->{count};
if ($count) {
return 1; return 1;
} }
$self->app->get_listed_mails_query->execute($mail);
if ( @{ $self->app->get_listed_mails_query->fetchall_arrayref } ) { $count = $self->pg->db->select(
'pending_mails',
'count(*) as count',
{
email => $mail,
num_tries => { '>', 1 },
}
)->hash->{count};
if ($count) {
return 1; return 1;
} }
return 0; return 0;
@ -1236,11 +1144,21 @@ qq{select * from pending_mails where email = ? and num_tries > 1;}
# -> Cache entries must be explicitly invalidated whenever the user # -> Cache entries must be explicitly invalidated whenever the user
# checks out of a train or manually edits/adds a journey. # checks out of a train or manually edits/adds a journey.
$self->app->get_stats_query->execute( $uid, $year, $month ); my $res = $self->pg->db->select(
my $rows = $self->app->get_stats_query->fetchall_arrayref; 'journey_stats',
['data'],
{
user_id => $uid,
year => $year,
month => $month
}
);
if ( @{$rows} == 1 ) { my $res_h = $res->expand->hash;
return JSON->new->decode( $rows->[0][0] );
if ($res_h) {
$res->finish;
return $res_h->{data};
} }
my $interval_start = DateTime->new( my $interval_start = DateTime->new(

View file

@ -182,8 +182,6 @@ sub delete {
return; return;
} }
my $now = DateTime->now( time_zone => 'Europe/Berlin' )->epoch;
if ( $self->param('action') eq 'delete' ) { if ( $self->param('action') eq 'delete' ) {
if ( if (
not $self->authenticate( not $self->authenticate(
@ -195,12 +193,10 @@ sub delete {
$self->render( 'account', invalid => 'password' ); $self->render( 'account', invalid => 'password' );
return; return;
} }
$self->app->mark_for_deletion_query->execute( $now, $self->flag_user_deletion( $self->current_user->{id} );
$self->current_user->{id} );
} }
else { else {
$self->app->mark_for_deletion_query->execute( undef, $self->unflag_user_deletion( $self->current_user->{id} );
$self->current_user->{id} );
} }
$self->redirect_to('account'); $self->redirect_to('account');
} }