-
Notifications
You must be signed in to change notification settings - Fork 1.5k
generate uuid v7 #4681
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
Merged
+103
−18
Merged
generate uuid v7 #4681
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c76f398
generate uuid v7
dkuku 6245180
update schema docs
dkuku 04e34b8
cr suggestions
dkuku d47aa86
typo
dkuku c3977d3
revert to system_time
dkuku 475af88
use options for uuid type
dkuku 3f2a6ef
allow autogenerate to acccept a list of options
dkuku 8563e5b
restore formatting
dkuku 5479ee2
update docs
dkuku 39de72d
update type in uuid
dkuku 5e5ab80
Remove additional autogenerate callback
josevalim 8dd203a
cr suggestions
dkuku File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,20 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defmodule Ecto.UUID do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @moduledoc """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| An Ecto type for UUID strings. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## Autogeneration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This type can be used for any UUID field in your schemas. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| It is used when autogenerating binary IDs in Ecto.Schema. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| By default, autogenerated UUIDs use version 4 (random): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Ecto.Schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @primary_key {:id, :binary_id, autogenerate: true} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| To use UUID v7 (time-ordered) instead: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Ecto.Schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @primary_key {:id, :binary_id, autogenerate: [version: 7]} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Ecto.Type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -15,6 +29,11 @@ defmodule Ecto.UUID do | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @type raw :: <<_::128>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @typedoc """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currently supported option is version, it accepts 4 or 7. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @type options :: [version: 4 | 7] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @doc false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def type, do: :uuid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -47,6 +66,7 @@ defmodule Ecto.UUID do | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @spec cast(t | raw | any) :: {:ok, t} | :error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def cast(uuid) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def cast( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <<a1, a2, a3, a4, a5, a6, a7, a8, ?-, b1, b2, b3, b4, ?-, c1, c2, c3, c4, ?-, d1, d2, d3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| d4, ?-, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -105,6 +125,7 @@ defmodule Ecto.UUID do | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @spec dump(uuid_string :: t | any) :: {:ok, raw} | :error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def dump(uuid_string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def dump( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <<a1, a2, a3, a4, a5, a6, a7, a8, ?-, b1, b2, b3, b4, ?-, c1, c2, c3, c4, ?-, d1, d2, d3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| d4, ?-, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -183,24 +204,49 @@ defmodule Ecto.UUID do | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @default_version 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @default_options version: 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @doc """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generates a random, version 4 UUID. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generates a uuid with the given options. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @spec generate() :: t | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def generate(), do: encode(bingenerate()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @spec generate(options) :: t | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def generate(opts \\ [@default_options]), do: encode(bingenerate(opts)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @doc """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generates a random, version 4 UUID in the binary format. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generates a version 4 uuid in binary format. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @spec bingenerate() :: raw | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def bingenerate() do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def bingenerate(), do: bingenerate(@default_options) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @doc """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generates a uuid with the given options in binary format. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @spec bingenerate(options) :: raw | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def bingenerate(opts) do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case Keyword.get(opts, :version, @default_version) do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 -> bingenerate_v4() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 -> bingenerate_v7() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ -> raise ArgumentError, "unknown UUID version: #{inspect(opts[:version])}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @spec bingenerate() :: raw | |
| def bingenerate() do | |
| def bingenerate(), do: bingenerate(@default_options) | |
| @doc """ | |
| Generates a uuid with the given options in binary format. | |
| """ | |
| @spec bingenerate(options) :: raw | |
| def bingenerate(opts) do | |
| case Keyword.get(opts, :version, @default_version) do | |
| 4 -> bingenerate_v4() | |
| 7 -> bingenerate_v7() | |
| _ -> raise ArgumentError, "unknown UUID version: #{inspect(opts[:version])}" | |
| end | |
| end | |
| @doc """ | |
| Generates a uuid with the given options in binary format. | |
| """ | |
| @spec bingenerate(options) :: raw | |
| def bingenerate(opts \\ []) do | |
| case Keyword.get(opts, :version, @default_version) do | |
| 4 -> bingenerate_v4() | |
| 7 -> bingenerate_v7() | |
| version -> raise ArgumentError, "unknown UUID version: #{inspect(version)}" | |
| end | |
| end |
josevalim marked this conversation as resolved.
Show resolved
Hide resolved
josevalim marked this conversation as resolved.
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably don't need the
@default_optionsand could just use an empty list since you supply a default version withKeyword.get/3