Skip to content

Instantly share code, notes, and snippets.

@angelikatyborska
Created January 28, 2019 16:00
Show Gist options
  • Save angelikatyborska/a68ab88a3dd838d2cfe2feca1ffd545f to your computer and use it in GitHub Desktop.
Save angelikatyborska/a68ab88a3dd838d2cfe2feca1ffd545f to your computer and use it in GitHub Desktop.
UUIDs with Ecto
defmodule MyApp.Repo.Migrations.AddUuidOssp do
use Ecto.Migration
def up do
execute("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\" WITH SCHEMA public;")
end
def down do
execute("DROP EXTENSION \"uuid-ossp\";")
end
end
defmodule MyApp.Repo.Migrations.AddModel do
use Ecto.Migration
def change do
create table(:model, primary_key: false) do
add(:id, :uuid, primary_key: true, default: fragment("uuid_generate_v4()"))
timestamps()
end
end
end
defmodule MyApp.Model do
use MyApp, :model
schema "model" do
timestamps()
end
end
defmodule MyApp do
def model do
quote do
use Ecto.Schema
import Ecto
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: false, read_after_writes: true}
@foreign_key_type :binary_id
end
end
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment