1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix extract default with CURRENT_TIMESTUMP

This commit is contained in:
Ryuta Kamizono 2015-05-04 19:01:24 +09:00
parent 4b1188b5ab
commit 744552f72d
3 changed files with 24 additions and 2 deletions

View file

@ -495,12 +495,16 @@ module ActiveRecord
end
# Returns an array of +Column+ objects for the table specified by +table_name+.
def columns(table_name)#:nodoc:
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|
type_metadata = fetch_type_metadata(field[:Type], field[:Extra])
new_column(field[:Field], field[:Default], type_metadata, field[:Null] == "YES", nil, field[:Collation])
if type_metadata.type == :datetime && field[:Default] == "CURRENT_TIMESTAMP"
new_column(field[:Field], nil, type_metadata, field[:Null] == "YES", field[:Default], field[:Collation])
else
new_column(field[:Field], field[:Default], type_metadata, field[:Null] == "YES", nil, field[:Collation])
end
end
end
end

View file

@ -96,6 +96,17 @@ if current_adapter?(:PostgreSQLAdapter)
end
if current_adapter?(:Mysql2Adapter)
class MysqlDefaultExpressionTest < ActiveRecord::TestCase
include SchemaDumpingHelper
if ActiveRecord::Base.connection.version >= '5.6.0'
test "schema dump includes default expression" do
output = dump_table_schema("datetime_defaults")
assert_match %r/t\.datetime\s+"modified_datetime",\s+default: -> { "CURRENT_TIMESTAMP" }/, output
end
end
end
class DefaultsTestWithoutTransactionalFixtures < ActiveRecord::TestCase
# ActiveRecord::Base#create! (and #save and other related methods) will
# open a new transaction. When in transactional tests mode, this will

View file

@ -1,4 +1,11 @@
ActiveRecord::Schema.define do
if ActiveRecord::Base.connection.version >= '5.6.0'
create_table :datetime_defaults, force: true do |t|
t.datetime :modified_datetime, default: -> { 'CURRENT_TIMESTAMP' }
end
end
create_table :binary_fields, force: true do |t|
t.binary :var_binary, limit: 255
t.binary :var_binary_large, limit: 4095