mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Support default expression for MySQL
MySQL 8.0.13 and higher supports default value to be a function or expression. https://dev.mysql.com/doc/refman/8.0/en/create-table.html
This commit is contained in:
parent
d496055b5b
commit
a2ad8f456e
5 changed files with 34 additions and 4 deletions
|
@ -1,3 +1,11 @@
|
|||
* Support default expression for MySQL.
|
||||
|
||||
MySQL 8.0.13 and higher supports default value to be a function or expression.
|
||||
|
||||
https://dev.mysql.com/doc/refman/8.0/en/create-table.html
|
||||
|
||||
*Ryuta Kamizono*
|
||||
|
||||
* Support expression indexes for MySQL.
|
||||
|
||||
MySQL 8.0.13 and higher supports functional key parts that index
|
||||
|
|
|
@ -106,10 +106,13 @@ module ActiveRecord
|
|||
|
||||
def new_column_from_field(table_name, field)
|
||||
type_metadata = fetch_type_metadata(field[:Type], field[:Extra])
|
||||
if type_metadata.type == :datetime && /\ACURRENT_TIMESTAMP(?:\([0-6]?\))?\z/i.match?(field[:Default])
|
||||
default, default_function = nil, field[:Default]
|
||||
else
|
||||
default, default_function = field[:Default], nil
|
||||
default, default_function = field[:Default], nil
|
||||
|
||||
if type_metadata.type == :datetime && /\ACURRENT_TIMESTAMP(?:\([0-6]?\))?\z/i.match?(default)
|
||||
default, default_function = nil, default
|
||||
elsif type_metadata.extra == "DEFAULT_GENERATED"
|
||||
default = +"(#{default})" unless default.start_with?("(")
|
||||
default, default_function = nil, default
|
||||
end
|
||||
|
||||
MySQL::Column.new(
|
||||
|
|
|
@ -106,6 +106,13 @@ if current_adapter?(:Mysql2Adapter)
|
|||
class MysqlDefaultExpressionTest < ActiveRecord::TestCase
|
||||
include SchemaDumpingHelper
|
||||
|
||||
if supports_default_expression?
|
||||
test "schema dump includes default expression" do
|
||||
output = dump_table_schema("defaults")
|
||||
assert_match %r/t\.binary\s+"uuid",\s+limit: 36,\s+default: -> { "\(uuid\(\)\)" }/i, output
|
||||
end
|
||||
end
|
||||
|
||||
if subsecond_precision_supported?
|
||||
test "schema dump datetime includes default expression" do
|
||||
output = dump_table_schema("datetime_defaults")
|
||||
|
|
|
@ -48,6 +48,15 @@ def mysql_enforcing_gtid_consistency?
|
|||
current_adapter?(:Mysql2Adapter) && "ON" == ActiveRecord::Base.connection.show_variable("enforce_gtid_consistency")
|
||||
end
|
||||
|
||||
def supports_default_expression?
|
||||
if current_adapter?(:PostgreSQLAdapter)
|
||||
true
|
||||
elsif current_adapter?(:Mysql2Adapter)
|
||||
conn = ActiveRecord::Base.connection
|
||||
!conn.mariadb? && conn.version >= "8.0.13"
|
||||
end
|
||||
end
|
||||
|
||||
def supports_savepoints?
|
||||
ActiveRecord::Base.connection.supports_savepoints?
|
||||
end
|
||||
|
|
|
@ -19,6 +19,9 @@ ActiveRecord::Schema.define do
|
|||
t.datetime :fixed_time, default: "2004-01-01 00:00:00"
|
||||
t.column :char1, "char(1)", default: "Y"
|
||||
t.string :char2, limit: 50, default: "a varchar field"
|
||||
if supports_default_expression?
|
||||
t.binary :uuid, limit: 36, default: -> { "(uuid())" }
|
||||
end
|
||||
end
|
||||
|
||||
create_table :binary_fields, force: true do |t|
|
||||
|
|
Loading…
Reference in a new issue