Code Monkey home page Code Monkey logo

Comments (8)

jashmenn avatar jashmenn commented on September 25, 2024

Interesting. More-or-less what you're asking is the point of this library: to provide binary UUIDs under an integer id compatible interface.

Could you post here what errors you're seeing? We've tried as best we can to confirm to the "normal" ActiveRecord conventions to make this possible.

from activeuuid.

threewordphrase avatar threewordphrase commented on September 25, 2024

I created a DB migration to convert the ID column to UUID, included ActiveUUID::UUID in my model, restarted webrick and then tried to load a record with an old ID (in this case, 82). Relevant lines from Rails log:

Parameters: {"utf8"=>"✓", "show"=>{"id"=>"82"}, "id"=>"presentation_report"}
Show Load (0.5ms) SELECT shows.* FROM shows WHERE shows.id = x'00000000000000000000000000003832' LIMIT 1
Completed 404 Not Found in 98ms (ActiveRecord: 7.2ms)
ActiveRecord::RecordNotFound (Couldn't find Show with 'id'=82):

My migration was just this one liner:

change_column :shows, :id, :uuid

IDs were appearing in their integer form when looking at the table in Sequel Pro. Perhaps I missed an installation step?

from activeuuid.

jashmenn avatar jashmenn commented on September 25, 2024

I haven't looked at this closely, but I don't think that a UUID conversion will keep your old IDs. E.g. in that request above, I don't think you'll be able to look up {"id" => "82"} and have that map to the UUID you're expecting.

You might want to try keeping your id column as an integer and then create an additional uuid column. Then re-save each record (which will create a uuid for each one) and then use the uuid column in that way.

from activeuuid.

dankohn avatar dankohn commented on September 25, 2024

@crypticsymbols Look at using for_each in your migration, which will load in batches of records at about ~1000 at a time but without running out of memory. For each one, you can just replace the existing id with a randomly generated ID from running SecureRandom.uuid.

from activeuuid.

threewordphrase avatar threewordphrase commented on September 25, 2024

Well, the trouble is we need to keep the old integer IDs. We can certainly re-save each record, but it doesn't seem to want to take an integer value. After the migration:

2.2.1 :012 > s.id
=> #<UUID:0x3fd0d35be0a8 UUID:38320000-0000-0000-0000-000000000000>
2.2.1 :013 > s.id = 82
=> 82
2.2.1 :014 > s.id
=> nil

Honestly I wasn't really expecting that to work ;). I also tried creating a new UUID column, setting that to an integer on a test, and then querying where uuid = 82 - but still no dice. Do I need to cast or convert it somehow?

Unless there's something obvious I'm missing, I don't want to take up too much time. We can deal with the performance hit of using string PKs.

from activeuuid.

dankohn avatar dankohn commented on September 25, 2024

Yes, what you're missing is that you can't store an integer in a uuid field. So, it sounds like you'll be best off adding a new column, generating uuids for that column, and then changing your primary key to be the uuid column.

from activeuuid.

threewordphrase avatar threewordphrase commented on September 25, 2024

Ah, gotcha. I misinterpreted the first comment about backwards compatibility. Thanks guys!

from activeuuid.

jashmenn avatar jashmenn commented on September 25, 2024

So the problem is that integer ids and binary ids aren't really interchangeable in that sort of "straightforward" way. You can keep your integer ids and uuids by doing the following:

  1. Revert back to your original state where you have your regular ids as integers (e.g. how you had it before you started using activeuuid
  2. Create a migration that adds a uuid column, and make the uuid column the binary uuid.

If you do this, you'll be able to find by id in your old way and then also find by uuid using the new uuids.

In the past, I've even gone so far as to patch .find to auto-detect whether it's an integer id or uuid and then automatically pick the right finder. (I think early versions of this plugin had that. But it isn't 100% reliable so something like that isn't in core.)

from activeuuid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.