Quantcast
Viewing all articles
Browse latest Browse all 38

Phoenix Framework Database Migrations

Here's a couple of useful snippets for running database migrations.


How to create a database migration:

mix ecto.gen.migration create_user
// creates .../migrations/20151216023702_create_user.exs

Then inside of this file you write what you want created:

defmodule Example.Repo.Migrations.CreateUser do use Ecto.Migration  
  def change do
    create table(:users) do
      add :name, :string
      add :username, :string, null: false add :password_hash, :string

      timestamps
    end

    create unique_index(:users, [:username]) 
  end
end  

Finally you can migrate with:

mix ecto.migrate  

This is very similar to Rails. I'll be adding more useful Phoenix tidbits here and there as I come across them.


Viewing all articles
Browse latest Browse all 38

Trending Articles