Creating our user schema

We're pretty comfortable with how our design for our user schema currently looks, so let's start working with this a little bit more and actually create the initial migration for our application! As per Chapter 3Storing and Retrieving Vote Data with Ecto Pages, we'll be working through all of this manually to make sure we get the design and creation of everything set exactly as we're expecting; sometimes working with generators will give you a lot of extra cruft and code that you may not necessarily want as part of the baseline for your application. Let's call our first migration create_users_table and run the Ecto migration generator mix command:

$ mix ecto.gen.migration create_users_table
* creating priv/repo/migrations
* creating priv/repo/migrations/20171017173615_create_users_table.exs

This will give us our default blank starting point for our migration:

defmodule Vocial.Repo.Migrations.CreateUsersTable do
use Ecto.Migration

def change do

end
end

Remember that id will always be created by default with any table creation and that we should include timestamps via the built-in timestamps() helper, leaving us needing to create username, email, and active. We should also set active to have a default value of true, since we don't want to be accidentally creating everyone's user account as inactive by default. Finally, we'll want to store the encrypted_password for the user (although we'll dive into how to implement that later).

That's actually not to say you couldn't set the default to false. You may have a scenario where you don't want user accounts to be active until they finish some portion of the registration process. For example, maybe they need to click a link to verify their supplied email address before they're made a fullyactive account!

This should leave us with a migration file that looks something like this:

defmodule Vocial.Repo.Migrations.CreateUsersTable do
use Ecto.Migration

def change do
create table(:users) do
add :username, :string
add :email, :string
add :active, :boolean, default: true
add :encrypted_password, :string

timestamps()
end
end
end

If we're satisfied with the results, we can start the process of actually running our migration. Returning to our terminal, we'll run the following command:

$ mix ecto.migrate
[info] == Running Vocial.Repo.Migrations.CreateUsersTable.change/0 forward
[info] create table users
[info] == Migrated in 0.1s

Success! We now have the users table created in our application. We can verify this through any database application that allows you to view the structure of your tables, such as Psequel for OS X:

Great, it's all there! We can now move on to creating the actual schema file in our application! Remember that, by default, we'll be starting with no file or anything, as we're doing everything ourselves instead of using generators to accomplish those tasks, so let's dive into things! We'll start off by briefly mentioning that we'll be creating an accounts context for our user schema, since this directly informs the directory and file structure that we'll need to create for our application. Under lib/vocial, create an accounts directory that just has one file in it: user.ex. We'll start off with a baseline for our user schema, similar to both the migration and previous schemas that we have created for polls and votes:

defmodule Vocial.Accounts.User do
use Ecto.Schema
import Ecto.Changeset

alias Vocial.Accounts.User

schema "users" do
field :username, :string
field :email, :string
field :active, :boolean, default: true
field :encrypted_password, :string

timestamps()
end

def changeset(%User{}=user, attrs) do
user
|> cast(attrs, [:username, :email, :active])
|> validate_required([:username, :email, :active])
end
end

Okay, we have a good baseline and have introduced the User Schema into our application. We've set it up with the initial columns that we designed and handled our standard imports, aliases, and macros to make this all fit nicely into Ecto's world. Let's move on to our context so we can start making sense of our schema!

We haven't added encrypted_password to our list of fields in either cast or validate_required yet on purpose! We're going to be dealing with this column specifically later on when we deal with password encryption!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset