My migration does not appear to work. Why?

I read that if I want to add a field to a table via Phoenix I can do so by opening the recent migration file and altering it with a field such as this:

    alter table(:testbeds) do
      add :url, :string
    end

So I took my original migration that looked like this:

defmodule App.Repo.Migrations.CreateTestbeds do
  use Ecto.Migration

  def change do
    create table(:testbeds) do
      add :name, :string
      add :version, :string
      add :note, :string
      add :developer, :string
      add :status, :string
      add :status_action_value, :string

      timestamps()
    end
  end
end

and changed it to this:

defmodule App.Repo.Migrations.CreateTestbeds do
  use Ecto.Migration

  def change do
    create table(:testbeds) do
      add :name, :string
      add :version, :string
      add :note, :string
      add :developer, :string
      add :status, :string
      add :status_action_value, :string

      timestamps()
    end

    alter table(:testbeds) do
      add :url, :string
    end

    
  end
end

I ran this command in the terminal:

mix ecto.migrate

A prompt said:

[info] Migrations already up

I opened up Postgres and queried the table, nothing has changed.

Short answer is make another migration for any database change.

Once a migration has been run, it’s not going to run again (unless it’s rolled back). Rolling back a migration to edit it is fine to do before it’s been committed and shared, but after that not something you’re really going to want to do.

I don’t know what that means.

Do I create a new file in the migrations folder? Do I use the same code?

I guess it was me that sent you the wrong way by not being clear enough :sweat_smile:

You should not modify migrations that have already run. ecto.migrate keeps track of migrations via the migrations’ module name, not the file contents.

You will need to create a new migration with mix ecto.gen.migration add_url_to_testbeds and add the alter table(...) there, then run mix ecto.migrate again.

Thanks, worked perfect

Expanding on that answer, the contents of the new migration file will look something like:

defmodule App.Repo.Migrations.AddUrlToTestbeds do
  use Ecto.Migration

  def change do
    alter table(:testbeds) do
      add :url, :string
    end
  end
end
2 Likes

I picked that up, but it’s good you wrote it out for the next person