Created
August 21, 2015 12:48
-
-
Save seriyps/a4c42766734b01a7aa02 to your computer and use it in GitHub Desktop.
Check if epgsql database connections are on the same timezone
This file contains hidden or 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
% If we have some amount of PostgreSQL instances and want to transfer data between them, | |
% in case when model has 'timeatamp' fields, we should check that both instances configured with the | |
% same timezone | |
on_same_timezone(Connections) -> | |
Tests = | |
[%% Check using TimeZone connection parameter | |
{fun(Conn) -> | |
{ok, TZ} = pgsql:get_parameter(Conn, <<"TimeZone">>), | |
TZ | |
end, | |
fun(A, B) -> | |
FixTZ = fun (<<"GMT">>) -> <<"UTC">>; | |
(TZ) -> TZ | |
end, | |
((A =/= undefined) | |
and (B =/= undefined) | |
and (FixTZ(A) == FixTZ(B))) | |
end}, | |
%% Check using now() call | |
{fun(Conn) -> | |
{ok, _, [{DT}]} = pgsql:equery(Conn, <<"SELECT now()::timestamp">>), | |
calendar:datetime_to_gregorian_seconds(db_dtime_fix_seconds(DT)) | |
end, | |
fun(A, B) -> | |
%% time differs not more than 60 sec | |
(max(A, B) - min(A, B)) < 60 | |
end}], | |
%% If any test succeed - return true | |
lists:any(fun({Extract, Compare}) -> | |
%% compare each value with first one | |
[V1 | Rest] = [Extract(C) || C <- Connections], | |
lists:all(fun(V2) -> | |
Res = Compare(V1, V2), | |
lager:debug("TimezoneCmp(~p, ~p) -> ~p", [V1, V2, Res]), | |
Res | |
end, Rest) | |
end, Tests). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment