Skip to content

Instantly share code, notes, and snippets.

View Ivor's full-sized avatar

Ivor Paul Ivor

View GitHub Profile
@Ivor
Ivor / deepmap.ex
Last active July 21, 2020 11:51
Quick and dirty way to get a struct to a map.
defmodule DeepMap do
def deep_to_map(%DateTime{} = struct), do: DateTime.to_iso8601(struct)
def deep_to_map(%Date{} = struct), do: Date.to_iso8601(struct)
def deep_to_map(%NaiveDateTime{} = struct), do: NaiveDateTime.to_iso8601(struct)
def deep_to_map(%_{} = struct) do
struct
|> Map.from_struct()
|> deep_to_map()
end
@Ivor
Ivor / 20160922000000_branches_street_address_unique_constraint.exs
Last active September 22, 2016 19:48
Ecto migration with unique_index on multiple fields.
defmodule VetMapper.Repo.Migrations.BranchesStreetAddressUniqueConstraint do
use Ecto.Migration
def up do
create unique_index(:branches, [:country, :state, :city, :street_name, :street_number], name: :index_branches_on_address)
end
def down do
drop index(:branches, [:country, :state, :city, :street_name, :street_number], name: :index_branches_on_address)
end
@Ivor
Ivor / branch.ex
Last active September 22, 2016 19:48
Ecto: How to validate unique combinations of two fields.
defmodule VetMapper.Branch do
use VetMapper.Web, :model
schema "branches" do
field :name, :string
field :country, :string
field :state, :string
field :city, :string
field :street_name, :string
field :street_number, :integer

Keybase proof

I hereby claim:

  • I am ivor on github.
  • I am apie (https://keybase.io/apie) on keybase.
  • I have a public key whose fingerprint is B7FA B217 D013 B7FA 2522 717E 002B A714 C868 DAB5

To claim this, I am signing this object:

@Ivor
Ivor / gist:7819044
Created December 6, 2013 05:32
Jan F.E. Cilliers.
Stil, broers,
daar gaan 'n man verby,
hy groet,
en dis verlaas.
Daar's nog maar één soos hy;
bekyk hom goed.
Die oog,
nou dof en weggesink,
soos vuurvonk kon hy blink -
@Ivor
Ivor / Postgress Reindex with ActiveRecord
Last active December 14, 2015 06:59
PG duplicate key issue after importing pg dump.
#If you get this issue when trying to save to a recently imported postgres db.
#PG::Error: ERROR: duplicate key value violates unique constraint
#Run the following in your rails console.
ActiveRecord::Base.connection.tables.each do |t|
ActiveRecord::Base.connection.reset_pk_sequence!(t)
end;nil
@Ivor
Ivor / Fixing missing hstore extension for postgres and rails.
Created February 16, 2013 08:43
I kept running into "PG::Error: ERROR: type "hstore" does not exist" because we had neglected to add hstore to our database and my migrations would constantly fail. Here are the steps to fix this problem:
HSTORE PROBLEMS:
If the specs are not running because you are running into hstore issues:
1. In the postgresql console:
update pg_database set datallowconn = TRUE where datname = 'template0';
This allows you to edit your postgresql default database template.
2. In your shell: