Creating a new migration
Given our newfound understanding of our complex object, we need to create our database table. Now, there are a few ways to do this. For example, we can use Phoenix and Ecto's built-in generators to give us a skeleton for this, but since we're trying to learn and understand the underlying systems that are needed for our application, we’ll start by NOT using generators to build our initial skeletons; later on, we’ll dive a little bit into using the generators and how to use them.
We'll get into a habit of learning what we can from the various help commands that exist in mix and IEx, so let's do the same. We can search for any of the Ecto commands that exist for mix with the following:
$ mix help | grep ecto
mix ecto # Prints Ecto help information
mix ecto.create # Creates the repository storage
mix ecto.drop # Drops the repository storage
mix ecto.dump # Dumps the repository database structure
mix ecto.gen.migration # Generates a new migration for the repo
mix ecto.gen.repo # Generates a new repository
mix ecto.load # Loads previously dumped database structure
mix ecto.migrate # Runs the repository migrations
mix ecto.migrations # Displays the repository migration status
mix ecto.rollback # Rolls back the repository migrations
mix phx.new.ecto # Creates a new Ecto project within an umbrella project
Typically any generator will have a .gen as part of the command itself. We've already run mix ecto.create to create our database, and we don't need more help information, nor do we want to drop or dump anything from our database. We don't need to load, and we have nothing to migrate yet, so that also crosses mix ecto.migrate and mix ecto.migrations off our list. Finally, we see mix phx.new.ecto, which talks about creating a new Ecto project within an umbrella project, which also doesn't fit into what we're trying to accomplish here, so that leaves us with our two generator commands.
mix ecto.gen.repo creates a new repository for our application, which we don't need, so we're going to work with mix ecto.gen.migration to create a migration for our application! One of my favorite parts of working with Elixir and Phoenix is how entirely fantastic the documentation that is built-in to almost every single command is, so let's take a look at the documentation for the migration generator and learn how to use it. To get help for anything in mix remember that we can prefix help to any of the commands; let's run mix help ecto.gen.migration:
$ mix help ecto.gen.migration
mix ecto.gen.migration
Generates a migration.
The repository must be set under :ecto_repos in the current app configuration
or given via the -r option.
## Examples
mix ecto.gen.migration add_posts_table
mix ecto.gen.migration add_posts_table -r Custom.Repo
The generated migration filename will be prefixed with the current timestamp in
UTC which is used for versioning and ordering.
By default, the migration will be generated to the "priv/YOUR_REPO/migrations"
directory of the current application but it can be configured to be any
subdirectory of priv by specifying the :priv key under the repository
configuration.
This generator will automatically open the generated file if you have
ECTO_EDITOR set in your environment variable.
## Command line options
? -r, --repo - the repo to generate migration for
Location: _build/dev/lib/ecto/ebin
Okay, this is a pretty simple command! Based on the help documentation, we can see that what we want to do is create a migration that creates a new table, which we’ve referred to previously as Polls.