Peter R
Peter R

Reputation: 3506

setting value in a migration

So currently my table has a date, and a date index, I want to drop the date index, add a start_date and end_date, and initially set both the start and end date equal to whatever date was, and then add start and end date indexes. Can this all be done in one migration? How would that look?

Upvotes: 0

Views: 57

Answers (1)

denis.peplin
denis.peplin

Reputation: 9831

def up do
  alter table(:table) do
    add :start_date, :date
    add :end_date, :date
  end

  execute "update table set start_date=date, end_date=date"

  alter table(:table) do
    remove :date
  end

  create index(:table, :start_date)
  create index(:table, :end_date)
end

Index will for date will be removed with field removal.

Upvotes: 1

Related Questions