Drop all database objects?

I’m still struggling with how to completely reset my database in production (not really production, but a production-like environment running in prod mode on Fly.io)

I have added a reset method to lib/myapp/release.ex as follows:

defmodule Myapp.Release do
  @moduledoc """
  Used for executing DB release tasks when run in production without Mix
  installed.
  """
  @app :myapp

  def migrate do
    load_app()

    for repo <- repos() do
      {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
    end
  end

  def rollback(repo, version) do
    load_app()
    {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
  end

  def reset do
    load_app()
    IO.inspect('+----------------------------------------------+')
    IO.inspect('| DROPPING ALL DATA AND RESETTING THE DATABASE |')
    IO.inspect('+----------------------------------------------+')

    for repo <- repos() do
      {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, all: true))
    end

    migrate()
  end

  defp repos do
    Application.fetch_env!(@app, :ecto_repos)
  end

  defp load_app do
    Application.load(@app)
  end
end

The reset method should basically just run all the down migrations.

My problem is that the local branch that I’m working on has completely different migration files now – I have deleted all the old migration files that were previously deployed to production and I now have completely new database migration files.

So I want to write a custom “down migration” that my reset method will run, and I want it to just say “drop all database tables and indexes and any other database objects”. Does anyone know what the incantation for this might be?

Thanks.

Have a look at the source for Mix.Tasks.Ecto.Drop & Mix.Tasks.Ecto.Create

At glance it seems like repo.__adapter__.storage_down/1 & repo.__adapter__.storage_up/1 might help you do what you want or at least put you on the right path.

I can’t drop because I already have a version of my app deployed and it is holding open database connections. Discussed a bit here: I need to run `mix ecto.reset` in production

Ah, okay. I saw that and thought you basically wanted to just drop the whole database.