7stud

7stud

Rollback/delete ecto database

Here’s my situation:

~/phoenix_apps/dog$ mix ecto.migrations

Repo: Dog.Repo

  Status    Migration ID    Migration Name
--------------------------------------------------
  up        20190715054239  create_users
  up        20190716225030  create_members
  up        20190716225304  create_memberships

But, when I try:

~/phoenix_apps/dog$ mix ecto.rollback

17:32:13.213 [info]  == Running 20190716225304 Dog.Repo.Migrations.CreateMemberships.change/0 backward

17:32:13.216 [info]  drop table memberships
** (Postgrex.Error) ERROR 42P01 (undefined_table) table "memberships" does not exist
    (ecto_sql) lib/ecto/adapters/sql.ex:621: Ecto.Adapters.SQL.raise_sql_call_error/1
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (ecto_sql) lib/ecto/adapters/sql.ex:708: Ecto.Adapters.SQL.execute_ddl/4
    (ecto_sql) lib/ecto/migration/runner.ex:340: Ecto.Migration.Runner.log_and_execute_ddl/3
    (ecto_sql) lib/ecto/migration/runner.ex:119: anonymous fn/6 in Ecto.Migration.Runner.flush/0
    (elixir) lib/enum.ex:1940: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto_sql) lib/ecto/migration/runner.ex:118: Ecto.Migration.Runner.flush/0
    (stdlib) timer.erl:166: :timer.tc/1
    (ecto_sql) lib/ecto/migration/runner.ex:27: Ecto.Migration.Runner.run/7
    (ecto_sql) lib/ecto/migrator.ex:276: Ecto.Migrator.attempt/7
    (ecto_sql) lib/ecto/migrator.ex:211: anonymous fn/4 in Ecto.Migrator.do_down/4
    (ecto_sql) lib/ecto/migrator.ex:258: anonymous fn/3 in Ecto.Migrator.run_maybe_in_transaction/6
    (ecto_sql) lib/ecto/adapters/sql.ex:890: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
    (db_connection) lib/db_connection.ex:1415: DBConnection.run_transaction/4
    (ecto_sql) lib/ecto/migrator.ex:257: Ecto.Migrator.run_maybe_in_transaction/6
    (elixir) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
    (elixir) lib/task/supervised.ex:35: Task.Supervised.reply/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

And, if I try:

~/phoenix_apps/dog$ mix ecto.migrate
17:39:34.415 [info]  Already up

~/phoenix_apps/dog$ mix ecto.migrations

Repo: Dog.Repo

  Status    Migration ID    Migration Name
--------------------------------------------------
  up        20190715054239  create_users
  up        20190716225030  create_members
  up        20190716225304  create_memberships

It appears I am stuck in an infinite circle of hell.

Marked As Solved

wolfiton

wolfiton

Also Liked

kokolegorille

kokolegorille

You can reset your db with

$ mix ecto.drop
$ mix ecto.create
$ mix ecto.migrate

Did You create migration cleanly?

7stud

7stud

Okay, I did that:

~/phoenix_apps/dog$ mix ecto.drop
The database for Dog.Repo has been dropped
~/phoenix_apps/dog$ mix ecto.create
The database for Dog.Repo has been created
~/phoenix_apps/dog$ mix ecto.migrate

17:42:49.198 [info]  == Running 20190715054239 Dog.Repo.Migrations.CreateUsers.change/0 forward

17:42:49.201 [info]  create table users

17:42:49.215 [info]  == Migrated 20190715054239 in 0.0s

17:42:49.252 [info]  == Running 20190716225030 Dog.Repo.Migrations.CreateMembers.change/0 forward

17:42:49.252 [info]  create table members

17:42:49.257 [info]  == Migrated 20190716225030 in 0.0s

17:42:49.263 [info]  == Running 20190716225304 Dog.Repo.Migrations.CreateMemberships.change/0 forward

17:42:49.263 [info]  create table memberships

17:42:49.271 [info]  == Migrated 20190716225304 in 0.0s

Then I was able to do this:

~/phoenix_apps/dog$ iex -S mix

iex(3)> Dog.MembershipManager.list_memberships()                                 
[debug] QUERY OK source="memberships" db=0.7ms queue=0.7ms
SELECT m0."id", m0."name", m0."start_date", m0."end_date", m0."member_id", m0."inserted_at", m0."updated_at" FROM "memberships" AS m0 []

Thanks!

@7stud Have you checked your SQL database to see if the tables exist?

No, sorry. I’m using OSX.

Handy to know:

defp aliases do
    [
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      test: ["ecto.create --quiet", "ecto.migrate", "test"]
    ]
  end

So, mix ecto.reset is equivalent to:

$ mix ecto.drop
$ mix ecto.create
$ mix ecto.migrate
$ mix run priv/repo/seeds.exs   #which in my case is empty

Thanks!

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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

We're in Beta

About us Mission Statement