Skip to content

Instantly share code, notes, and snippets.

@Narven
Forked from stevedomin/create_post.exs
Created October 12, 2021 06:24
Show Gist options
  • Save Narven/eedffd4e84a88d02d1ce378563f97990 to your computer and use it in GitHub Desktop.
Save Narven/eedffd4e84a88d02d1ce378563f97990 to your computer and use it in GitHub Desktop.

Revisions

  1. @stevedomin stevedomin revised this gist Mar 1, 2016. 3 changed files with 14 additions and 24 deletions.
    13 changes: 13 additions & 0 deletions create_post.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    defmodule MyBlog.Repo.Migrations.CreatePost do
    use Ecto.Migration

    def change do
    create table(:posts, primary_key: false) do
    add :id, :uuid, primary_key: true
    add :body, :string
    add :word_count, :integer

    timestamps
    end
    end
    end
    23 changes: 0 additions & 23 deletions ecto_table_with_uuid_pkey.exs
    Original file line number Diff line number Diff line change
    @@ -1,23 +0,0 @@
    defmodule MyBlog.Repo.Migrations.CreatePost do
    use Ecto.Migration

    def up do
    # This should go in a separate migration if you're using UUIDs across your app
    execute "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""

    create table(:posts, primary_key: false) do
    add :id, :uuid, primary_key: true, default: fragment("uuid_generate_v1()")
    add :body, :string
    add :word_count, :integer

    timestamps
    end
    end


    def down do
    execute "DROP EXTENSION IF EXISTS \"uuid-ossp\""

    drop table(:posts)
    end
    end
    2 changes: 1 addition & 1 deletion post.ex
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    defmodule MyBlog.Post do
    use MyBlog.Web, :model

    @primary_key {:id, Ecto.UUID, [read_after_writes: true]}
    @primary_key {:id, :binary_id, autogenerate: true}

    schema "posts" do
    field :body, :string
  2. @stevedomin stevedomin renamed this gist May 7, 2015. 1 changed file with 0 additions and 0 deletions.
  3. @stevedomin stevedomin renamed this gist May 7, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @stevedomin stevedomin created this gist May 7, 2015.
    23 changes: 23 additions & 0 deletions create_post.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    defmodule MyBlog.Repo.Migrations.CreatePost do
    use Ecto.Migration

    def up do
    # This should go in a separate migration if you're using UUIDs across your app
    execute "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""

    create table(:posts, primary_key: false) do
    add :id, :uuid, primary_key: true, default: fragment("uuid_generate_v1()")
    add :body, :string
    add :word_count, :integer

    timestamps
    end
    end


    def down do
    execute "DROP EXTENSION IF EXISTS \"uuid-ossp\""

    drop table(:posts)
    end
    end
    26 changes: 26 additions & 0 deletions post.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    defmodule MyBlog.Post do
    use MyBlog.Web, :model

    @primary_key {:id, Ecto.UUID, [read_after_writes: true]}

    schema "posts" do
    field :body, :string
    field :word_count, :integer

    timestamps
    end

    @required_fields ~w(body word_count)
    @optional_fields ~w()

    @doc """
    Creates a changeset based on the `model` and `params`.
    If `params` are nil, an invalid changeset is returned
    with no validation performed.
    """
    def changeset(model, params \\ nil) do
    model
    |> cast(params, @required_fields, @optional_fields)
    end
    end