activeuuid
activeuuid copied to clipboard
schema.rb not correct for SQLite
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?
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'
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.
Also, apolgies for the constant closing/reopening -- keep tabbing to the wrong button!
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.
@bilbosan can you try the patches in #22 and tell me if they work ok for you?
@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:in
spec_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:in
map'
/Users/altonymous/Projects/activeuuid/lib/activeuuid/schema_dumper.rb:53:in `table'
@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