jeremyevans / rodauth

Ruby's Most Advanced Authentication Framework
http://rodauth.jeremyevans.net
MIT License
1.65k stars 95 forks source link

Support custom primary key in accounts table on MySQL #372

Closed janko closed 8 months ago

janko commented 8 months ago

Following on https://github.com/janko/rodauth-rails/discussions/239, if one wants to use UUID primary keys on MySQL, one possibility is using a string column and generating UUIDs in Ruby.

create_table :accounts do
  String :id, primary_key: true
  # ...
end
before_create_account do
  account[:id] = SecureRandom.uuid
end

However, Sequel's #insert appears to return 0 when the primary key is something other than an autoincrementing integer on MySQL. This causes Rodauth's #save_account to overwrite the generated UUID with 0 on the account hash, causing problems down the line in things like account verification.

My idea was to just modify #save_account to set the :id if it wasn't already set. My only blocker is how to write tests for this. On Postgres, the primary key value is always returned because of the RETURNING clause. I was thinking of piggy backing on the $RODAUTH_SPEC_UUID path, but that's currently Postgres-only.

jeremyevans commented 8 months ago

Thanks for the patch. This makes sense, and Sequel::Model has same behavior of not overriding an existing primary key value.