Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema.rb not correct for SQLite #27

Open
patrick-minton opened this issue Apr 3, 2013 · 7 comments
Open

schema.rb not correct for SQLite #27

patrick-minton opened this issue Apr 3, 2013 · 7 comments

Comments

@patrick-minton
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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

@pyromaniac
Copy link
Collaborator

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 - #22
Also, migrators and schema dumpers are written really terrible in rails, so it is hard to patch em.

@agios
Copy link

agios commented Apr 14, 2013

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

@Altonymous
Copy link

@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
Copy link

@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: #33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants