Skip to content

Commit

Permalink
Fix issue #16 and minor tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
DAddYE committed Feb 15, 2012
1 parent b12fd45 commit afb8378
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
language: ruby
before_install: 'gem update --system' before_install: 'gem update --system'
rvm: rvm:
- 1.8.7 - 1.8.7
Expand Down
18 changes: 16 additions & 2 deletions lib/mini_record/auto_schema.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ def col(*args)
type = options.delete(:as) || options.delete(:type) || :string type = options.delete(:as) || options.delete(:type) || :string
index = options.delete(:index) index = options.delete(:index)
args.each do |column_name| 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 column_name = table_definition.columns[-1].name
case index case index
when Hash when Hash
Expand Down Expand Up @@ -190,7 +200,9 @@ class << connection; attr_accessor :table_definition; end unless connection.resp
new_attr = {} new_attr = {}


# First, check if the field type changed # 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 changed = true
end end


Expand All @@ -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 next if att == :type or att == :base or att == :name # special cases
value = true if att == :null && value.nil? value = true if att == :null && value.nil?
if value != fields_in_db[field].send(att) 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 new_attr[att] = value
changed = true changed = true
end end
Expand Down
17 changes: 16 additions & 1 deletion test/test_mini_record.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ class Fake < ActiveRecord::Base
assert_equal 2, fake.group_id assert_equal 2, fake.group_id
end 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 describe 'relation #belongs_to' do
it 'creates a column and index based on relation' do it 'creates a column and index based on relation' do
Article.create(:title => 'Hello', :publisher_id => 1) Article.create(:title => 'Hello', :publisher_id => 1)
Expand Down Expand Up @@ -338,7 +353,7 @@ class Foo < ActiveRecord::Base
assert_includes cols, 'customer_id' assert_includes cols, 'customer_id'
end 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 tables = Photogallery.connection.tables
assert_includes tables, 'pages_photogalleries' assert_includes tables, 'pages_photogalleries'
index_name_length = Photogallery.connection.index_name_length index_name_length = Photogallery.connection.index_name_length
Expand Down

0 comments on commit afb8378

Please sign in to comment.