pcasa
pcasa

Reputation: 3730

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

Upvotes: 7

Views: 1920

Answers (2)

Matt Connolly
Matt Connolly

Reputation: 9857

You could hack your copy of active record; Add the following

          opts[:name] = opts[:name][0..63] # can't be more than 64 chars long

Around line 535 (in version 3.2.9) of $GEM_HOME/gems/activerecord-3.2.9/lib/active_record/connection_adapters/sqlite_adapter.rb

It's a hack but it might get you past a hurdle. If I had more time, I'd look in to writing a test and sending a pull request to rails core team.

Upvotes: 0

brettish
brettish

Reputation: 2648

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.

Upvotes: 1

Related Questions