Created
August 10, 2018 16:58
-
-
Save renatocron/04c4f693c26153640ec203ec6209cac8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create schema if not exists utils; | |
create table utils.replaceable_now ( the_time timestamp with time zone ); | |
CREATE OR REPLACE FUNCTION replaceable_now() RETURNS timestamp with time zone AS $AA$ | |
SELECT coalesce((select the_time from utils.replaceable_now limit 1), now()); | |
$AA$ LANGUAGE SQL STABLE; | |
/* | |
-- run in prod: | |
drop table utils.replaceable_now; | |
CREATE OR REPLACE FUNCTION replaceable_now() RETURNS timestamp with time zone AS $AA$ | |
SELECT now() | |
$AA$ LANGUAGE SQL STABLE; | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub set_relative_time { | |
my ( $date, %conf ) = @_; | |
my $schema = SFC->model( exists $conf{model} ? $conf{model} : 'DB' ); | |
$schema->storage->dbh_do( | |
sub { | |
shift; | |
my $cn = shift; | |
my $res = $cn->do(qq{update utils.replaceable_now set the_time = '$date'::timestamp with time zone }); | |
if ( $res eq '0E0' ) { | |
$cn->do(qq{insert into utils.replaceable_now ( the_time) values ( '$date'::timestamp with time zone )}); | |
} | |
} | |
); | |
} | |
sub epoch_to_datetime { | |
my $epoch = shift; | |
return DateTime->from_epoch( epoch => $epoch )->datetime; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment