Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails migration and column change

working with sqlite3 for local dev. Prod DB is MySql.

Have a migration file for a column change.

class ChangeDateToOrders < ActiveRecord::Migration
  def self.up
    change_column(:orders, :closed_date, :datetime)
  end

  def self.down
    change_column(:orders, :closed_date, :date)
  end
end

Errors out saying index name 'temp_index_altered_orders_on_closed_location_id_and_parent_company_id' on table 'altered_orders' is too long; the limit is 64 characters

Know there is a limitation on index name with sqlite, but is there a workaround for this?

EDIT Workaround I used.

class ChangeDateToOrders < ActiveRecord::Migration
  def self.up
    remove_index(:orders, [:closed_location_id, :parent_company_id])
    change_column(:orders, :closed_date, :datetime)
    add_index(:orders, [:closed_location_id, :parent_company_id], :name => "add_index_to_orders_cli_pci")
  end

  def self.down
    remove_index(:orders, :name => "add_index_to_orders_cli_pci")
    change_column(:orders, :closed_date, :date)
    add_index(:orders, [:closed_location_id, :parent_company_id])
  end
end
like image 671
pcasa Avatar asked Apr 06 '11 14:04

pcasa


People also ask

Can you edit a migration file Rails?

If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rake db:migrate.

What does migration do in Rails?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.


1 Answers

Personally, I like my production and development environments to match as much as possible. Its helps avoid gotchas. If I were deploying MySQL I would run my development environment with MySQL too. Besides, I am also not super familiar with SQLite so this approach appeals to my lazy side - I only need to know the ins and outs of one db.

like image 195
brettish Avatar answered Sep 23 '22 09:09

brettish