activeuuid icon indicating copy to clipboard operation
activeuuid copied to clipboard

schema.rb not correct for SQLite

Open patrick-minton opened this issue 11 years ago • 7 comments

Given a migration such as:

class CreateSomeObject < ActiveRecord::Migration
  def change
    create_table :some_objects, :id => false do |t|
      t.uuid   :id, :primary_key => true
      t.string :name
    end
  end
end

You get in an entry in schema.rb such as:

create_table "some_objects", :force => true do |t|
  t.string "name"
end

Notice it looks EXACTLY like a normal table with an ID. I believe, because of this, your sqlite test database does not use UUIDs:

class SomeObjectTest < ActiveSupport::TestCase
  test "Has a UUID primary key" do
    o = SomeObject.create(:name => "FooBar")
    assert_equal UUIDTools::UUID, o.id.class
  end
end

gets:

  1) Failure:
  test_Has_a_UUID_primary_key(SomeObjectTest) [/Users/patrick/src/someobjecttest/test/unit/someobject_test.rb:6]:
  <UUIDTools::UUID> expected but was
  <Fixnum>.

If I edit the schema.rb file to look like:

 create_table :some_objects, :id => false do |t|
    t.uuid   :id, :primary_key => true
    t.string :name
 end

The same test now outputs:

 Finished tests in 0.273841s, 3.6518 tests/s, 3.6518 assertions/s.

 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

However, clearly, editing the schema.rb file by hand after every migration is not an option.

Is this a bug? Something I am missing?

patrick-minton avatar Apr 03 '13 23:04 patrick-minton

For the interim, I seem to have solved this by making the migrations thus:

  class CreateSomeObject < ActiveRecord::Migration
    def change
      create_table :some_objects, :id => false do |t|
        t.uuid   :id, :primary_key => true
        t.string :name
      end
    end
  end

And then adding this to the model:

  class SomeObject < ActiveRecord::Base

include ActiveUUID::UUID self.primary_key = 'id'

patrick-minton avatar Apr 04 '13 00:04 patrick-minton

For the moment I solved this by NOT specifying :primary_key => true clause in the migration, then in the model doing:

include ActiveUUID::UUID
self.primary_key = 'id' 

However this solution seems...hacky.

patrick-minton avatar Apr 04 '13 00:04 patrick-minton

Also, apolgies for the constant closing/reopening -- keep tabbing to the wrong button!

patrick-minton avatar Apr 04 '13 00:04 patrick-minton

It's all because of the rails migrator and schema dumpers. Internally, migrator users :primary_key create_table option only for creation integer id field. The same is for schema dumper - if it see the primary key id field (also only the field named id can be primary key in rails), it supose this id field to be integer without any type check and generates standart create_table primary_key: true migration dump.

There is a kind of solution, but it is not finished yet - https://github.com/jashmenn/activeuuid/pull/22 Also, migrators and schema dumpers are written really terrible in rails, so it is hard to patch em.

pyromaniac avatar Apr 04 '13 00:04 pyromaniac

@bilbosan can you try the patches in #22 and tell me if they work ok for you?

agios avatar Apr 14 '13 08:04 agios

@agios I gave this a try and it isn't working for me.

undefined method default_string' for #<ActiveRecord::SchemaDumper:0x007faa77594970> /Users/altonymous/Projects/activeuuid/lib/activeuuid/schema_dumper.rb:23:inspec_for_column' /Users/altonymous/Projects/activeuuid/lib/activeuuid/schema_dumper.rb:56:in block in table' /Users/altonymous/Projects/activeuuid/lib/activeuuid/schema_dumper.rb:53:inmap' /Users/altonymous/Projects/activeuuid/lib/activeuuid/schema_dumper.rb:53:in `table'

Altonymous avatar Aug 23 '13 04:08 Altonymous

@agios I copied the default_string method from rails 3.2.13

def default_string(value) case value when BigDecimal value.to_s when Date, DateTime, Time "'" + value.to_s(:db) + "'" else value.inspect end end

Now it seems to work okay for me.

I've added it to my pull request which is another fix for Rails 4.

Pull Request: https://github.com/jashmenn/activeuuid/pull/33

Altonymous avatar Aug 23 '13 05:08 Altonymous