Add UUID v7 helper functions#4739
Conversation
Adds `timestamp/1`, `datetime/1`, and `version/1` helpers for dealing with UUIDs.
|
I don't use UUIDv7 but one thing I am also wondering is why Postgres only allows shifts from the current time rather than setting precise timestamps. I'm guessing it screws up the guarantees somehow and might be insecure. So I think we need to understand this before opening up the option in Ecto. |
|
Let's please revert the
We also need to decide if we should raise or return |
|
I found the following email on the removal of The use-case they outline is pretty much exactly my usecase:
I understand why it shouldn't be a general part of this UUIDv7 implementation though, so I'll add it as a (temp) function for my migration. I've removed the |
|
Sorry, my feedback was unclear, so let me expand. We should have both to_unix and to_datetime. They raise as before. There are no bang variants for conversion functions in Elixir. |
|
Hopefully I got it right this time :) |
Having the "from timestamp" helper function is nice if you need to backfill records and want them with a specific timestamp (I do do this sometimes, and just make my own helper). However, I agree with the reasons they stated that it doesn't fit UUIDv7 spec and that making it UUIDv8 makes sense. Probably deserves a discussion outside of this PR. In my opinion, just wait for the postgres folks to make a decision and then copy that, since they have more minds dedicated to these discussions. |
We had convert integer PKs to UUIDv7s and I wanted to set the timestamp based on the Something like this with def generate_uuid(unixtime) do
<<rand_a::12, _::6, rand_b::62>> = :crypto.strong_rand_bytes(10)
raw = <<unixtime::biguint(48), 7::4, rand_a::12, 2::2, rand_b::62>>
Uniq.UUID.format(raw, :default)
endEdit: with def generate_uuid(unixtime) do
<<rand_a::12, _::6, rand_b::62>> = :crypto.strong_rand_bytes(10)
raw = <<unixtime::biguint(48), 7::4, rand_a::12, 2::2, rand_b::62>>
Ecto.UUID.cast!(raw)
end |
You can also replace the |
|
💚 💙 💜 💛 ❤️ |
Adds
timestamp/1,datetime/1, andversion/1helpers for dealing with UUIDs.My main use case is dealing with importing data with existing timestamps but no
UUIDv7 yet. There's currently no way to control the timestamp that's used for
the manual creation of the UUID.
Some parallels with Postgres:
uuid_extract_timestamp->Ecto.UUID.timestampuuid_extract_version->Ecto.UUID.version