jaysoifer

jaysoifer

Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)?

Would

mix ecto.rollback -v 20080906120000

accomplish that?

Showing Posts 1 to 7

bobbypriambodo

bobbypriambodo

I believe you can’t. Here are all the available options as per mix help ecto.rollback:

## Command line options

  • -r, --repo - the repo to rollback
  • --all - revert all applied migrations
  • --step / -n - revert n number of applied migrations
  • --to / -v - revert all migrations down to and including version
  • --quiet - do not log migration commands
  • --prefix - the prefix to run migrations on
  • --pool-size - the pool size if the repository is started only for the
    task (defaults to 1)

-v reverts all down to a version, not a specific version.

I can also see why this is hard (if not impossible) to implement. The essential idea of having migrations is to have a consistent, versioned, and repeatable scripts to construct your DB. In constructing it, there are easily revertible operations such as creating tables, but also “weird” ones such as creating triggers or adding columns.

Suppose it’s possible: say migration 1 creates a table A, and migration 2 adds a column to that table. You then rollback migration 1, effectively deleting table A. What would happen to migration 2, should you try to rollback again later?

Technically it’s possible, but there will be so many edge cases that it’s a hell lot easier to just not allow that :slight_smile:

jaysoifer

jaysoifer OP

That makes sense. Just wanted to confirm it.

Thank you

OvermindDL1

OvermindDL1

You can always make a new migration that just undoes what a prior one did, but still keeps a proper versioned state.

sean

sean

It’s not pretty but this works for me:

def rollback(version) when is_integer(version) do
  re = ~r/^#{version}_.*\.exs/
  path = Application.app_dir(:your_app, Path.join(["priv", "repo", "migrations"]))

  with {:find, "" <> file} <- {:find, Enum.find(File.ls!(path), &String.match?(&1, re))},
       {:compile, [{mod, _} | _]} <- {:compile, Code.compile_file(Path.join(path, file))},
       {:rollback, :ok} <- {:rollback, Ecto.Migrator.down(Repo, version, mod)} do
    {:ok, "Reversed migration: #{file}"}
  else
    {:find, _} -> {:error, "No migration found with version prefix: #{version}"}
    {:compile, e} -> {:error, "Problem compiling migration module: #{inspect(e)}"}
    {:rollback, e} -> {:error, "Problem reversing migration: #{inspect(e)}"}
    e -> {:error, "Something unexpected happened: #{inspect(e)}"}
  end
end
i_a

i_a

Here’s an adapted example from the Ecto.Migrator docs.

Ecto.Migrator.with_repo(your_repo, &Ecto.Migrator.run(&1, :down, to: version))

evadne

evadne

This is incorrect! This will still run all migrations between the version desired and the current version.

voyenavion

voyenavion

this i think is the most helpful answer

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews