aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
diff options
context:
space:
mode:
authorRizwan Reza <rizwanreza@gmail.com>2010-03-27 12:57:41 +0430
committerwycats <wycats@gmail.com>2010-03-27 01:33:42 -0700
commit41e5c7ed44fedb95636ef9b7a792c46ea03309bd (patch)
tree596ba598a895aa10b91890e842d0a4cee4bf653c /activerecord/lib/active_record/connection_adapters
parent68ade93cde3fcb3ac9fdfe0541d33d22c2c748d7 (diff)
downloadrails-41e5c7ed44fedb95636ef9b7a792c46ea03309bd.tar.gz
rails-41e5c7ed44fedb95636ef9b7a792c46ea03309bd.tar.bz2
rails-41e5c7ed44fedb95636ef9b7a792c46ea03309bd.zip
primary_key now supports :limit. [#876 state:resolved]
Signed-off-by: wycats <wycats@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb8
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql_adapter.rb26
2 files changed, 20 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
index 64faaef4a0..5e29baf51f 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -274,7 +274,7 @@ module ActiveRecord
column_options = {}
column_options[:null] = null unless null.nil?
column_options[:default] = default unless default.nil?
- add_column_options!(column_sql, column_options) unless type.to_sym == :primary_key
+ add_column_options!(column_sql, column_options)
column_sql
end
@@ -334,8 +334,8 @@ module ActiveRecord
# Appends a primary key definition to the table definition.
# Can be called multiple times, but this is probably not a good idea.
- def primary_key(name)
- column(name, :primary_key)
+ def primary_key(name, options = {})
+ column(name, :primary_key, options)
end
# Returns a ColumnDefinition for the column with name +name+.
@@ -357,7 +357,7 @@ module ActiveRecord
#
# Available options are (none of these exists by default):
# * <tt>:limit</tt> -
- # Requests a maximum column length. This is number of characters for <tt>:string</tt> and <tt>:text</tt> columns and number of bytes for :binary and :integer columns.
+ # Requests a maximum column length. This is number of characters for <tt>:string</tt> and <tt>:text</tt> columns and number of bytes for :binary, :integer and :primary_key columns.
# * <tt>:default</tt> -
# The column's default value. Use nil for NULL.
# * <tt>:null</tt> -
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 521bd810d0..12ca02924a 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -183,7 +183,7 @@ module ActiveRecord
QUOTED_TRUE, QUOTED_FALSE = '1'.freeze, '0'.freeze
NATIVE_DATABASE_TYPES = {
- :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze,
+ :primary_key => { :name => "DEFAULT NULL auto_increment PRIMARY KEY", :limit => 4 },
:string => { :name => "varchar", :limit => 255 },
:text => { :name => "text" },
:integer => { :name => "int", :limit => 4 },
@@ -541,15 +541,21 @@ module ActiveRecord
# Maps logical Rails types to MySQL-specific data types.
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
- return super unless type.to_s == 'integer'
-
- case limit
- when 1; 'tinyint'
- when 2; 'smallint'
- when 3; 'mediumint'
- when nil, 4, 11; 'int(11)' # compatibility with MySQL default
- when 5..8; 'bigint'
- else raise(ActiveRecordError, "No integer type has byte size #{limit}")
+ case type.to_s
+ when 'primary_key':
+ native = native_database_types[:primary_key]
+ return type_to_sql('integer', limit) + ' ' + native[:name]
+ when 'integer':
+ case limit
+ when 1; 'tinyint'
+ when 2; 'smallint'
+ when 3; 'mediumint'
+ when nil, 4, 11; 'int(11)' # compatibility with MySQL default
+ when 5..8; 'bigint'
+ else raise(ActiveRecordError, "No integer type has byte size #{limit}")
+ end
+ else
+ super
end
end