tadasajon

tadasajon

I need to run `mix ecto.reset` in production

My dev environment is my MacBook and my prod environment is on Fly with a Postgres database I have configured via Fly.io.

I cannot run mix tasks on my Fly.io setup because it is working with releases.

I have a lib/myapp/release.ex file which runs Ecto migrations for me when I deploy to prod.

But I’m still in heavy learning/development mode overall, so my prod environment is not really production and I have no users – it is better thought of as a staging environment.

I’m constantly refactoring my schemas as I learn and experiment, so in development I constantly run mix ecto.reset to drop everything in my database and re-run my migrations from scratch.

But in my prod environment I cannot run mix ecto.reset. I could just add a migration that drops everything, but I’d rather not end up with hundreds of migrations before arriving at my final starting point.

So how can I add code to my release.ex module to just drop everything and start from scratch every time I deploy to production? Once I’m ready to actually keep my data around and get some users I’ll obviously remove the code that drops all the data every time I deploy.

Most Liked

k3nt

k3nt

Another option if you don’t want to have to edit your migrate def, is to ssh into your fly machine, start IEx, and then just call Ecto.Migrator.run(MyApp.Repo, :down, all: true). Then just do another release and your default migrate def will create your tables again. Steps below.

$fly ssh console
# app/bin/my_app remote
iex(my_app@5521219a)1> Ecto.Migrator.run(MyAPP.Repo, :down, all: true)
thiagomajesk

thiagomajesk

Hi @tadasajon! You are in the right direction, you can use the same technique recommended for running migrations:

defmodule MyApp.Release do
  def greet(), do: IO.puts "Hello World"
end

And then you can eval the code:

_build/prod/rel/my_app/bin/my_app eval "MyApp.Release.greet"

You can check the code inside those ecto tasks for more information on how to do it programmatically for you app release:

TLDR; Take a look at Ecto.Adapters.Postgres.storage_down and Ecto.Adapters.Postgres.storage_up functions. You can see an example of how to do it in the test helper of this lib I’m working on.

tadasajon

tadasajon

This was super-helpful @thiagomajesk. Following your example, I was able to add the following function to lib/myapp/release.ex:

  def reset do
    load_app()
    Ecto.Adapters.Postgres.storage_down(Myapp.Repo.config())
    Ecto.Adapters.Postgres.storage_up(Myapp.Repo.config())
    migrate()
  end

and I have adjusted my fly.toml file to include these lines:

[deploy]
  release_command = "/app/bin/myapp eval Myapp.Release.reset"

But I’m unfortunately still hitting a snag. When I deploy my app the reset function runs as expected, but I get the following error:

{:error, "ERROR 55006 (object_in_use) database \"myapp\" is being accessed by other users\n\nThere are 11 other sessions using the database."}

I guess this is because I already have a previous version of my app up and running and it is holding some database connections.

I thought I could solve this by running fly suspend and bringing my app down for a few minutes, but after running fly suspend I am unable to run fly deploy, since I get the error:

Error App 'myapp' is currently suspended. Resume before deploying.

I’ll update here if I come up with a solution.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement