PhoenixEctoHeroku

HomePage | RecentChanges | Preferences

Phoenix and Ecto: from `mix new` to Heroku

--- layout: post title: "Phoenix and Ecto: From `mix new` to Heroku" date: 2015-07-12 14:49:00 tags: elixir phoenix ecto heroku ---

In [Deploying a Phoenix app to Heroku](...) we looked at the absolute minimum necessary to get the Hello Phoenix app running on Heroku. This involved modifying config files and adding some environment variables.

Now let's start over and deploy a more realistic app, one that actually uses a database, so we can make sure it really works.

      1. Step 1: Create a sample app and get it under version control

The first steps are the same, create a new Phoenix app and get it under version control:

$ mix phoenix.new my_app
$ cd my_app
$ git init && git add . && git commit -m "Initial commit of Phoenix app"

Now if anything goes wrong, we can back up to this point.

      1. Step 2: Choose a License

The next thing to do is to choose a license for your project. If your project has a chance of ever getting out in public, for example if you are going to put it on GitHub, it's important to include a license so that someone coming upon it later understands what your intentions are and what they are allowed to do with your code.

I'm a long time Apache person, and I prefer the Apache License. Many projects (and Phoenix itself) use the MIT License. GitHub has an excellent article on how to choose a license and why you should do so.

Enough of my soapbox. If you're going to, add your LICENSE file and commit it.

$ curl http://www.apache.org/licenses/LICENSE-2.0.txt > LICENSE
$ git add LICENSE && git commit -m "Add Apache License 2.0"

(If you'd like to automate adding the MIT license to your project, this looks like an interesting option: <https://github.com/remy/mit-license>; .)

      1. Step 3: Add a Users model

Let's add a simple model for a User by following part of the [Ecto Models](http://www.phoenixframework.org/docs/ecto-models) guide.

$ mix phoenix.gen.html User users name:string email:string access_token:string refresh_token:string
$ git add . && git commit -m "Add generated User model"

(You can omit the access and refresh tokens -- I'm working on an app that will use OAuth2 so I will need them.)

The output tells us to

Add the resource to your browser scope in web/router.ex:

    resources "/users", UserController

Open `web/router.ex` and paste that line in:

  scope "/", MyApp do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    resources "/users", UserController  # Added the resource
  end

(Remember to commit your changes. I'll stop reminding you after this.)

Now create the database and run the migration. (The [Ecto Models] guide has all the details on this.)

$ mix ecto.create
$ mix ecto.migrate

If you received errors here, check that Postgres is running locally.

At this point you can start the app locally with `mix phoenix.server` and play around with the <http://localhost:4000/users>; page, adding, editing and deleting users.

      1. Step 4: Modify configuration

The sample app generated by `mix phoenix.new` expects to import a prod.secret.exs file that we will not be using. Remove this section from config.prod.exs

-
-# Finally import the config/prod.secret.exs
-# which should be versioned separately.
-import_config "prod.secret.exs"

Instead, add the following to prod.exs:

# Configure secret_key_base for Cookie Session Storage
config :my_app, MyApp.Endpoint,
  secret_key_base: {system: "SECRET_KEY_BASE"}

# Configure your database
config :my_app, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: {system: "DATABASE_URL"},
  size: 20 # The amount of database connections in the pool

      1. Step 3: Create the Heroku application

$ heroku create
$ heroku buildpacks:add https://github.com/HashNuke/heroku-buildpack-elixir
$ heroku buildpacks:add https://github.com/gjaldon/phoenix-static-buildpack
$ heroku addons:create heroku-postgresql
$ heroku config:set SECRET_KEY_BASE=[long random string copied from config/prod.secret.exs]
$ heroku run mix ecto.create
$ heroku run mix ecto.migrate
$ git push heroku master

$ heroku run mix ecto.create Running `mix ecto.create` attached to terminal... up, run.4650

.

According to http://stackoverflow.com/questions/17300341/migrate-not-working-on-heroku this can be ignored

The first time I did this I noticed that MIX_ENV is getting set to 'prod' which is exactly correct... but how did that happen?

grr... after reverting a commit, this happens: http://stackoverflow.com/questions/6104258/how-to-reset-heroku-app-and-re-commit-everything nothing there works.


HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited July 11, 2015 12:48 pm by 198.98.94.133 (diff)
Search: