uuid_v7 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 94567f6de44869cfa3bd066891e55473aa7f00fa12034eb312d846d848da15b2
4
- data.tar.gz: afe1f2896353a13a33f35fcd4435e0b1473ea5dcf2aad514fbdd6e7b1442b0cb
3
+ metadata.gz: 4bfb30c0bb6b0b8ba240c7a3cafc4a0108ece68cceef7b076a1f7f81a6906c3c
4
+ data.tar.gz: 9563de770f70af0359ef6a443f6b7c723f8ac5b713448149de13b58a54484729
5
5
  SHA512:
6
- metadata.gz: 5cae446d157e8da70b4c477f3cb608cdace0503a7bd5888400bc262d3735d6ebc269c0f98fb1cb9f04390cf26d3454bc225d558c8802eea8366bd6f82ba9a490
7
- data.tar.gz: '0299d71e9c3148a04eda0b2d5cefa4bb1676e945f3db84862a4a7ce3f2c1d891ac4f70f3557caf6603f386b00e71cc1bf4d81d3e85e7eac283033dd798a452a7'
6
+ metadata.gz: df2e3e982b03512447da3edba9df9166ea97b1e22a3ae0ab1db2b46184331b445db009dc2d819499d03591ac944b29806b2a33568bb8bc7d0e77bda1ac2de617
7
+ data.tar.gz: ce8499ff324be43d751d16d55f9e783739a4c85b67b54854c0e36968d93a1b0cc5f755c2baa0ba905b3435fb17913cdbe2cf74314a4fbd8d1011e004320cfb56
data/.rubocop.yml CHANGED
@@ -6,7 +6,7 @@ require:
6
6
  - rubocop-rspec
7
7
 
8
8
  AllCops:
9
- TargetRubyVersion: 3.2.1
9
+ TargetRubyVersion: 3.3.3
10
10
  NewCops: enable
11
11
 
12
12
  Style/StringLiterals:
data/.rubocop_todo.yml CHANGED
@@ -1,15 +1,20 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2023-12-06 07:57:17 UTC using RuboCop version 1.57.2.
3
+ # on 2024-07-10 09:34:11 UTC using RuboCop version 1.57.2.
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
9
  # Offense count: 1
10
+ # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
11
+ Metrics/AbcSize:
12
+ Max: 23
13
+
14
+ # Offense count: 3
10
15
  # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
11
16
  Metrics/MethodLength:
12
- Max: 64
17
+ Max: 42
13
18
 
14
19
  # Offense count: 2
15
20
  # Configuration parameters: IgnoredMetadata.
@@ -32,17 +37,17 @@ RSpec/ExampleLength:
32
37
  RSpec/MultipleExpectations:
33
38
  Max: 2
34
39
 
35
- # Offense count: 2
40
+ # Offense count: 7
36
41
  # Configuration parameters: AllowedGroups.
37
42
  RSpec/NestedGroups:
38
- Max: 5
43
+ Max: 6
39
44
 
40
45
  # Offense count: 2
41
46
  RSpec/SubjectStub:
42
47
  Exclude:
43
48
  - 'spec/integrations/migrations/mysql/fx_migration_spec.rb'
44
49
 
45
- # Offense count: 3
50
+ # Offense count: 6
46
51
  # This cop supports safe autocorrection (--autocorrect).
47
52
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
48
53
  # URISchemes: http, https
data/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.3] - 2024-07-10
4
+
5
+ - Fix Zeitwerk::NameError
6
+ - Check if created_at is present
7
+
8
+ ## [0.1.2] - 2024-02-09
9
+
10
+ Configure The Behaviours When UUID Is Invalid
11
+
12
+ ```ruby
13
+ UuidV7.configure do |config|
14
+ config.throw_invalid_uuid = false
15
+ end
16
+ ```
17
+
18
+ ```ruby
19
+ record_class.find_by(uuid: "invalid")
20
+ => nil
21
+ ```
22
+
23
+ ```ruby
24
+ UuidV7.configure do |config|
25
+ config.throw_invalid_uuid = true
26
+ end
27
+ ```
28
+
29
+ ```ruby
30
+ record_class.find_by(uuid: "invalid")
31
+ raise_error(UuidV7::Types::InvalidUUID, "invalid is not a valid UUID")
32
+ ```
33
+
3
34
  ## [0.1.1] - 2023-12-11
4
35
 
