Skip to content

Commit

Permalink
Don't append limit to primary key column definition. Freeze some cons…
Browse files Browse the repository at this point in the history
…tants.
  • Loading branch information
jeremy committed Jan 15, 2009
1 parent c329794 commit 7192691
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
Expand Up @@ -254,7 +254,7 @@ def dump_schema_information #:nodoc:

def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
if native = native_database_types[type]
column_type_sql = native.is_a?(Hash) ? native[:name] : native
column_type_sql = (native.is_a?(Hash) ? native[:name] : native).dup

if type == :decimal # ignore limit, use precision and scale
scale ||= native[:scale]
Expand All @@ -269,7 +269,7 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
raise ArgumentError, "Error adding decimal column: precision cannot be empty if scale if specified"
end

elsif limit ||= native.is_a?(Hash) && native[:limit]
elsif (type != :primary_key) && (limit ||= native.is_a?(Hash) && native[:limit])
column_type_sql << "(#{limit})"
end

Expand Down
34 changes: 19 additions & 15 deletions activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
Expand Up @@ -151,13 +151,30 @@ class MysqlAdapter < AbstractAdapter
@@emulate_booleans = true
cattr_accessor :emulate_booleans

ADAPTER_NAME = 'MySQL'.freeze

LOST_CONNECTION_ERROR_MESSAGES = [
"Server shutdown in progress",
"Broken pipe",
"Lost connection to MySQL server during query",
"MySQL server has gone away"
]

NATIVE_DATABASE_TYPES = {
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze,
:string => { :name => "varchar", :limit => 255 },
:text => { :name => "text" },
:integer => { :name => "int"},
:float => { :name => "float" },
:decimal => { :name => "decimal" },
:datetime => { :name => "datetime" },
:timestamp => { :name => "datetime" },
:time => { :name => "time" },
:date => { :name => "date" },
:binary => { :name => "blob" },
:boolean => { :name => "tinyint", :limit => 1 }
}

def initialize(connection, logger, connection_options, config)
super(connection, logger)
@connection_options, @config = connection_options, config
Expand All @@ -166,28 +183,15 @@ def initialize(connection, logger, connection_options, config)
end

def adapter_name #:nodoc:
'MySQL'
ADAPTER_NAME
end

def supports_migrations? #:nodoc:
true
end

def native_database_types #:nodoc:
{
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
:string => { :name => "varchar", :limit => 255 },
:text => { :name => "text" },
:integer => { :name => "int", :limit => 11 },
:float => { :name => "float" },
:decimal => { :name => "decimal" },
:datetime => { :name => "datetime" },
:timestamp => { :name => "datetime" },
:time => { :name => "time" },
:date => { :name => "date" },
:binary => { :name => "blob" },
:boolean => { :name => "tinyint", :limit => 1 }
}
NATIVE_DATABASE_TYPES
end


Expand Down
Expand Up @@ -224,9 +224,26 @@ module ConnectionAdapters
# * <tt>:min_messages</tt> -- An optional client min messages that is used in a SET client_min_messages TO <min_messages> call on the connection.
# * <tt>:allow_concurrency</tt> -- If true, use async query methods so Ruby threads don't deadlock; otherwise, use blocking query methods.
class PostgreSQLAdapter < AbstractAdapter
ADAPTER_NAME = 'PostgreSQL'.freeze

NATIVE_DATABASE_TYPES = {
:primary_key => "serial primary key".freeze,
:string => { :name => "character varying", :limit => 255 },
:text => { :name => "text" },
:integer => { :name => "integer" },
:float => { :name => "float" },
:decimal => { :name => "decimal" },
:datetime => { :name => "timestamp" },
:timestamp => { :name => "timestamp" },
:time => { :name => "time" },
:date => { :name => "date" },
:binary => { :name => "bytea" },
:boolean => { :name => "boolean" }
}

# Returns 'PostgreSQL' as adapter name for identification purposes.
def adapter_name
'PostgreSQL'
ADAPTER_NAME
end

# Initializes and connects a PostgreSQL adapter.
Expand Down Expand Up @@ -268,20 +285,7 @@ def disconnect!
end

def native_database_types #:nodoc:
{
:primary_key => "serial primary key",
:string => { :name => "character varying", :limit => 255 },
:text => { :name => "text" },
:integer => { :name => "integer" },
:float => { :name => "float" },
:decimal => { :name => "decimal" },
:datetime => { :name => "timestamp" },
:timestamp => { :name => "timestamp" },
:time => { :name => "time" },
:date => { :name => "date" },
:binary => { :name => "bytea" },
:boolean => { :name => "boolean" }
}
NATIVE_DATABASE_TYPES
end

# Does PostgreSQL support migrations?
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/fixtures.rb
Expand Up @@ -543,7 +543,7 @@ def initialize(connection, table_name, class_name, fixture_path, file_filter = D
@connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
@class_name = class_name ||
(ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize)
@table_name = ActiveRecord::Base.table_name_prefix + @table_name + ActiveRecord::Base.table_name_suffix
@table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}"
@table_name = class_name.table_name if class_name.respond_to?(:table_name)
@connection = class_name.connection if class_name.respond_to?(:connection)
read_fixture_files
Expand Down

0 comments on commit 7192691

Please sign in to comment.