uuid_v7 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +6 -6
- data/CHANGELOG.md +26 -0
- data/README.md +24 -0
- data/lib/generators/uuid_v7/install/templates/uuid_v7.rb.tt +1 -0
- data/lib/uuid_v7/configuration.rb +2 -1
- data/lib/uuid_v7/patches/mysql/fk_helper.rb +36 -0
- data/lib/uuid_v7/patches/mysql/fx_migration.rb +3 -3
- data/lib/uuid_v7/types/base.rb +5 -1
- data/lib/uuid_v7/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c908c8d88e22891fb147c5663b41cafdc6af30bcac9fa1fbcfabc30b255968c0
|
4
|
+
data.tar.gz: 2c5b7a5d99ae22350e9456b07b51b33a2dab4ac72308cac434264b935c15387b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3377eca301f991088bb5efa01b36f242544dc0a61ac81065461aedf3748d4fde80d1798b19bd10c0e37c188b5387558ac4b215dc624e994ef773f06613533e43
|
7
|
+
data.tar.gz: 77b5f0fbd47744296b8a51ad46c75f0ec999538a58f8765e4cdfa35c6206e8a5b12b0689260e862b7b5359be101dc9754ee8fead17bc8750e6e35ee80ee154a5
|
data/.rubocop_todo.yml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on
|
3
|
+
# on 2024-02-09 11:25:12 UTC using RuboCop version 1.59.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
|
-
# Offense count:
|
9
|
+
# Offense count: 3
|
10
10
|
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
11
11
|
Metrics/MethodLength:
|
12
|
-
Max:
|
12
|
+
Max: 38
|
13
13
|
|
14
14
|
# Offense count: 2
|
15
15
|
# Configuration parameters: IgnoredMetadata.
|
@@ -32,17 +32,17 @@ RSpec/ExampleLength:
|
|
32
32
|
RSpec/MultipleExpectations:
|
33
33
|
Max: 2
|
34
34
|
|
35
|
-
# Offense count:
|
35
|
+
# Offense count: 6
|
36
36
|
# Configuration parameters: AllowedGroups.
|
37
37
|
RSpec/NestedGroups:
|
38
|
-
Max:
|
38
|
+
Max: 6
|
39
39
|
|
40
40
|
# Offense count: 2
|
41
41
|
RSpec/SubjectStub:
|
42
42
|
Exclude:
|
43
43
|
- 'spec/integrations/migrations/mysql/fx_migration_spec.rb'
|
44
44
|
|
45
|
-
# Offense count:
|
45
|
+
# Offense count: 4
|
46
46
|
# This cop supports safe autocorrection (--autocorrect).
|
47
47
|
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
|
48
48
|
# URISchemes: http, https
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,31 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.1.2] - 2024-02-09
|
4
|
+
|
5
|
+
Configure The Behaviours When UUID Is Invalid
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
UuidV7.configure do |config|
|
9
|
+
config.throw_invalid_uuid = false
|
10
|
+
end
|
11
|
+
```
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
record_class.find_by(uuid: "invalid")
|
15
|
+
=> nil
|
16
|
+
```
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
UuidV7.configure do |config|
|
20
|
+
config.throw_invalid_uuid = true
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
record_class.find_by(uuid: "invalid")
|
26
|
+
raise_error(UuidV7::Types::InvalidUUID, "invalid is not a valid UUID")
|
27
|
+
```
|
28
|
+
|
3
29
|
## [0.1.1] - 2023-12-11
|
4
30
|
|
5
31
|
Fix wrong default field name. Change from :uuid to :id
|
data/README.md
CHANGED
@@ -99,6 +99,30 @@ end
|
|
99
99
|
|
100
100
|
**Recommendation:** It's advised to use `:id` as the primary key with Rails for compatibility and convention.
|
101
101
|
|
102
|
+
### Behaviours with Invalid UUIDs
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
UuidV7.configure do |config|
|
106
|
+
config.throw_invalid_uuid = false
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
record_class.find_by(uuid: "invalid")
|
112
|
+
=> nil
|
113
|
+
```
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
UuidV7.configure do |config|
|
117
|
+
config.throw_invalid_uuid = true
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
record_class.find_by(uuid: "invalid")
|
123
|
+
raise_error(UuidV7::Types::InvalidUUID, "invalid is not a valid UUID")
|
124
|
+
```
|
125
|
+
|
102
126
|
### Migrations
|
103
127
|
|
104
128
|
#### Helper for Mysql
|
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
module UuidV7
|
4
4
|
class Configuration
|
5
|
-
attr_accessor :field_name, :implicit_inclusion_strategy
|
5
|
+
attr_accessor :field_name, :implicit_inclusion_strategy, :throw_invalid_uuid
|
6
6
|
|
7
7
|
def initialize
|
8
8
|
self.field_name = :id
|
9
9
|
self.implicit_inclusion_strategy = true
|
10
|
+
self.throw_invalid_uuid = true
|
10
11
|
end
|
11
12
|
end
|
12
13
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Alliantist
|
4
|
+
module Migrations
|
5
|
+
module UuidFkHelper
|
6
|
+
# update_fk_uuid_between_table(parent_table_name: :engines, child_table_name: :pistons)
|
7
|
+
def update_fk_uuid_between_table(parent_table_name:, child_table_name:)
|
8
|
+
table_name_parent = parent_table_name.to_s.to_sym # :engines
|
9
|
+
table_name_child = child_table_name.to_s.to_sym # :pistons
|
10
|
+
foreign_key_id = :"#{table_name_parent.to_s.singularize}_id" # :engine_id
|
11
|
+
foreign_key_uuid = :"#{table_name_parent.to_s.singularize}_uuid" # :engine_uuid
|
12
|
+
|
13
|
+
add_column table_name_child, foreign_key_uuid, :binary, limit: 16, null: true # :pistons, :engine_uuid
|
14
|
+
|
15
|
+
# Update the foreign_key in child table
|
16
|
+
connection.execute <<-SQL.squish
|
17
|
+
UPDATE #{table_name_child} child
|
18
|
+
JOIN #{table_name_parent} parent ON child.#{foreign_key_id} = parent.id
|
19
|
+
SET child.#{foreign_key_uuid} = parent.uuid;
|
20
|
+
SQL
|
21
|
+
|
22
|
+
# change_column_null :pistons, :engine_uuid, false
|
23
|
+
change_column_null table_name_child, foreign_key_uuid, false
|
24
|
+
|
25
|
+
# add_index :pistons, :engine_uuid
|
26
|
+
add_index table_name_child, foreign_key_uuid
|
27
|
+
|
28
|
+
# add_foreign_key :pistons, :engines, column: :engine_uuid, primary_key: :uuid, type: :binary
|
29
|
+
add_foreign_key table_name_child, table_name_parent, column: foreign_key_uuid, primary_key: :uuid, type: :binary
|
30
|
+
|
31
|
+
# remove_column table_name_child, foreign_key_id
|
32
|
+
# rename_column table_name_child, foreign_key_uuid, foreign_key_id
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -15,7 +15,7 @@ module UuidV7
|
|
15
15
|
SQL
|
16
16
|
|
17
17
|
connection.exec_query <<~SQL.squish
|
18
|
-
CREATE FUNCTION uuid_v7 ()
|
18
|
+
CREATE FUNCTION uuid_v7 (time_of_creation DATETIME)
|
19
19
|
RETURNS BINARY(16)
|
20
20
|
LANGUAGE SQL
|
21
21
|
NOT DETERMINISTIC
|
@@ -27,7 +27,7 @@ module UuidV7
|
|
27
27
|
DECLARE currentTime BIGINT;
|
28
28
|
DECLARE buuid BINARY(16);
|
29
29
|
|
30
|
-
SET currentTime = UNIX_TIMESTAMP() * 1000 + FLOOR(MICROSECOND(NOW(6)) / 1000);
|
30
|
+
SET currentTime = UNIX_TIMESTAMP(time_of_creation) * 1000 + FLOOR(MICROSECOND(NOW(6)) / 1000);
|
31
31
|
|
32
32
|
SET uuid = LOWER(
|
33
33
|
CONCAT(
|
@@ -46,7 +46,7 @@ module UuidV7
|
|
46
46
|
SQL
|
47
47
|
|
48
48
|
connection.execute <<~SQL
|
49
|
-
UPDATE #{table_name} SET #{column_name} = uuid_v7() WHERE #{column_name} IS NULL;
|
49
|
+
UPDATE #{table_name} SET #{column_name} = uuid_v7(created_at) WHERE #{column_name} IS NULL;
|
50
50
|
SQL
|
51
51
|
|
52
52
|
connection.execute <<~SQL
|
data/lib/uuid_v7/types/base.rb
CHANGED
@@ -37,7 +37,11 @@ module UuidV7
|
|
37
37
|
# To avoid SQL injection, verify that it looks like a UUID. ActiveRecord
|
38
38
|
# does not explicity escape the Binary data type. escaping is implicit as
|
39
39
|
# the Binary data type always converts its value to a hex string.
|
40
|
-
|
40
|
+
unless value.match?(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/)
|
41
|
+
raise InvalidUUID, "#{value} is not a valid UUID" if UuidV7.configuration.throw_invalid_uuid
|
42
|
+
|
43
|
+
return nil
|
44
|
+
end
|
41
45
|
|
42
46
|
return value if value.is_a?(Data)
|
43
47
|
|
data/lib/uuid_v7/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uuid_v7
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Azemar
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -104,6 +104,7 @@ files:
|
|
104
104
|
- lib/uuid_v7/base.rb
|
105
105
|
- lib/uuid_v7/configuration.rb
|
106
106
|
- lib/uuid_v7/configure.rb
|
107
|
+
- lib/uuid_v7/patches/mysql/fk_helper.rb
|
107
108
|
- lib/uuid_v7/patches/mysql/fx_migration.rb
|
108
109
|
- lib/uuid_v7/railtie.rb
|
109
110
|
- lib/uuid_v7/types/base.rb
|
@@ -132,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
133
|
- !ruby/object:Gem::Version
|
133
134
|
version: '0'
|
134
135
|
requirements: []
|
135
|
-
rubygems_version: 3.5.
|
136
|
+
rubygems_version: 3.5.3
|
136
137
|
signing_key:
|
137
138
|
specification_version: 4
|
138
139
|
summary: Provides UUID V7 support for Ruby on Rails applications using Mysql and Sqlite.
|