module ActiveRecord::Mysql::Awesome

Constants

VERSION

Public Instance Methods

column_spec_for_primary_key(column, options) click to toggle source
# 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 = {}) click to toggle source
# 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
extract_precision(sql_type) click to toggle source
Calls superclass method
# File lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb, line 200
def extract_precision(sql_type)
  if /time/ === sql_type
    super || 0
  else
    super
  end
end
migration_keys() click to toggle source
Calls superclass method
# 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) click to toggle source
Calls superclass method
# 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
quote(value, column = nil) click to toggle source
Calls superclass method
# 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?() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
Calls superclass method
# 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) click to toggle source
# 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

Protected Instance Methods

add_column_sql(table_name, column_name, type, options = {}) click to toggle source
# File lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb, line 291
def add_column_sql(table_name, column_name, type, options = {})
  td = create_table_definition(table_name)
  cd = td.new_column_definition(column_name, type, options)
  schema_creation.visit_AddColumn cd
end
change_column_sql(table_name, column_name, type, options = {}) click to toggle source
# File lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb, line 297
def change_column_sql(table_name, column_name, type, options = {})
  column = column_for(table_name, column_name)

  unless options_include_default?(options)
    options[:default] = column.default
  end

  unless options.has_key?(:null)
    options[:null] = column.null
  end

  td = create_table_definition(table_name)
  cd = td.new_column_definition(column.name, type, options)
  schema_creation.accept(ChangeColumnDefinition.new(cd, column.name))
end
rename_column_sql(table_name, column_name, new_column_name) click to toggle source
# File lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb, line 313
def rename_column_sql(table_name, column_name, new_column_name)
  column  = column_for(table_name, column_name)
  options = {
    default: column.default,
    null: column.null,
    auto_increment: column.auto_increment?
  }

  current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'", 'SCHEMA')["Type"]
  td = create_table_definition(table_name)
  cd = td.new_column_definition(new_column_name, current_type, options)
  schema_creation.accept(ChangeColumnDefinition.new(cd, column.name))
end

Private Instance Methods

configure_connection() click to toggle source
Calls superclass method
# File lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb, line 329
def configure_connection
  _config = @config
  if [':default', :default].include?(@config[:strict])
    @config = @config.deep_merge(variables: { sql_mode: :default })
  end
  super
ensure
  @config = _config
end