aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb59
1 files changed, 28 insertions, 31 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index 5c8c4b883a..8db4bcd7e3 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -63,7 +63,7 @@ module ActiveRecord
def column_spec_for_primary_key(column)
spec = {}
- if column.extra == 'auto_increment'
+ if column.auto_increment?
return unless column.limit == 8
spec[:id] = ':bigint'
else
@@ -73,6 +73,12 @@ module ActiveRecord
spec
end
+ def prepare_column_options(column)
+ spec = super
+ spec.delete(:precision) if /time/ === column.sql_type && column.precision == 0
+ spec
+ end
+
class Column < ConnectionAdapters::Column # :nodoc:
delegate :strict, :collation, :extra, to: :sql_type_metadata, allow_nil: true
@@ -103,6 +109,10 @@ module ActiveRecord
collation && !collation.match(/_ci$/)
end
+ def auto_increment?
+ extra == 'auto_increment'
+ end
+
private
# MySQL misreports NOT NULL column default when none is given.
@@ -241,6 +251,10 @@ module ActiveRecord
version[0] >= 5
end
+ def supports_datetime_with_precision?
+ (version[0] == 5 && version[1] >= 6) || version[0] >= 6
+ end
+
def native_database_types
NATIVE_DATABASE_TYPES
end
@@ -613,13 +627,6 @@ module ActiveRecord
when 0x1000000..0xffffffff; 'longtext'
else raise(ActiveRecordError, "No text type has character length #{limit}")
end
- when 'datetime'
- return super unless precision
-
- case precision
- when 0..6; "datetime(#{precision})"
- else raise(ActiveRecordError, "No datetime type has precision of #{precision}. The allowed range of precision is from 0 to 6.")
- end
else
super
end
@@ -708,11 +715,6 @@ module ActiveRecord
m.alias_type %r(year)i, 'integer'
m.alias_type %r(bit)i, 'binary'
- m.register_type(%r(datetime)i) do |sql_type|
- precision = extract_precision(sql_type)
- MysqlDateTime.new(precision: precision)
- end
-
m.register_type(%r(enum)i) do |sql_type|
limit = sql_type[/^enum\((.+)\)/i, 1]
.split(',').map{|enum| enum.strip.length - 2}.max
@@ -730,6 +732,14 @@ module ActiveRecord
end
end
+ def extract_precision(sql_type)
+ if /time/ === sql_type
+ super || 0
+ else
+ super
+ end
+ end
+
def fetch_type_metadata(sql_type, collation = "", extra = "")
MysqlTypeMetadata.new(super(sql_type), collation: collation, extra: extra, strict: strict_mode?)
end
@@ -808,7 +818,7 @@ module ActiveRecord
options = {
default: column.default,
null: column.null,
- auto_increment: column.extra == "auto_increment"
+ auto_increment: column.auto_increment?
}
current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'", 'SCHEMA')["Type"]
@@ -912,24 +922,8 @@ module ActiveRecord
TableDefinition.new(native_database_types, name, temporary, options, as)
end
- class MysqlDateTime < Type::DateTime # :nodoc:
- def type_cast_for_database(value)
- if value.acts_like?(:time) && value.respond_to?(:usec)
- result = super.to_s(:db)
- case precision
- when 1..6
- "#{result}.#{sprintf("%0#{precision}d", value.usec / 10 ** (6 - precision))}"
- else
- result
- end
- else
- super
- end
- end
- end
-
class MysqlString < Type::String # :nodoc:
- def type_cast_for_database(value)
+ def serialize(value)
case value
when true then "1"
when false then "0"
@@ -947,6 +941,9 @@ module ActiveRecord
end
end
end
+
+ ActiveRecord::Type.register(:string, MysqlString, adapter: :mysql)
+ ActiveRecord::Type.register(:string, MysqlString, adapter: :mysql2)
end
end
end