How to execute .sql file script in Phoenix?

I executed a pg dump of my existing database to get it’s schema. I would like to run this .sql script against whatever Postgresql database defined in my test.exs file.

How can I do that with Phoenix and Ecto?

2 Likes

You can create a migration to run your .sql file as long as the database exists. Something like:

Create your migration:

iex> mix ecto.gen.migration my_database_structure_migration

Then edit the migration in priv/repo/migrations/2017xxxxxxxxxxx_my_database_structure_migration.exs to be something like the following:

defmodule My.Database.Structure.Migration do
  use Ecto.Migration

  def up do
    execute File.read!("/path/to/sql_dump.sql")
  end

  def down do
    
  end
end
8 Likes

Thank you, that worked out nicely. :slight_smile: