
auto_upgrade! fails to run on existing STI tables
Reported by Postmodern | June 2nd, 2009 @ 04:27 PM
I noticed that when using STI and running auto-upgrade! the initial upgrade is successful, but subsequent upgrades will raise an exception concerning duplicate columns.
Running the following example (http://gist.github.com/121233),
you can see the bug in action:
gem 'dm-core', '0.10.0'
require 'dm-core'
module ModelMixin
include DataMapper::Types
def self.included(base)
base.module_eval do
include DataMapper::Resource
include DataMapper::Migrations
property :type, Discriminator
end
end
end
class User
include ModelMixin
property :id, Serial
property :name, String
end
class Admin < User
property :level, Integer
end
DataMapper.setup(:default, 'sqlite3:test.db') DataMapper.auto_upgrade!
unless ARGV[0] == 'stop'
system('ruby',File.expand_path(__FILE__),'stop')
end
/usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:139:in `execute_non_query': duplicate column name: level (Sqlite3Error)
Query: ALTER TABLE "users" ADD COLUMN "level" INTEGER
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:139:in `upgrade_model_storage'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:133:in `map'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:133:in `upgrade_model_storage'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:253:in `with_connection'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:132:in `upgrade_model_storage'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:667:in `upgrade_model_storage'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:743:in `auto_upgrade!'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:745:in `auto_upgrade!'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:31:in `auto_upgrade!'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:57:in `with_each_model_and_repository'
from /usr/lib/ruby/1.8/set.rb:188:in `each'
from /usr/lib/ruby/1.8/set.rb:188:in `each_key'
from /usr/lib/ruby/1.8/set.rb:188:in `each'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:56:in `with_each_model_and_repository'
from /usr/lib64/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:30:in `auto_upgrade!'
from dm_auto_migration_bug.rb:34
Comments and changes to this ticket
-
Postmodern June 2nd, 2009 @ 04:36 PM
- Assigned user set to Dan Kubb (dkubb)
-
Postmodern June 5th, 2009 @ 05:26 PM
- Tag changed from 0.10.0, auto_upgrade, sti to 0.10.0, auto_upgrade, sqlite3, sti
The problem appears to be in Migrations#field_exists?. The field_exists? method attempts to run the SQL query "SHOW COLUMNS", although SQLite does not yet support "SHOW COLUMNS". This causes the query to fail, and field_exists? to return false by default causing auto_upgrade! to believe no columns exist yet.
-
Postmodern June 5th, 2009 @ 05:41 PM
Hmm the bigger question would be why is Migrations::MysqlAdapter being used instead of Migrations::Sqlite3Adapter.
-
Postmodern June 5th, 2009 @ 05:53 PM
Oops, I was looking at the wrong module. Now that I look at Sqlite3Adapter#field_exists? I notice it is selecting from "information_schema"."columns", which is not a table provided by sqlite3.
-
Postmodern June 5th, 2009 @ 06:32 PM
Blarg, never mind again.
I've added some debugging code to Sqlite3Adapter#query_table and noticed that "PRAGMA table_info" is returning empty sets 80% of the time. This would indicate that the bug is non-trivial and depends on certain settings.
-
Dan Kubb (dkubb) June 7th, 2009 @ 01:24 AM
- State changed from unconfirmed to accepted
Can you try applying the following patch and let me know if it resolves the problem for you:
-
Dan Kubb (dkubb) June 7th, 2009 @ 02:00 AM
- State changed from accepted to resolved
I have committed the fix for this ticket into dm-core/next: http://github.com/datamapper/dm-core/commit/d349924345f2bdf4b38ea31...
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »