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.
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.
In order to load your existing database schema from Rails in your Phoenix app, you'll need to dump the database schema as
rails db:structure:dumpin your Rails App
- Copy the generated
priv/repo/structure.sqlin your Phoenix App
You can now load the structure in your Phoenix App via
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 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.
By default, Ecto uses
updated_at for timestamps while Active Record uses
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) end
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"] ] end
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.
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
prod.exs with the same configuration from your Rails'
Now have fun re-writing your Rails App in Phoenix 😇