5
36
  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
@@ -5,4 +5,5 @@ require "uuid_v7"
5
5
  UuidV7.configure do |config|
6
6
  # config.field_name = :id # default :id
7
7
  # config.implicit_inclusion_strategy = true # default true
8
+ # config.throw_invalid_uuid = false # default false
8
9
  end
@@ -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,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UuidV7
4
+ module Patches
5
+ module Mysql
6
+ module FkHelper
7
+ # update_fk_uuid_between_table(parent_table_name: :engines, child_table_name: :pistons)
8
+ def update_fk_uuid_between_table(parent_table_name:, child_table_name:)
9
+ table_name_parent = parent_table_name.to_s.to_sym # :engines
10
+ table_name_child = child_table_name.to_s.to_sym # :pistons
11
+ foreign_key_id = :"#{table_name_parent.to_s.singularize}_id" # :engine_id
12
+ foreign_key_uuid = :"#{table_name_parent.to_s.singularize}_uuid" # :engine_uuid
13
+
14
+ add_column table_name_child, foreign_key_uuid, :binary, limit: 16, null: true # :pistons, :engine_uuid
15
+
16
+ # Update the foreign_key in child table
17
+ connection.execute <<-SQL.squish
18
+ UPDATE #{table_name_child} child
19
+ JOIN #{table_name_parent} parent ON child.#{foreign_key_id} = parent.id
20
+ SET child.#{foreign_key_uuid} = parent.uuid;
21
+ SQL
22
+
23
+ # change_column_null :pistons, :engine_uuid, false
24
+ change_column_null table_name_child, foreign_key_uuid, false
25
+
26
+ # add_index :pistons, :engine_uuid
27
+ add_index table_name_child, foreign_key_uuid
28
+
29
+ # add_foreign_key :pistons, :engines, column: :engine_uuid, primary_key: :uuid, type: :binary
30
+ add_foreign_key table_name_child, table_name_parent, column: foreign_key_uuid, primary_key: :uuid, type: :binary
31
+
32
+ # remove_column table_name_child, foreign_key_id
33
+ # rename_column table_name_child, foreign_key_uuid, foreign_key_id
34
+ end
35
+ end
36
+ end
37
+ end
38
+ 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(
@@ -45,8 +45,15 @@ module UuidV7
45
45
  END
46
46
  SQL
47
47
 
48
+ # Check for NULL created_at values
49
+ result = connection.exec_query(<<~SQL)
50
+ SELECT COUNT(*) AS count FROM #{table_name} WHERE created_at IS NULL;
51
+ SQL
52
+
53
+ raise ActiveRecord::RecordInvalid, "There are records with NULL created_at in #{table_name}" if (result.rows[0][0]).positive?
54
+
48
55
  connection.execute <<~SQL
49
- UPDATE #{table_name} SET #{column_name} = uuid_v7() WHERE #{column_name} IS NULL;
56
+ UPDATE #{table_name} SET #{column_name} = uuid_v7(created_at) WHERE #{column_name} IS NULL;
50
57
  SQL
51
58
 
52
59
  connection.execute <<~SQL
@@ -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
- raise InvalidUUID, "#{value} is not a valid UUID" unless value.match?(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/)
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
 
@@ -73,7 +77,7 @@ module UuidV7
73
77
  def initialize(value)
74
78
  @value = value
75
79
 
76
- super(value)
80
+ super
77
81
  end
78
82
 
79
83
  def hex
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UuidV7
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
data/uuid_v7.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
 
14
14
  spec.homepage = "https://github.com/joel/uuid_v7"
15
15
  spec.license = "MIT"
16
- spec.required_ruby_version = ">= 3.2.1"
16
+ spec.required_ruby_version = ">= 3.3.3"
17
17
 
18
18
  # Specify which files should be added to the gem when it is released.
19
19
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
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.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Azemar
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-11 00:00:00.000000000 Z
11
+ date: 2024-07-10 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
@@ -125,14 +126,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
126
  requirements:
126
127
  - - ">="
127
128
  - !ruby/object:Gem::Version
128
- version: 3.2.1
129
+ version: 3.3.3
129
130
  required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  requirements:
131
132
  - - ">="
132
133
  - !ruby/object:Gem::Version
133
134
  version: '0'
134
135
  requirements: []
135
- rubygems_version: 3.5.0.dev
136
+ rubygems_version: 3.5.11
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.