-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add support for UUID keys #2
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
Open
chiperific
wants to merge
5
commits into
rubymonolith:main
Choose a base branch
from
chiperific:feat-add-support-for-uuid-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -214,6 +214,7 @@ GEM | |
| zeitwerk (2.7.4) | ||
|
|
||
| PLATFORMS | ||
| arm64-darwin-23 | ||
| arm64-darwin-24 | ||
| x86_64-linux | ||
|
|
||
|
|
||
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
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,11 +1,24 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "zlib" | ||
| require_relative "key/integer" | ||
| require_relative "key/uuid" | ||
|
|
||
| module FixtureBot | ||
| module Key | ||
| def self.generate(table_name, record_name) | ||
| Zlib.crc32("#{table_name}:#{record_name}") & 0x7FFFFFFF | ||
| def self.resolve(value) | ||
| case value | ||
| when :integer then Integer.new | ||
| when :uuid then UUID.new | ||
| when Symbol | ||
| raise ArgumentError, | ||
| "unsupported primary key type: #{value.inspect} (must be :integer, :uuid, or a FixtureBot::Key object that responds to #generate)" | ||
| else | ||
| unless value.respond_to?(:generate) | ||
| raise ArgumentError, | ||
| "primary_key_type must be :integer, :uuid, or respond to #generate" | ||
| end | ||
| value | ||
| end | ||
| end | ||
| end | ||
| end |
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "zlib" | ||
|
|
||
| module FixtureBot | ||
| module Key | ||
| class Integer | ||
| def generate(table_name, record_name) | ||
| Zlib.crc32("#{table_name}:#{record_name}") & 0x7FFFFFFF | ||
| end | ||
| end | ||
| end | ||
| end |
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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "digest/sha1" | ||
|
|
||
| module FixtureBot | ||
| module Key | ||
| class UUID | ||
| # RFC 4122 URL namespace UUID, used as the base namespace for UUID v5 generation. | ||
| URL_NAMESPACE = "6ba7b811-9dad-11d1-80b4-00c04fd430c8" | ||
|
|
||
| def generate(table_name, record_name) | ||
| uuid_v5(URL_NAMESPACE, "fixturebot:#{table_name}:#{record_name}") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def uuid_v5(namespace_uuid, name) | ||
| namespace_bytes = [namespace_uuid.tr("-", "")].pack("H32") | ||
| hash = Digest::SHA1.digest(namespace_bytes + name.to_s) | ||
|
|
||
| bytes = hash.bytes[0, 16] | ||
| bytes[6] = (bytes[6] & 0x0F) | 0x50 # Version 5 | ||
| bytes[8] = (bytes[8] & 0x3F) | 0x80 # RFC 4122 variant | ||
|
|
||
| hex = bytes.map { |b| "%02x" % b }.join | ||
| "#{hex[0, 8]}-#{hex[8, 4]}-#{hex[12, 4]}-#{hex[16, 4]}-#{hex[20, 12]}" | ||
| end | ||
| end | ||
| end | ||
| end |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Allows users to hardcode primary keys in fixture files