Permalink
Browse files

fix mysql primary keys on one-column tables

  • Loading branch information...
seamusabshere committed Jun 23, 2012
1 parent befc94c commit 8e700b8b7e0843635c8f4101b1d1918f3b6023a7
Showing with 58 additions and 15 deletions.
  1. +6 −0 CHANGELOG
  2. +1 −1 lib/active_record_inline_schema/config.rb
  3. +1 −1 lib/active_record_inline_schema/version.rb
  4. +16 −0 spec/models.rb
  5. +34 −13 spec/shared_examples.rb
View
@@ -1,3 +1,9 @@
+0.5.7 / 2012-06-22
+
+* Bug fix
+
+ * In MySQL, properly create non-standard primary keys for one-column tables.
+
0.5.6 / 2012-06-22
* Enhancements
@@ -62,7 +62,7 @@ def apply(create_table_options)
elsif mysql?
if non_standard_primary_key
k = connection.quote_column_name(model.primary_key)
- create_sql.sub! /#{k} *([^\)]+)(?:[^,]+),/, "#{k} \\1) PRIMARY KEY,"
+ create_sql.sub! /#{k}([^\)]+)\)([^\),]*)/, "#{k}\\1) PRIMARY KEY"
end
connection.execute create_sql
end
@@ -1,3 +1,3 @@
module ActiveRecordInlineSchema
- VERSION = "0.5.6"
+ VERSION = "0.5.7"
end
View
@@ -87,6 +87,22 @@ class Pet2 < ActiveRecord::Base
col :name
end
+class Pet3 < ActiveRecord::Base
+ include SpecHelper
+end
+
+class Pet4 < ActiveRecord::Base
+ include SpecHelper
+ self.primary_key = "name"
+ col :name
+end
+
+class Pet5 < ActiveRecord::Base
+ include SpecHelper
+ self.primary_key = "id"
+ col :id, :type => :integer
+end
+
case ENV['DB_ADAPTER']
when 'mysql'
class CustomMysql < ActiveRecord::Base
View
@@ -5,20 +5,41 @@
end
end
- it "properly registers non-incrementing integer primary keys" do
- Pet2.auto_upgrade!
- e = if sqlite?
- ActiveRecord::StatementInvalid
- else
- ActiveRecord::RecordNotUnique
- end
- lambda do
- 2.times do
- p = Pet2.new
- p.id = 1
- p.save!
+ describe :regressions do
+ def assert_unique(model, column_name, v)
+ e = if sqlite?
+ ActiveRecord::StatementInvalid
+ else
+ ActiveRecord::RecordNotUnique
end
- end.must_raise(e)
+ lambda do
+ 2.times do
+ p = model.new
+ p.send "#{column_name}=", v
+ p.save!
+ end
+ end.must_raise(e)
+ end
+
+ it "properly creates tables with only one column, an auto-increment primary key" do
+ Pet3.auto_upgrade!
+ assert_unique Pet3, :id, 1
+ end
+
+ it "properly creates with only one column, a string primary key" do
+ Pet4.auto_upgrade!
+ assert_unique Pet4, :name, 'Jerry'
+ end
+
+ it "properly creates with only one column, a non-auto-increment integer primary key" do
+ Pet5.auto_upgrade!
+ assert_unique Pet5, :id, 1
+ end
+
+ it "properly registers non-incrementing integer primary keys" do
+ Pet2.auto_upgrade!
+ assert_unique Pet2, :id, 1
+ end
end
it 'has #key,col,property,attribute inside model' do

0 comments on commit 8e700b8

Please sign in to comment.