Migrate from Rails to Phoenix

ivmexx profile image Markus Chmelar ・3 min read

Say you have a running Rails app and want to migrate the app to Phoenix for whatever reason.

I'll be honest - in my case, it's just because I'm curious about Phoenix and want to learn more about it.

The Rails app in question is a relatively simple JSON API backend for iOS- and Android mobile apps, as well as a Javascript Web Client.

I want to be able to run both servers in parallel to compare both versions and to eventually switch over without interruption. For these reasons, I want to keep using the existing Postgres database.

In this post, I'm describing the steps that are necessary to get started. I'll assume you are already familiar with Phoenix and will only cover the special steps necessary to use the existing Postgres database from a Rails app.

Dump Schema

In order to load your existing database schema from Rails in your Phoenix app, you'll need to dump the database schema as structure.sql

  • Run rails db:structure:dump in your Rails App
  • Copy the generated structure.sql file to priv/repo/structure.sql in your Phoenix App

You can now load the structure in your Phoenix App via mix ecto.load.

Alternatively, you can directly use the existing database. For tests, it's still necessary to have the structure.sql ready to create the test database.

Create Schemas, delete Ecto Migrations

Create your schemas such that they exactly match your existing database structure.

This is not too hard since Ecto is a bit more configurable than ActiveRecord and naming conventions seem to match up pretty well anyway.

If you've used generators to generate your schemas, mix will also have created migrations for you.

Since the database structure is already loaded, you'll need to delete these migration files. Otherwise, Phoenix will not run because of pending migrations.

Fix Timestamps

By default, Ecto uses inserted_at and updated_at for timestamps while Active Record uses created_at and updated_at.

The latter is not a problem, but the former is different, so you will get errors like this one

** (Postgrex.Error) ERROR 42703 (undefined_column) column "inserted_at" of relation "maps" does not exist

Its easy to tell Ecto to use custom names for timestamp columns, by using the following option in your schema:

  schema "maps" do
    # Your fields...

    timestamps(inserted_at: :created_at)

Adjust Ecto Tasks

By default, the provided mix tasks will run the migrations to create the database.

For normal development, you might not mind because you ran mix ecto.load manually or are using your existing rails database.

But when testing, mix will by default clear and re-create the test database on each run via migrations, which will not work.

To fix this, you can adjust the aliases in your mix.exs file to load the existing structure instead:

  defp aliases do
      setup: ["deps.get", "ecto.setup", "cmd npm install --prefix assets"],
      "ecto.setup": ["ecto.create", "ecto.load", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      test: ["ecto.create --quiet", "ecto.load --quiet --skip-if-loaded", "test"]

Pay attention to the --skip-if-loaded option in the test alias, otherwise you'll need to confirm that the schema is already loaded on each test run.

Use the existing database from your Rails App

To use the existing database directly instead of creating a new database that just has the same structure, all you need to do is setup your Repo in dev.exs or prod.exs with the same configuration from your Rails' database.yml

Thats it

Now have fun re-writing your Rails App in Phoenix 😇

Posted on by:


markdown guide