Permalink
Browse files

Fix issue #16 and minor tweaks.

  • Loading branch information...
1 parent b12fd45 commit afb8378a5abd9acc2b71cc15d97190751061e671 @DAddYE committed Feb 15, 2012
Showing with 33 additions and 3 deletions.
  1. +1 −0 .travis.yml
  2. +16 −2 lib/mini_record/auto_schema.rb
  3. +16 −1 test/test_mini_record.rb
View
@@ -1,3 +1,4 @@
+language: ruby
before_install: 'gem update --system'
rvm:
- 1.8.7
@@ -33,7 +33,17 @@ def col(*args)
type = options.delete(:as) || options.delete(:type) || :string
index = options.delete(:index)
args.each do |column_name|
- table_definition.send(type, column_name, options)
+ # Allow custom types like:
+ # t.column :type, "ENUM('EMPLOYEE','CLIENT','SUPERUSER','DEVELOPER')"
+ if type.is_a?(String)
+ table_definition.column(column_name, type, options.reverse_merge(:limit => 0))
+ # else translate in:
+ # t.references :parent
+ # t.string :name
+ else
+ table_definition.send(type, column_name, options)
+ end
+ # Get the formatted column name and add correctly index
column_name = table_definition.columns[-1].name
case index
when Hash
@@ -190,7 +200,9 @@ class << connection; attr_accessor :table_definition; end unless connection.resp
new_attr = {}
# First, check if the field type changed
- if fields_in_schema[field].type.to_sym != fields_in_db[field].type.to_sym
+ if fields_in_schema[field].sql_type.to_s.downcase != fields_in_db[field].sql_type.to_s.downcase
+ logger.debug "[MiniRecord] Detected schema changed for #{table_name}.#{field}#type from " +
+ "#{fields_in_schema[field].sql_type.to_s.downcase.inspect} in #{fields_in_db[field].sql_type.to_s.downcase.inspect}" if logger
changed = true
end
@@ -205,6 +217,8 @@ class << connection; attr_accessor :table_definition; end unless connection.resp
next if att == :type or att == :base or att == :name # special cases
value = true if att == :null && value.nil?
if value != fields_in_db[field].send(att)
+ logger.debug "[MiniRecord] Detected schema changed for #{table_name}.#{field}##{att} "+
+ "from #{fields_in_db[field].send(att).inspect} in #{value.inspect}" if logger
new_attr[att] = value
changed = true
end
View
@@ -187,6 +187,21 @@ class Fake < ActiveRecord::Base
assert_equal 2, fake.group_id
end
+ it 'allow custom query' do
+ skip unless conn.adapter_name =~ /mysql/i
+
+ class Foo < ActiveRecord::Base
+ col :name, :as => "ENUM('foo','bar')"
+ end
+ Foo.auto_upgrade!
+ assert_match /ENUM/, Foo.queries
+
+ Foo.auto_upgrade!
+ assert_empty Foo.queries
+ assert_equal %w[id name], Foo.db_columns
+ assert_equal %w[id name], Foo.schema_columns
+ end
+
describe 'relation #belongs_to' do
it 'creates a column and index based on relation' do
Article.create(:title => 'Hello', :publisher_id => 1)
@@ -338,7 +353,7 @@ class Foo < ActiveRecord::Base
assert_includes cols, 'customer_id'
end
- it 'creates a join table with indexes for has_and_belongs_to_many relations with long index name' do
+ it 'creates a join table with indexes with long indexes names' do
tables = Photogallery.connection.tables
assert_includes tables, 'pages_photogalleries'
index_name_length = Photogallery.connection.index_name_length

0 comments on commit afb8378

Please sign in to comment.