Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6999 (new defect)

Opened 2 years ago

Last modified 1 year ago

[PATCH] MySQL drops options during rename_column

Reported by: manfred Assigned to: bitsweat
Priority: normal Milestone: 1.2.7
Component: ActiveRecord Version: edge
Severity: normal Keywords: migrations rename_column mysql auto_increment
Cc: jnoon

Description

MySQL drops options when rename_column is called on the connection. This patch adds a test to see if column options are preserved and a solution for MySQL.

I've tested the patch with MySQL 5 and SQLite 3.

Attachments

rename_table_with_null_and_default.diff (2.2 kB) - added by manfred on 01/11/07 21:23:19.
preserve_options_rename_column_mysql.diff (3.2 kB) - added by manfred on 01/28/07 16:27:01.
rename_column_migration_in_mysql_keeps_default_and_null_state_and_auto_increment.diff (6.0 kB) - added by two on 09/18/07 12:18:23.

Change History

01/11/07 21:23:19 changed by manfred

  • attachment rename_table_with_null_and_default.diff added.

01/22/07 21:42:45 changed by davidjrice

Regarding http://dev.rubyonrails.org/ticket/7142#comment:2, should this ticket be closed?

01/22/07 21:47:35 changed by manfred

I personally think my implementation, although less complete is a bit cleaner.

01/28/07 14:01:57 changed by bitsweat

  • keywords changed from migrations to migrations rename_column mysql.
  • owner changed from core to bitsweat.

Closing the newer ticket. Please clean up and attach a combined patch here. Thanks!

01/28/07 16:27:01 changed by manfred

  • attachment preserve_options_rename_column_mysql.diff added.

07/03/07 17:14:07 changed by jnoon

  • cc set to jnoon.

+1

07/19/07 12:02:01 changed by robd

  • keywords changed from migrations rename_column mysql to migrations rename_column mysql auto_increment.

(in reply to: ↑ description ) 07/19/07 15:10:44 changed by robd

I tried the latest patch and came across an error when I tried to rename a not null column:

Mysql::Error: Invalid default value for 'booking_type': ALTER TABLE bookings CHANGE BookingType booking_type int(11) DEFAULT NOT NULL

I hacked my patch to do something like this:

add_column_options!(rename_column_sql, :default => optionsDefault?) unless optionsDefault?.blank?

but there's probably a better way to fix this. I think its something to do with the fact that for not-null columns, mysql show leaves the default as blank rather than NULL

(follow-up: ↓ 8 ) 07/19/07 15:13:57 changed by robd

not sure if my single quotes are being stripped, but lets try again. Error:

Mysql::Error: Invalid default value for 'booking_type': ALTER TABLE bookings CHANGE BookingType booking_type int(11) DEFAULT '' NOT NULL

Fix: split this line:

    add_column_options!(rename_column_sql, :default => options["Default"], :null => (options["Null"] == "YES")) 

into these lines:

    add_column_options!(rename_column_sql, :null => (options["Null"] == "YES")) 
    add_column_options!(rename_column_sql, :default => options["Default"]) unless options["Default"].blank? 

(in reply to: ↑ 7 ) 09/18/07 12:14:48 changed by two

Replying to robd:

Fix: split this line:

    add_column_options!(rename_column_sql, :default => options["Default"], :null => (options["Null"] == "YES")) 

into these lines:

    add_column_options!(rename_column_sql, :null => (options["Null"] == "YES")) 
    add_column_options!(rename_column_sql, :default => options["Default"]) unless options["Default"].blank? 

Hello robd, you are right: the patch fails as for not-null columns, mysql show leaves the default as empty string rather than NULL. However, the patch with the change above will lose empty strings defaults to :string or :text columns when renaming the column. I'll upload an updated patch with more tests that also includes the changes from rails edge (column names are quoted now).

09/18/07 12:18:23 changed by two

  • attachment rename_column_migration_in_mysql_keeps_default_and_null_state_and_auto_increment.diff added.