Skip to content

Instantly share code, notes, and snippets.

View talentdeficit's full-sized avatar

alisdair sullivan talentdeficit

View GitHub Profile
@talentdeficit
talentdeficit / numeric.ex
Last active January 26, 2017 17:08
optional Decimal
defmodule Postgrex.Extensions.Numeric do
@moduledoc false
import Postgrex.BinaryUtils, warn: false
use Postgrex.BinaryExtension, send: "numeric_send"
def init(opts) do
Keyword.get(opts, :numeric, Postgrex.Extensions.Numeric.Decimal)
end
def encode(handler) do
@talentdeficit
talentdeficit / :posterize_xt_date.ex
Last active January 8, 2017 22:03
date with infinity
defmodule :posterize_xt_date do
@moduledoc false
@behaviour Postgrex.Extension
import Postgrex.BinaryUtils, warn: false
use Postgrex.BinaryExtension, [send: "date_send"]
@pg_epoch :calendar.date_to_gregorian_days({ 2000, 1, 1 })
@max_year 5874897
@min_year -4713
@talentdeficit
talentdeficit / posterize_xt_timestamp.ex
Created January 8, 2017 21:59
timestamps with infinity
defmodule :posterize_xt_timestamp do
@moduledoc false
@behaviour Postgrex.Extension
import Postgrex.BinaryUtils, warn: false
use Postgrex.BinaryExtension, [send: "timestamp_send"]
@unix_epoch :calendar.datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}})
@gs_epoch :calendar.datetime_to_gregorian_seconds({{2000, 1, 1}, {0, 0, 0}})
@adjustment :erlang.convert_time_unit(@gs_epoch - @unix_epoch, :seconds, :microsecond)
@talentdeficit
talentdeficit / json_config.ex
Created January 5, 2017 03:50
using a json blob as application config
def reload_config(path_to_config) do
config = File.read!(path_to_config) |> Poison.decode!
Application.put_env(AppA, KeyA, config["key_a"])
Application.put_env(AppB, KeyB, config["key_b"])
Application.stop(AppA)
Application.stop(AppB)
Application.start(AppA)
Application.start(AppB)
@talentdeficit
talentdeficit / leapsecond.sql
Created January 1, 2017 00:58
postgres sucks
talentdeficit=# SELECT '2016-12-31T23:59:60Z'::timestamp;
timestamp
---------------------
2017-01-01 00:00:00
(1 row)
postgres=# SELECT TIMESTAMP 'January 1, 99 AD' > TIMESTAMP 'January 1, 100 AD'
?column?
----------
t
(1 row)
postgres=# SELECT TIMESTAMP 'January 1, 2001 AD' = TIMESTAMP 'January 1, 1 AD' ;
?column?
----------
t
defmodule :posterize_xt_time do
@moduledoc false
@behaviour Postgrex.Extension
import Postgrex.BinaryUtils, warn: false
def init(opts) do
case Keyword.get(opts, :units) do
nil -> :native
units when is_atom(units) -> units
end
<%= for {name, info} <- context[:shapes] do %>shape(<%= name %>) ->
<%= if info[:type] == <<"list">> %>
ok;
<% else %>
ok;
<% end %>
<% end %>shape(_) -> erlang:error(badarg).
iex(5)> dates = [DateTime.from_unix!(100000000), DateTime.from_unix!(100000)] [%DateTime{calendar: Calendar.ISO, day: 3, hour: 9, microsecond: {0, 0},
minute: 46, month: 3, second: 40, std_offset: 0, time_zone: "Etc/UTC",
utc_offset: 0, year: 1973, zone_abbr: "UTC"},
%DateTime{calendar: Calendar.ISO, day: 2, hour: 3, microsecond: {0, 0},
minute: 46, month: 1, second: 40, std_offset: 0, time_zone: "Etc/UTC",
utc_offset: 0, year: 1970, zone_abbr: "UTC"}]
iex(6)> Enum.sort(dates)
[%DateTime{calendar: Calendar.ISO, day: 2, hour: 3, microsecond: {0, 0},
minute: 46, month: 1, second: 40, std_offset: 0, time_zone: "Etc/UTC",
utc_offset: 0, year: 1970, zone_abbr: "UTC"},
# :for defaults to self()
{ :ok, ref } = Postgrex.aquery(conn, "SELECT user_id, text FROM comments", [{ :for, pid }])
# some gen_server (@pid)
handle_info({ :postgrex_reply, ref, { :ok, %Postgrex.Result{..} } }, state) ->
...
end
handle_info({ :postgrex_reply, ref, { :error, %Postgrex.Error{..} } }, state) ->
...
end