Module: ActiveRecord::Mysql::Awesome

Included in:
ConnectionAdapters::AbstractMysqlAdapter
Defined in:
lib/activerecord/mysql/awesome/railtie.rb,
lib/activerecord/mysql/awesome/version.rb,
lib/activerecord-mysql-awesome/active_record/schema_dumper.rb,
lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb

Defined Under Namespace

Modules: Column, ColumnMethods, SchemaCreation, SchemaDumper Classes: ChangeColumnDefinition, ColumnDefinition, Railtie, Table, TableDefinition

Constant Summary collapse

VERSION =
"0.0.9"

Instance Method Summary collapse

Instance Method Details

#column_spec_for_primary_key(column, options) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb', line 245

def column_spec_for_primary_key(column, options)
  spec = {}
  if column.auto_increment?
    spec[:id] = ':bigint' if column.bigint?
    spec[:unsigned] = 'true' if column.unsigned?
    return if spec.empty?
  else
    spec[:id] = column.type.inspect
    spec.merge!(prepare_column_options(column, options).delete_if { |key, _| [:name, :type, :null].include?(key) })
  end
  spec
end

#drop_table(table_name, options = {}) ⇒ Object



285
286
287
# File 'lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb', line 285

def drop_table(table_name, options = {})
  execute "DROP#{' TEMPORARY' if options[:temporary]} TABLE#{' IF EXISTS' if options[:if_exists]} #{quote_table_name(table_name)}#{' CASCADE' if options[:force] == :cascade}"
end

#migration_keysObject



269
270
271
# File 'lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb', line 269

def migration_keys
  super | [:unsigned, :collation]
end

#options_for_column_spec(table_name) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb', line 237

def options_for_column_spec(table_name)
  if collation = select_one("SHOW TABLE STATUS LIKE '#{table_name}'")["Collation"]
    super.merge(collation: collation)
  else
    super
  end
end

#prepare_column_options(column, options) ⇒ Object

:nodoc:



258
259
260
261
262
263
264
265
266
267
# File 'lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb', line 258

def prepare_column_options(column, options) # :nodoc:
  spec = super
  spec.delete(:precision) if /time/ === column.sql_type && column.precision == 0
  spec.delete(:limit) if :boolean === column.type
  spec[:unsigned] = 'true' if column.unsigned?
  if column.collation && column.collation != options[:collation]
    spec[:collation] = column.collation.inspect
  end
  spec
end

#quote(value, column = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb', line 135

def quote(value, column = nil)
  return super if value.nil? || !value.acts_like?(:time)
  return super unless column && /time/ === column.sql_type

  if value.acts_like?(:time)
    zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal

    if value.respond_to?(zone_conversion_method)
      value = value.send(zone_conversion_method)
    end
  end

  if (precision = column.precision) && value.respond_to?(:usec)
    number_of_insignificant_digits = 6 - precision
    round_power = 10 ** number_of_insignificant_digits
    value = value.change(usec: value.usec / round_power * round_power)
  end

  result = value.to_s(:db)
  if value.respond_to?(:usec) && value.usec > 0
    "'#{result}.#{sprintf("%06d", value.usec)}'"
  else
    "'#{result}'"
  end
end

#supports_datetime_with_precision?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb', line 211

def supports_datetime_with_precision?
  version >= '5.6.4'
end

#table_options(table_name) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
# File 'lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb', line 273

def table_options(table_name)
  create_table_info = select_one("SHOW CREATE TABLE #{quote_table_name(table_name)}")["Create Table"]

  return unless create_table_info

  # strip create_definitions and partition_options
  raw_table_options = create_table_info.sub(/\A.*\n\) /m, '').sub(/\n\/\*!.*\*\/\n\z/m, '').strip

  # strip AUTO_INCREMENT
  raw_table_options.sub(/(ENGINE=\w+)(?: AUTO_INCREMENT=\d+)/, '\1')
end

#type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = false) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb', line 215

def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = false)
  sql = case type
  when :integer
    case limit
    when nil, 4, 11; 'int'  # compatibility with MySQL default
    else
      super(type, limit, precision, scale)
    end
  when :datetime, :time
    case precision
    when nil; super(type, limit, precision, scale)
    when 0..6; "#{type}(#{precision})"
    else raise(ActiveRecordError, "No #{type} type has precision of #{precision}. The allowed range of precision is from 0 to 6")
    end
  else
    super(type, limit, precision, scale)
  end

  sql << ' unsigned' if unsigned && type != :primary_key
  sql
end

#update_table_definition(table_name, base) ⇒ Object



117
118
119
# File 'lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb', line 117

def update_table_definition(table_name, base)
  Table.new(table_name, base)
end