Skip to content

Commit

Permalink
Merge pull request #9928 from vipulnsward/fix_rename_auto_increment_m…
Browse files Browse the repository at this point in the history
…ysql

respect auto_increment in rename_column for mysql
  • Loading branch information
rafaelfranca committed Mar 26, 2013
2 parents b6e5971 + 437961b commit 1707763
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
7 changes: 6 additions & 1 deletion activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##

* `rename_column` preserves auto_increment in mysql migrations.
Fixes #3493.

*Vipul A M*

* PostgreSQL geometric type point is supported by ActiveRecord. Fixes #7324.

*Martin Schuerrer*
Expand Down Expand Up @@ -36,7 +41,7 @@

*Ian Young*

* If ``:inverse_of` is true on an association, then when one calls `find()` on
* If `:inverse_of` is true on an association, then when one calls `find()` on
the association, Active Record will first look through the in-memory objects
in the association for a particular id. Then, it will go to the DB if it
is not found. This is accomplished by calling `find_by_scan` in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,9 @@ def add_column_options!(sql, options) #:nodoc:
if options[:null] == false
sql << " NOT NULL"
end
if options[:auto_increment] == true
sql << " AUTO_INCREMENT"
end
end

# SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def schema_creation
end

class Column < ConnectionAdapters::Column # :nodoc:
attr_reader :collation, :strict
attr_reader :collation, :strict, :extra

def initialize(name, default, sql_type = nil, null = true, collation = nil, strict = false)
def initialize(name, default, sql_type = nil, null = true, collation = nil, strict = false, extra = "")
@strict = strict
@collation = collation

@extra = extra
super(name, default, sql_type, null)
end

Expand Down Expand Up @@ -221,8 +221,8 @@ def each_hash(result) # :nodoc:
end

# Overridden by the adapters to instantiate their specific Column type.
def new_column(field, default, type, null, collation) # :nodoc:
Column.new(field, default, type, null, collation)
def new_column(field, default, type, null, collation, extra = "") # :nodoc:
Column.new(field, default, type, null, collation, extra)
end

# Must return the Mysql error number from the exception, if the exception has an
Expand Down Expand Up @@ -452,7 +452,7 @@ def columns(table_name)#:nodoc:
sql = "SHOW FULL FIELDS FROM #{quote_table_name(table_name)}"
execute_and_free(sql, 'SCHEMA') do |result|
each_hash(result).map do |field|
new_column(field[:Field], field[:Default], field[:Type], field[:Null] == "YES", field[:Collation])
new_column(field[:Field], field[:Default], field[:Type], field[:Null] == "YES", field[:Collation], field[:Extra])
end
end
end
Expand Down Expand Up @@ -682,6 +682,7 @@ def rename_column_sql(table_name, column_name, new_column_name)
if column = columns(table_name).find { |c| c.name == column_name.to_s }
options[:default] = column.default
options[:null] = column.null
options[:auto_increment] = (column.extra == "auto_increment")
else
raise ActiveRecordError, "No such column: #{table_name}.#{column_name}"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def each_hash(result) # :nodoc:
end
end

def new_column(field, default, type, null, collation) # :nodoc:
Column.new(field, default, type, null, collation, strict_mode?)
def new_column(field, default, type, null, collation, extra = "") # :nodoc:
Column.new(field, default, type, null, collation, strict_mode?, extra)
end

def error_number(exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ def each_hash(result) # :nodoc:
end
end

def new_column(field, default, type, null, collation) # :nodoc:
Column.new(field, default, type, null, collation, strict_mode?)
def new_column(field, default, type, null, collation, extra = "") # :nodoc:
Column.new(field, default, type, null, collation, strict_mode?, extra)
end

def error_number(exception) # :nodoc:
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/migration/columns_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ def test_rename_column_preserves_default_value_not_null
assert_equal 70000, default_after
end

if current_adapter?(:MysqlAdapter, :Mysql2Adapter)
def test_mysql_rename_column_preserves_auto_increment
rename_column "test_models", "id", "id_test"
assert_equal "auto_increment", connection.columns("test_models").find { |c| c.name == "id_test" }.extra
end
end

def test_rename_nonexistent_column
exception = if current_adapter?(:PostgreSQLAdapter, :OracleAdapter)
ActiveRecord::StatementInvalid
Expand Down

0 comments on commit 1707763

Please sign in to comment.