uuid_v7 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +24 -0
- data/.rubocop_todo.yml +50 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +143 -0
- data/Rakefile +27 -0
- data/lib/generators/uuid_v7/install/templates/uuid_v7.rb.tt +8 -0
- data/lib/generators/uuid_v7/install_generator.rb +14 -0
- data/lib/generators/uuid_v7/migrations/templates/create_table_migration.rb.tt +46 -0
- data/lib/generators/uuid_v7/migrations_generator.rb +14 -0
- data/lib/uuid_v7/base.rb +11 -0
- data/lib/uuid_v7/configuration.rb +12 -0
- data/lib/uuid_v7/configure.rb +15 -0
- data/lib/uuid_v7/patches/mysql/fx_migration.rb +59 -0
- data/lib/uuid_v7/railtie.rb +35 -0
- data/lib/uuid_v7/types/base.rb +114 -0
- data/lib/uuid_v7/types/mysql_type.rb +10 -0
- data/lib/uuid_v7/types/sqlite_type.rb +16 -0
- data/lib/uuid_v7/version.rb +5 -0
- data/lib/uuid_v7.rb +14 -0
- data/sig/uuid_v7.rbs +4 -0
- data/uuid_v7.gemspec +43 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7f2e1977f048847c67027e4d8b3198694649c1f133c2aaa67860f9e4ac2d88f6
|
4
|
+
data.tar.gz: fc53aad1bee9794d09625d9dacde902740bb64c37cac35880d22303d06ca7c99
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d190d6665642ad22858d65fb97456eb79de4cce1ac73fc1f93af0fe675685087dfa01d68457f42a21152c816c48bb7393a6bf80df6869d58ac8b305482471ef
|
7
|
+
data.tar.gz: 4795012dfad57805620ec1527f2226daab4ffea576efc91bcb507467d0f3d5a9fec3542defeae0405b7070844efd431d8443400a6331e8070c1c52ce1d528161
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
require:
|
4
|
+
- rubocop-performance
|
5
|
+
- rubocop-rake
|
6
|
+
- rubocop-rspec
|
7
|
+
|
8
|
+
AllCops:
|
9
|
+
TargetRubyVersion: 3.2.1
|
10
|
+
NewCops: enable
|
11
|
+
|
12
|
+
Style/StringLiterals:
|
13
|
+
Enabled: true
|
14
|
+
EnforcedStyle: double_quotes
|
15
|
+
|
16
|
+
Style/StringLiteralsInInterpolation:
|
17
|
+
Enabled: true
|
18
|
+
EnforcedStyle: double_quotes
|
19
|
+
|
20
|
+
Style/Documentation:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Lint/SuppressedException:
|
24
|
+
Enabled: false
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2023-12-06 07:57:17 UTC using RuboCop version 1.57.2.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 1
|
10
|
+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
11
|
+
Metrics/MethodLength:
|
12
|
+
Max: 64
|
13
|
+
|
14
|
+
# Offense count: 2
|
15
|
+
# Configuration parameters: IgnoredMetadata.
|
16
|
+
RSpec/DescribeClass:
|
17
|
+
Exclude:
|
18
|
+
- '**/spec/features/**/*'
|
19
|
+
- '**/spec/requests/**/*'
|
20
|
+
- '**/spec/routing/**/*'
|
21
|
+
- '**/spec/system/**/*'
|
22
|
+
- '**/spec/views/**/*'
|
23
|
+
- 'spec/integrations/crud_ar_spec.rb'
|
24
|
+
- 'spec/integrations/migrations/mysql/fx_migration_spec.rb'
|
25
|
+
|
26
|
+
# Offense count: 2
|
27
|
+
# Configuration parameters: CountAsOne.
|
28
|
+
RSpec/ExampleLength:
|
29
|
+
Max: 12
|
30
|
+
|
31
|
+
# Offense count: 2
|
32
|
+
RSpec/MultipleExpectations:
|
33
|
+
Max: 2
|
34
|
+
|
35
|
+
# Offense count: 2
|
36
|
+
# Configuration parameters: AllowedGroups.
|
37
|
+
RSpec/NestedGroups:
|
38
|
+
Max: 5
|
39
|
+
|
40
|
+
# Offense count: 2
|
41
|
+
RSpec/SubjectStub:
|
42
|
+
Exclude:
|
43
|
+
- 'spec/integrations/migrations/mysql/fx_migration_spec.rb'
|
44
|
+
|
45
|
+
# Offense count: 3
|
46
|
+
# This cop supports safe autocorrection (--autocorrect).
|
47
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
|
48
|
+
# URISchemes: http, https
|
49
|
+
Layout/LineLength:
|
50
|
+
Max: 155
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at joel.azemar@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Joel Azemar
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
# UUID V7 for Ruby on Rails
|
2
|
+
|
3
|
+
## Introduction
|
4
|
+
`uuid_v7` is a RubyGem specifically designed to provide UUID V7 support for Mysql and Sqlite databases in Ruby on Rails applications. This gem is particularly useful because these databases lack a native type for supporting UUIDs.
|
5
|
+
|
6
|
+
**Note:** PostgreSQL users do not require this gem as PostgreSQL supports UUID natively.
|
7
|
+
|
8
|
+
## Features
|
9
|
+
- Converts UUID V7 CHAR(36) to a more efficient CHAR(32) and stores it in BINARY(16) for performance enhancement.
|
10
|
+
- Integrates seamlessly with `ActiveRecord::Base`, enabling UUID as the primary key for models.
|
11
|
+
- Migration helper for easy transition to UUIDs in existing Rails applications, specifically tailored for Mysql.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'uuid_v7'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
bundle install
|
25
|
+
```
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
```bash
|
30
|
+
gem install uuid_v7
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
### Basic Setup
|
36
|
+
|
37
|
+
Once installed, `uuid_v7` extends `ActiveRecord::Base` to use UUID as the primary key.
|
38
|
+
|
39
|
+
### Foreign Keys
|
40
|
+
|
41
|
+
Declare foreign key associations in your models as follows:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class Author < ActiveRecord::Base
|
45
|
+
has_many :books
|
46
|
+
end
|
47
|
+
|
48
|
+
class Book < ActiveRecord::Base
|
49
|
+
attribute :author_id, :uuid_v7
|
50
|
+
belongs_to :author
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
## Configuration
|
55
|
+
|
56
|
+
### Custom Primary Key
|
57
|
+
|
58
|
+
If your primary key is not `:id`, run:
|
59
|
+
|
60
|
+
```bash
|
61
|
+
rails generate uuid_v7:install
|
62
|
+
```
|
63
|
+
|
64
|
+
And configure:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
UuidV7.configure do |config|
|
68
|
+
config.field_name = :uuid
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
### Implicit Inclusion Strategy
|
73
|
+
|
74
|
+
By defaut the primary key type is overrided, if you want to prevent that behaviour toogle the strategy.
|
75
|
+
|
76
|
+
in `config/initializers/uuid_v7.rb``
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
UuidV7.configure do |config|
|
80
|
+
config.implicit_inclusion_strategy = false
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
Then add manually to the model you want.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
class Author < ActiveRecord::Base
|
88
|
+
attribute :id, :uuid_v7, default: -> { SecureRandom.uuid_v7 }
|
89
|
+
|
90
|
+
has_many :books
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
**Recommendation:** It's advised to use `:id` as the primary key with Rails for compatibility and convention.
|
95
|
+
|
96
|
+
### Migrations
|
97
|
+
|
98
|
+
#### Helper for Mysql
|
99
|
+
|
100
|
+
`uuid_v7` provides a migration helper for Mysql. To add a UUID field to a model, run:
|
101
|
+
|
102
|
+
```bash
|
103
|
+
rails generate migration AddUuidToUser uuid:binary
|
104
|
+
```
|
105
|
+
|
106
|
+
Then use the helper in your migration file:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
class AddUuidToUser < ActiveRecord::Migration[7.1]
|
110
|
+
def change
|
111
|
+
# Add the field :uuid
|
112
|
+
populate_uuid_field(table_name: :users, column_name: :uuid)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
#### Custom Migration Template
|
118
|
+
|
119
|
+
To override the default `:id` implementation in new migrations:
|
120
|
+
|
121
|
+
```bash
|
122
|
+
rails generate uuid_v7:migrations
|
123
|
+
```
|
124
|
+
|
125
|
+
A modified migration template will be available under `lib/templates/active_record/migration/create_table_migration.rb`.
|
126
|
+
|
127
|
+
Example of a generated model migration:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
class AddAuthor < ActiveRecord::Migration[7.1]
|
131
|
+
def change
|
132
|
+
create_table :authors, id: false do |t|
|
133
|
+
t.binary :id, limit: 16, null: false, index: { unique: true }, primary_key: true
|
134
|
+
t.string :name
|
135
|
+
t.timestamps
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
```
|
140
|
+
|
141
|
+
## Contributing
|
142
|
+
|
143
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/alliantist/uuid_v7].
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
ENV["DATABASE"] ||= "sqlite3"
|
7
|
+
|
8
|
+
case ENV.fetch("DATABASE", nil)
|
9
|
+
when "mysql"
|
10
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
11
|
+
task.exclude_pattern = "spec/integrations/migrations/sqlite3/*_spec.rb"
|
12
|
+
end
|
13
|
+
when "sqlite3"
|
14
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
15
|
+
task.exclude_pattern = "spec/integrations/migrations/mysql/*_spec.rb"
|
16
|
+
end
|
17
|
+
else
|
18
|
+
raise "Unsupported database: #{ENV.fetch("DATABASE", nil)}"
|
19
|
+
end
|
20
|
+
|
21
|
+
require "rubocop/rake_task"
|
22
|
+
|
23
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
24
|
+
task.options = ["-A"] # auto_correct
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: %i[spec rubocop]
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UuidV7
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path("install/templates", __dir__)
|
7
|
+
|
8
|
+
desc "rails generate uuid_v7:install"
|
9
|
+
def copy_initializer_file
|
10
|
+
template "uuid_v7.rb.tt", "config/initializers/uuid_v7.rb"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Source: https://github.com/rails/rails/blob/v7.1.2/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt
|
2
|
+
#
|
3
|
+
# This template comes from your local project under lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt.
|
4
|
+
# It is based on Rails 7.1. If your Rails version diverges, please note that the generator is outdated, and changes might be made.
|
5
|
+
#
|
6
|
+
class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
7
|
+
def change
|
8
|
+
<% if ["SQLite", "Mysql2"].include?(ActiveRecord::Base.connection.adapter_name) -%>
|
9
|
+
create_table :<%= table_name %>, id: false do |t|
|
10
|
+
t.binary :<%= UuidV7.configuration.field_name %>, limit: 16, null: false, index: { unique: true }, primary_key: true
|
11
|
+
<% else -%>
|
12
|
+
create_table :<%= table_name %><%= primary_key_type %> do |t|
|
13
|
+
<% end -%>
|
14
|
+
<% attributes.each do |attribute| -%>
|
15
|
+
<% if attribute.password_digest? -%>
|
16
|
+
t.string :password_digest<%= attribute.inject_options %>
|
17
|
+
<% elsif attribute.token? -%>
|
18
|
+
t.string :<%= attribute.name %><%= attribute.inject_options %>
|
19
|
+
<% elsif attribute.reference? -%>
|
20
|
+
<% if ["SQLite", "Mysql2"].include?(ActiveRecord::Base.connection.adapter_name) -%>
|
21
|
+
<% if UuidV7.configuration.field_name == :id -%>
|
22
|
+
t.references :<%= attribute.name %>, foreign_key: true, index: true, type: :binary, limit: 16, null: false
|
23
|
+
<% else -%>
|
24
|
+
t.binary :<%= attribute.name %>_id, null: false, limit: 16
|
25
|
+
t.foreign_key :<%= attribute.name %>s, column: :<%= attribute.name %>_id, primary_key: :<%= UuidV7.configuration.field_name %>
|
26
|
+
t.index :<%= attribute.name %>_id
|
27
|
+
<% end -%>
|
28
|
+
<% else -%>
|
29
|
+
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %><%= foreign_key_type %>
|
30
|
+
<% end -%>
|
31
|
+
<% elsif !attribute.virtual? -%>
|
32
|
+
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
|
33
|
+
<% end -%>
|
34
|
+
<% end -%>
|
35
|
+
<% if options[:timestamps] %>
|
36
|
+
t.timestamps
|
37
|
+
<% end -%>
|
38
|
+
end
|
39
|
+
<% attributes.select(&:token?).each do |attribute| -%>
|
40
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
|
41
|
+
<% end -%>
|
42
|
+
<% attributes_with_index.each do |attribute| -%>
|
43
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
44
|
+
<% end -%>
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UuidV7
|
4
|
+
module Generators
|
5
|
+
class MigrationsGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path("migrations/templates", __dir__)
|
7
|
+
|
8
|
+
desc "rails generate uuid_v7:migrations"
|
9
|
+
def copy_initializer_file
|
10
|
+
copy_file "create_table_migration.rb.tt", "lib/templates/active_record/migration/create_table_migration.rb.tt"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/uuid_v7/base.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UuidV7
|
4
|
+
module Patches
|
5
|
+
module Mysql
|
6
|
+
module FxMigration
|
7
|
+
def populate_uuid_field(table_name:, column_name:)
|
8
|
+
table_name = table_name.to_s.to_sym
|
9
|
+
column_name = column_name.to_s.to_sym
|
10
|
+
|
11
|
+
raise ArgumentError, "Column #{column_name} does not exist on table #{table_name}" unless column_exists?(table_name, column_name)
|
12
|
+
|
13
|
+
connection.execute <<~SQL
|
14
|
+
DROP FUNCTION IF EXISTS uuid_v7;
|
15
|
+
SQL
|
16
|
+
|
17
|
+
connection.exec_query <<~SQL.squish
|
18
|
+
CREATE FUNCTION uuid_v7 ()
|
19
|
+
RETURNS BINARY(16)
|
20
|
+
LANGUAGE SQL
|
21
|
+
NOT DETERMINISTIC
|
22
|
+
NO SQL
|
23
|
+
SQL SECURITY DEFINER
|
24
|
+
BEGIN
|
25
|
+
DECLARE uuid CHAR(36);
|
26
|
+
DECLARE undashed_uuid CHAR(32);
|
27
|
+
DECLARE currentTime BIGINT;
|
28
|
+
DECLARE buuid BINARY(16);
|
29
|
+
|
30
|
+
SET currentTime = UNIX_TIMESTAMP() * 1000 + FLOOR(MICROSECOND(NOW(6)) / 1000);
|
31
|
+
|
32
|
+
SET uuid = LOWER(
|
33
|
+
CONCAT(
|
34
|
+
LPAD(HEX(currentTime), 12, '0'), '-7',
|
35
|
+
LPAD(HEX(FLOOR(RAND() * 0x1000)), 3, '0'), '-',
|
36
|
+
HEX(0x8000 | FLOOR(RAND() * 0x4000)), '-',
|
37
|
+
LPAD(HEX(FLOOR(RAND() * 0x1000000000000)), 12, '0')
|
38
|
+
)
|
39
|
+
);
|
40
|
+
|
41
|
+
SET undashed_uuid = REPLACE(uuid, '-', '');
|
42
|
+
SET buuid = UNHEX(undashed_uuid);
|
43
|
+
|
44
|
+
RETURN buuid;
|
45
|
+
END
|
46
|
+
SQL
|
47
|
+
|
48
|
+
connection.execute <<~SQL
|
49
|
+
UPDATE #{table_name} SET #{column_name} = uuid_v7() WHERE #{column_name} IS NULL;
|
50
|
+
SQL
|
51
|
+
|
52
|
+
connection.execute <<~SQL
|
53
|
+
DROP FUNCTION IF EXISTS uuid_v7;
|
54
|
+
SQL
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/railtie"
|
4
|
+
require "active_support"
|
5
|
+
|
6
|
+
module UuidV7
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
initializer "uuid_v7.initialize" do
|
9
|
+
case ActiveRecord::Base.connection.adapter_name
|
10
|
+
when "SQLite"
|
11
|
+
require_relative "types/sqlite_type"
|
12
|
+
ActiveRecord::Type.register(:uuid_v7, UuidV7::Types::SqliteType, adapter: :sqlite3)
|
13
|
+
when "Mysql2"
|
14
|
+
require_relative "types/mysql_type"
|
15
|
+
ActiveRecord::Type.register(:uuid_v7, UuidV7::Types::MysqlType, adapter: :mysql2)
|
16
|
+
|
17
|
+
ActiveSupport.on_load(:active_record) do
|
18
|
+
ActiveRecord::Migration.include(Patches::Mysql::FxMigration) unless ActiveRecord::Migration.include?(Patches::Mysql::FxMigration)
|
19
|
+
end
|
20
|
+
when "PostgreSQL"
|
21
|
+
raise NotImplementedError, "PostgreSQL support native UUID type, no need for this gem."
|
22
|
+
else
|
23
|
+
raise NotImplementedError, "Adapter #{ActiveRecord::Base.connection.adapter_name} not supported"
|
24
|
+
end
|
25
|
+
|
26
|
+
Rails.application.config.after_initialize do
|
27
|
+
if UuidV7.configuration.implicit_inclusion_strategy
|
28
|
+
ActiveSupport.on_load(:active_record) do
|
29
|
+
include UuidV7::Base unless include?(UuidV7::Base)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_model/type"
|
4
|
+
|
5
|
+
module UuidV7
|
6
|
+
module Types
|
7
|
+
class InvalidUUID < StandardError; end
|
8
|
+
|
9
|
+
class Base < ActiveModel::Type::Binary
|
10
|
+
def type
|
11
|
+
:uuid_v7
|
12
|
+
end
|
13
|
+
|
14
|
+
# Type casts a value from user input (e.g. from a setter). This value may
|
15
|
+
# be a string from the form builder, or a ruby object passed to a setter.
|
16
|
+
# There is currently no way to differentiate between which source it came
|
17
|
+
# from.
|
18
|
+
#
|
19
|
+
# The return value of this method will be returned from
|
20
|
+
# ActiveRecord::AttributeMethods::Read#read_attribute. See also:
|
21
|
+
# Value#cast_value.
|
22
|
+
#
|
23
|
+
# +value+ The raw input, as provided to the attribute setter.
|
24
|
+
def cast(value)
|
25
|
+
return unless value
|
26
|
+
|
27
|
+
super(add_dashes(value.to_s)) # super will encode the value as binary (Encoding::BINARY)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Casts a value from the ruby type to a type that the database knows how
|
31
|
+
# to understand. The returned value from this method should be a
|
32
|
+
# +String+, +Numeric+, +Date+, +Time+, +Symbol+, +true+, +false+, or
|
33
|
+
# +nil+.
|
34
|
+
def serialize(value)
|
35
|
+
return unless value
|
36
|
+
|
37
|
+
# To avoid SQL injection, verify that it looks like a UUID. ActiveRecord
|
38
|
+
# does not explicity escape the Binary data type. escaping is implicit as
|
39
|
+
# the Binary data type always converts its value to a hex string.
|
40
|
+
raise InvalidUUID, "#{value} is not a valid UUID" unless value.match?(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/)
|
41
|
+
|
42
|
+
return value if value.is_a?(Data)
|
43
|
+
|
44
|
+
Data.new(strip_dashes(value))
|
45
|
+
end
|
46
|
+
|
47
|
+
# Converts a value from database input to the appropriate ruby type. The
|
48
|
+
# return value of this method will be returned from
|
49
|
+
# ActiveRecord::AttributeMethods::Read#read_attribute. The default
|
50
|
+
# implementation just calls Value#cast.
|
51
|
+
#
|
52
|
+
# +value+ The raw input, as provided from the database.
|
53
|
+
|
54
|
+
# ActiveModel::Type::Binary implements this method as:
|
55
|
+
# def deserialize(value)
|
56
|
+
# cast(value)
|
57
|
+
# end
|
58
|
+
def deserialize(value)
|
59
|
+
return unless value
|
60
|
+
|
61
|
+
# If the value is a binary data, convert it to a hex string, and call cast(value)
|
62
|
+
if value.is_a?(String) && value.encoding == Encoding::ASCII_8BIT
|
63
|
+
super(value.unpack1("H*"))
|
64
|
+
else
|
65
|
+
super # foward to cast(value)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# We need to override the hex method to avoid unpacking when quoting
|
70
|
+
# ActiveRecord calls hex method when quoting a value.
|
71
|
+
# https://github.com/rails/rails/blob/7-1-stable/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb#L57
|
72
|
+
class Data < ActiveModel::Type::Binary::Data
|
73
|
+
def initialize(value)
|
74
|
+
@value = value
|
75
|
+
|
76
|
+
super(value)
|
77
|
+
end
|
78
|
+
|
79
|
+
def hex
|
80
|
+
@value
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
# A UUID consists of 5 groups of characters.
|
87
|
+
# 8 chars - 4 chars - 4 chars - 4 chars - 12 characters
|
88
|
+
#
|
89
|
+
# This function re-introduces the dashes since we removed them during
|
90
|
+
# serialization, so:
|
91
|
+
#
|
92
|
+
# add_dashes("2b4a233152694c6e9d1e098804ab812b")
|
93
|
+
# => "2b4a2331-5269-4c6e-9d1e-098804ab812b"
|
94
|
+
#
|
95
|
+
def add_dashes(uuid)
|
96
|
+
return uuid if uuid.include?("-")
|
97
|
+
|
98
|
+
[uuid[0..7], uuid[8..11], uuid[12..15], uuid[16..19], uuid[20..]].join("-")
|
99
|
+
end
|
100
|
+
|
101
|
+
# A UUID has 4 dashes is displayed with 4 dashes at the same place all
|
102
|
+
# the time. So they don't add anything semantically. We can safely remove
|
103
|
+
# them before storing to the database, and re-add them whenever we
|
104
|
+
# retrieved a value from the database.
|
105
|
+
#
|
106
|
+
# strip_dashes("2b4a2331-5269-4c6e-9d1e-098804ab812b")
|
107
|
+
# => "2b4a233152694c6e9d1e098804ab812b"
|
108
|
+
#
|
109
|
+
def strip_dashes(uuid)
|
110
|
+
uuid.delete("-")
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "base"
|
4
|
+
|
5
|
+
module UuidV7
|
6
|
+
module Types
|
7
|
+
class SqliteType < Base
|
8
|
+
# SQLite does not return binary data, it returns a String, no need to unpack it.
|
9
|
+
def deserialize(value)
|
10
|
+
return unless value
|
11
|
+
|
12
|
+
cast(value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/uuid_v7.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "zeitwerk"
|
4
|
+
loader = Zeitwerk::Loader.for_gem
|
5
|
+
loader.ignore("#{__dir__}/generators")
|
6
|
+
loader.ignore("#{__dir__}/uuid_v7/railtie.rb")
|
7
|
+
loader.setup
|
8
|
+
|
9
|
+
require "uuid_v7/railtie" if defined?(Rails)
|
10
|
+
|
11
|
+
module UuidV7
|
12
|
+
extend Configure
|
13
|
+
class Error < StandardError; end
|
14
|
+
end
|
data/sig/uuid_v7.rbs
ADDED
data/uuid_v7.gemspec
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/uuid_v7/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "uuid_v7"
|
7
|
+
spec.version = UuidV7::VERSION
|
8
|
+
spec.authors = ["Joel Azemar"]
|
9
|
+
spec.email = ["joel.azemar@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Provides UUID V7 support for Ruby on Rails applications using Mysql and Sqlite."
|
12
|
+
spec.description = "uuid_v7 enables UUID V7 as primary keys in Rails, optimized for Mysql and Sqlite, with efficient migration and integration features."
|
13
|
+
|
14
|
+
spec.homepage = "https://github.com/alliantist/uuid_v7"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.required_ruby_version = ">= 3.2.1"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(__dir__) do
|
21
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
22
|
+
(File.expand_path(f) == __FILE__) ||
|
23
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
# Uncomment to register a new dependency of your gem
|
31
|
+
|
32
|
+
spec.add_dependency "activemodel", "~> 7.0"
|
33
|
+
spec.add_dependency "activesupport", "~> 7.0"
|
34
|
+
spec.add_dependency "base58", "~> 0.2.3" # Used to convert between integers and base58 strings.
|
35
|
+
spec.add_dependency "securerandom", "~> 0.3.0" # Needed for SecureRandom.uuid_v7 until we upgrade to Ruby 3.3
|
36
|
+
spec.add_dependency "zeitwerk", "~> 2.5" # Used to autoload classes
|
37
|
+
|
38
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
39
|
+
|
40
|
+
# For more information and examples about making a new gem, check out our
|
41
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
42
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uuid_v7
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel Azemar
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '7.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '7.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: base58
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.2.3
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.2.3
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: securerandom
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.3.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.3.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: zeitwerk
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.5'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.5'
|
83
|
+
description: uuid_v7 enables UUID V7 as primary keys in Rails, optimized for Mysql
|
84
|
+
and Sqlite, with efficient migration and integration features.
|
85
|
+
email:
|
86
|
+
- joel.azemar@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".rubocop_todo.yml"
|
94
|
+
- CHANGELOG.md
|
95
|
+
- CODE_OF_CONDUCT.md
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- lib/generators/uuid_v7/install/templates/uuid_v7.rb.tt
|
100
|
+
- lib/generators/uuid_v7/install_generator.rb
|
101
|
+
- lib/generators/uuid_v7/migrations/templates/create_table_migration.rb.tt
|
102
|
+
- lib/generators/uuid_v7/migrations_generator.rb
|
103
|
+
- lib/uuid_v7.rb
|
104
|
+
- lib/uuid_v7/base.rb
|
105
|
+
- lib/uuid_v7/configuration.rb
|
106
|
+
- lib/uuid_v7/configure.rb
|
107
|
+
- lib/uuid_v7/patches/mysql/fx_migration.rb
|
108
|
+
- lib/uuid_v7/railtie.rb
|
109
|
+
- lib/uuid_v7/types/base.rb
|
110
|
+
- lib/uuid_v7/types/mysql_type.rb
|
111
|
+
- lib/uuid_v7/types/sqlite_type.rb
|
112
|
+
- lib/uuid_v7/version.rb
|
113
|
+
- sig/uuid_v7.rbs
|
114
|
+
- uuid_v7.gemspec
|
115
|
+
homepage: https://github.com/alliantist/uuid_v7
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata:
|
119
|
+
rubygems_mfa_required: 'true'
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 3.2.1
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubygems_version: 3.5.0.dev
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Provides UUID V7 support for Ruby on Rails applications using Mysql and Sqlite.
|
139
|
+
test_files: []
|