Skip to content

Commit

Permalink
Initial support for auto_increment option to column fields in create_…
Browse files Browse the repository at this point in the history
…table
  • Loading branch information
vipulnsward committed Jan 24, 2013
1 parent 68a6fb6 commit 7937a42
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths, :
# are typically created by methods in TableDefinition, and added to the
# +columns+ attribute of said TableDefinition object, in order to be used
# for generating a number of table creation or table changing SQL statements.
class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :precision, :scale, :default, :null) #:nodoc:
class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :precision, :scale, :default, :null, :auto_increment) #:nodoc:

def string_to_binary(value)
value
end

def sql_type
base.type_to_sql(type.to_sym, limit, precision, scale)
base.type_to_sql(type.to_sym, limit, precision, scale, auto_increment)
end

def to_sql
Expand Down Expand Up @@ -249,6 +249,7 @@ def column(name, type, options = {})
column.scale = options[:scale]
column.default = options[:default]
column.null = options[:null]
column.auto_increment = options[:auto_increment] if !primary_key_defined?
self
end

Expand Down Expand Up @@ -311,6 +312,10 @@ def primary_key_column_name
primary_key_column && primary_key_column.name
end

def primary_key_defined?
columns.detect { |c| c.type == :primary_key }
end

def native
@base.native_database_types
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def native_database_types
{}
end

def native_auto_increment
""
end

# Truncates a table alias according to the limits of the current adapter.
def table_alias_for(table_name)
table_name[0...table_alias_length].tr('.', '_')
Expand Down Expand Up @@ -553,7 +557,7 @@ def assume_migrated_upto_version(version, migrations_paths = ActiveRecord::Migra
end
end

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

Expand All @@ -573,7 +577,7 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
elsif (type != :primary_key) && (limit ||= native.is_a?(Hash) && native[:limit])
column_type_sql << "(#{limit})"
end

column_type_sql << native_auto_increment if auto_increment && type.to_sym == :integer
column_type_sql
else
type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ def native_database_types
NATIVE_DATABASE_TYPES
end

def native_auto_increment
" auto_increment UNIQUE" #Need to specify key
end

# HELPER METHODS ===========================================

# The two drivers have slightly different ways of yielding hashes of results, so
Expand Down Expand Up @@ -500,7 +504,7 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
end

# Maps logical Rails types to MySQL-specific data types.
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
def type_to_sql(type, limit = nil, precision = nil, scale = nil, auto_increment = nil)
case type.to_s
when 'binary'
case limit
Expand All @@ -510,14 +514,16 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
else raise(ActiveRecordError, "No binary type has character length #{limit}")
end
when 'integer'
case limit
integer_type = 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
integer_type << native_auto_increment if auto_increment
integer_type
when 'text'
case limit
when 0..0xff; 'tinytext'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def index_name_length
end

# Maps logical Rails types to PostgreSQL-specific data types.
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
def type_to_sql(type, limit = nil, precision = nil, scale = nil, auto_increment = nil)
case type.to_s
when 'binary'
# PostgreSQL doesn't support limits on binary (bytea) columns.
Expand All @@ -402,6 +402,7 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
else raise(ActiveRecordError, "The limit on text can be at most 1GB - 1byte.")
end
when 'integer'
return 'serial' if auto_increment
return 'integer' unless limit

case limit
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/migration/change_schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ChangeSchemaTest < ActiveRecord::TestCase

def setup
super
ActiveRecord::Base.logger = Logger.new(STDOUT)
@connection = ActiveRecord::Base.connection
@table_name = :testings
end
Expand Down Expand Up @@ -337,6 +338,20 @@ def test_column_exists_on_table_with_no_options_parameter_supplied
end
end

test "autoincrement works without primary key" do
connection.create_table :testing_autoincrement, :id => false, :force => true do |t|
t.column :foo, :integer, :auto_increment => true
end

end

test "autoincrement works with primary key" do
connection.create_table :testing_autoincrement, :force => true do |t|
t.column :foo, :integer, :auto_increment => true
end

end

private
def testing_table_with_only_foo_attribute
connection.create_table :testings, :id => false do |t|
Expand Down

2 comments on commit 7937a42

@vipulnsward
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuki24 could you take a look at this, before I send a PR.

@yuki24
Copy link

@yuki24 yuki24 commented on 7937a42 Jan 24, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vipulnsward thank you, I'll take a look.

Please sign in to comment.