Search code examples
elixirphoenix-frameworkecto

Update the last before last schema, does it need to rollback to the that migrations?


My migration status is follows:

macbook:mirror yuchen$ mix ecto.migrations -r Mirror.Repo

Repo: Mirror.Repo

  Status    Migration ID    Migration Name
--------------------------------------------------
  up        20221011000915  create_importers
  up        20221011120109  create_products
  up        20221011233005  create_pictures

I have updated '20221011120109_create_products.exs' file by adding one fields.

defmodule Mirror.Repo.Migrations.CreateProducts do
  use Ecto.Migration

  def change do
    create table(:products) do
      add :importer_id, :string # newly added field
      add :title, :string
      add :cid, :string
      add :seller_cids, :string

Is it possible to save the test data and update schema.

In addtion, is it possible to update 'create_products' directly and rollback 'create_pictures'?

How to do it?


Solution

  • This is not exactly how migrations are supposed to be used. Instead of amending the already applied migration, you should create the new one, modifying the column, like:

    alter table(:products) do
      add :importer_id, :string # newly added field
    end
    

    But you still can apply migrations manually with a help of Ecto.Migrator.