midas / with_uuid

Provides facilities to utilize UUIDs with ActiveRecord, including model and migration extensions.
MIT License
1 stars 3 forks source link

adapter_name failure with Rails-4.0.2 #2

Open derekatkins opened 10 years ago

derekatkins commented 10 years ago

Trying to use with_uuid 0.2.0 with rails 4.0.2 and ruby 1.9.3, I built a simple test app model and migration:

class TestModel < ActiveRecord::Base
  include WithUuid::Model
end
class CreateTestModels < ActiveRecord::Migration
  def change
    create_table :test_models, id: false do |t|
      t.uuid :id, primary_key: true
      t.string :name

      t.timestamps
    end
  end
end

But running rake db:migrate results in the following error:

rake db:migrate
==  CreateTestModels: migrating ===============================================
-- create_table(:test_models, {:id=>false})
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `adapter_name' for nil:NilClass/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/with_uuid-0.2.0/lib/with_uuid/extensions/migrations.rb:11:in `block in uuid'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/with_uuid-0.2.0/lib/with_uuid/extensions/migrations.rb:10:in `each'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/with_uuid-0.2.0/lib/with_uuid/extensions/migrations.rb:10:in `uuid'
/home/warlord/src/mocana/MDP/MDP/sources/Portal/TestApp/db/migrate/20131209212352_create_test_models.rb:4:in `block in change'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:184:in `create_table'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:625:in `block in method_missing'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `block in say_with_time'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `say_with_time'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:617:in `method_missing'
/home/warlord/src/mocana/MDP/MDP/sources/Portal/TestApp/db/migrate/20131209212352_create_test_models.rb:3:in `change'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:571:in `exec_migration'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `ddl_transaction'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:764:in `up'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/migration.rb:742:in `migrate'
/home/warlord/.rvm/gems/ruby-1.9.3-p484@mdp/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
midas commented 10 years ago

Thanks for the bug report. As you can see this is pre 1.0 software and we are not yet trying to get it to work with all versions of Rails. You are more than welcome to submit a pull if you would like and I will consider it as long as it does not break Rails ~> 3 capabilities.

derekatkins commented 10 years ago

If I were more a ruby/rails guru I would; I'm very much a novice. Right now I'm not even sure what class/object @base is supposed to refer to. It's clearly nil in this case and the current code doesn't handle that.

FYI, before I found with_uuid I also tried usesguid; I found a bug there (ruby 1.8 v 1.9) but there's no issue tracker to report it (and give you the one-liner patch I came up with to fix the issue, needing .ord to get the string numeric value).

derekatkins commented 10 years ago

Doing a little research it looks like ActiveRecord::ConnectionAdapters::Table still has the @base in 4.0.2, but ActiveRecord::ConnectionAdapters::TableDefinition has changed and no longer gets the base passed into the initializer. I'm still researching how to find the actual adapter for the TableDefinition extension.

derekatkins commented 10 years ago

I did a little more research and came up with a potential solution, but it makes an assumption that the various connection adapters define certain data types uniquely. I changed the code in extensions/migrations.rb to read:

        column_names.each do |name|
          type = 'varchar(36)'
          # Handle Table (and TableDefinition in Rails 3)
          if @base
            case(@base.adapter_name.downcase)
            when 'sqlserver'
              type = 'uniqueidentifier'
            when 'postgresql'
              type = 'uuid'
            end
          # Handle TableDefinition in Rails 4
          elsif @native
            if @native[:uuid] #postgres in 4.0 already defines uuid
              type = @native[:uuid].is_a?(Hash) ? @native[:uuid][:name] : 'uuid'
            elsif @native[:ss_timestamp]  #sqlserver
              type = 'uniqueidentifier'
            end
          end

What do you think?

derekatkins commented 10 years ago

Submitted https://github.com/midas/with_uuid/pull/3