Running custom DDL commands in Ecto

I’d like to run a command in my deployment pre-start hooks which deletes the public schema in the database and then re-creates it before running my migrations. (Kind of a wipe and replace.)

I’m having trouble finding the right command for this. This doesn’t work, and I’m not even sure if it’s the right function or approach. How can I accomplish this?

  def drop_and_recreate_schema do
    Ecto.Adapters.Postgres.Connection.execute_ddl("
      DROP SCHEMA public CASCADE;
      CREATE SCHEMA public;
    ")
  end

I figured it out:

def drop_and_recreate_schema do
  Ecto.Adapters.Postgres.execute_ddl(MyApp.Repo, "DROP SCHEMA public CASCADE;", [])
  Ecto.Adapters.Postgres.execute_ddl(MyApp.Repo, "CREATE SCHEMA public;", [])
end
1 Like