rails-uuid-pk 0.5.0 → 0.6.0

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: 615c351b860e9cf6b5cc663f8f1a8d4c5781bb6f7f12d658440f42a3173d8fc0
4
- data.tar.gz: 5973fbaed1fb21337bd3e3377c59f7ba410dd50d8837eb444e0c15ead96ddaaf
3
+ metadata.gz: 93efad36dbc0fb3ab34a2a348a5dad4ae0cb75c3f6f62bf7da4580146fbcccb3
4
+ data.tar.gz: df2a8e217103e1c732c894fd4ab48333e149e8a42a1a92a08bb023d72673ffa3
5
5
  SHA512:
6
- metadata.gz: b9a9adf355b72109ed26fa08586983581aa0b4005bbbbc504782a6f5297198b4852f6b664138b24976f2405c464adeba017d177143b15f95761b2901f2aee336
7
- data.tar.gz: bde5e20373b9d07a9013194f3b90faa01cdad6a63de4598b1d1925c0b80694c8416256290f4410d4c4821c1e7ff51e5bcb9281753bcc12f0ec31fa56c06790be
6
+ metadata.gz: '09577137b7dbdd2b488260130f6afd4473292d06fb6646e141176de11a425f243b95ae3c43b8e69e89a7fcf3cb6b72d3f6cdb3b9794b56bf884c05895e4822de'
7
+ data.tar.gz: 9a5818221c1251cb137e61a30045fb98a0957af248f9f3e5908e5e9fb6f8d74d4de84dfb60d40135d60028d4d679070c8d295174990f8cc990f58752841a33f0
data/CHANGELOG.md CHANGED
@@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v1.0.0.html).
7
7
 
8
+ ## [0.6.0] - 2026-01-12
9
+
10
+ ### Added
11
+ - **Support for `add_reference` and `add_belongs_to`**: Migration helpers now automatically handle foreign key types for these methods as well.
12
+ - **Performance Caching**: Added primary key lookup caching during migrations to improve performance.
13
+
14
+ ### Changed
15
+ - **Improved Migration Helpers**: Enhanced robustness of foreign key type detection by handling more default types (`:bigint`, `:integer`, `nil`).
16
+ - **Refactored Railtie**: Unified UUID type registration for SQLite and MySQL, improving code maintainability.
17
+ - **Better Initialization**: Improved timing of adapter extensions using `ActiveSupport.on_load`.
18
+
8
19
  ## [0.5.0] - 2026-01-10
9
20
 
10
21
  ### Changed
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # rails-uuid-pk
2
2
 
3
- **Dead-simple UUIDv7 primary keys for modern Rails apps**
4
- Works great with **PostgreSQL 18+**, **MySQL 8.0+**, and **SQLite 3.51+** — zero extra extensions required.
3
+ **Dead-simple UUIDv7 primary keys for modern Rails apps**
4
+
5
+ Automatically use UUID v7 for **all primary keys** in Rails applications. Works with PostgreSQL, MySQL, and SQLite — **zero configuration required**. Just add the gem and you're done!
5
6
 
6
7
  [![Gem Version](https://img.shields.io/gem/v/rails-uuid-pk.svg?style=flat-square)](https://rubygems.org/gems/rails-uuid-pk)
7
8
  [![Ruby](https://img.shields.io/badge/ruby-≥3.3-red.svg?style=flat-square)](https://www.ruby-lang.org)
@@ -24,7 +25,7 @@ Works great with **PostgreSQL 18+**, **MySQL 8.0+**, and **SQLite 3.51+** — ze
24
25
  Add to your `Gemfile`:
25
26
 
26
27
  ```ruby
27
- gem "rails-uuid-pk", "~> 0.5"
28
+ gem "rails-uuid-pk", "~> 0.6"
28
29
  ```
29
30
 
30
31
  Then run:
@@ -6,32 +6,51 @@ module RailsUuidPk
6
6
  ref_table = options.delete(:to_table) || ref_name.to_s.pluralize
7
7
 
8
8
  # Only set UUID type if not already explicitly set by user
9
- # Rails sets type: :integer by default, so we override that
10
- # But if user explicitly set a different type, we respect it
11
- if (uuid_primary_key?(ref_table) || (options[:polymorphic] && application_uses_uuid_primary_keys?)) && options[:type] == :integer
9
+ # In Rails, default type is often passed as :integer or :bigint in the options hash
10
+ # depending on the Rails version and whether it is a migration or a table definition.
11
+ # We want to override it to :uuid if the target table uses UUID primary keys.
12
+ current_type = options[:type]
13
+ if (current_type.nil? || current_type == :integer || current_type == :bigint) &&
14
+ (uuid_primary_key?(ref_table) || (options[:polymorphic] && application_uses_uuid_primary_keys?))
12
15
  options[:type] = :uuid
13
16
  end
14
17
 
15
- super
18
+ super(*args, **options)
19
+ end
20
+
21
+ def add_reference(table_name, ref_name, **options)
22
+ ref_table = options.delete(:to_table) || ref_name.to_s.pluralize
23
+
24
+ current_type = options[:type]
25
+ if (current_type.nil? || current_type == :integer || current_type == :bigint) &&
26
+ (uuid_primary_key?(ref_table) || (options[:polymorphic] && application_uses_uuid_primary_keys?))
27
+ options[:type] = :uuid
28
+ end
29
+
30
+ super(table_name, ref_name, **options)
16
31
  end
17
32
 
18
33
  def application_uses_uuid_primary_keys?
19
34
  # Check if the application is configured to use UUID primary keys globally
20
- Rails.application.config.generators.options[:active_record]&.[](:primary_key_type) == :uuid
35
+ defined?(Rails) && Rails.application.config.generators.options[:active_record]&.[](:primary_key_type) == :uuid
21
36
  end
22
37
 
23
38
  alias_method :belongs_to, :references
39
+ alias_method :add_belongs_to, :add_reference
24
40
 
25
41
  private
26
42
 
27
43
  def uuid_primary_key?(table_name)
28
- conn = @conn || @base || (respond_to?(:connection) ? connection : nil)
29
- return false unless conn&.table_exists?(table_name)
44
+ # Cache results for the duration of the migration process to improve performance
45
+ @uuid_pk_cache ||= {}
46
+ return @uuid_pk_cache[table_name] if @uuid_pk_cache.key?(table_name)
30
47
 
31
- pk_column = find_primary_key_column(table_name, conn)
32
- return false unless pk_column
48
+ conn = @conn || @base || (respond_to?(:connection) ? connection : self)
49
+ # Ensure we have a connection-like object that can check for table existence
50
+ return false unless conn.respond_to?(:table_exists?) && conn.table_exists?(table_name)
33
51
 
34
- pk_column.sql_type.downcase.match?(/\A(uuid|varchar\(36\))\z/)
52
+ pk_column = find_primary_key_column(table_name, conn)
53
+ @uuid_pk_cache[table_name] = !!(pk_column && pk_column.sql_type.downcase.match?(/\A(uuid|varchar\(36\))\z/))
35
54
  end
36
55
 
37
56
  def find_primary_key_column(table_name, conn)
@@ -8,54 +8,31 @@ module RailsUuidPk
8
8
 
9
9
  initializer "rails-uuid-pk.configure_type_map", after: "active_record.initialize_database" do
10
10
  ActiveSupport.on_load(:active_record) do
11
- if ActiveRecord::Base.connection.adapter_name == "SQLite"
12
- # Register the UUID type with ActiveRecord
13
- ActiveRecord::Type.register(:uuid, RailsUuidPk::Type::Uuid, adapter: :sqlite3)
14
-
15
- # Map varchar SQL type to our custom UUID type (since that's how UUID columns are stored in SQLite)
16
- ActiveRecord::Base.connection.send(:type_map).register_type(/varchar/i) do |sql_type|
17
- RailsUuidPk::Type::Uuid.new
18
- end
19
-
20
- # Also map "uuid" SQL type to our custom UUID type for direct lookups
21
- ActiveRecord::Base.connection.send(:type_map).register_type "uuid" do |sql_type|
22
- RailsUuidPk::Type::Uuid.new
23
- end
24
- elsif ActiveRecord::Base.connection.adapter_name == "MySQL"
25
- # Register the UUID type with ActiveRecord for MySQL
26
- ActiveRecord::Type.register(:uuid, RailsUuidPk::Type::Uuid, adapter: :mysql2)
27
-
28
- # Map varchar SQL type to our custom UUID type (since that's how UUID columns are stored in MySQL)
29
- ActiveRecord::Base.connection.send(:type_map).register_type(/varchar/i) do |sql_type|
30
- RailsUuidPk::Type::Uuid.new
31
- end
32
-
33
- # Also map "uuid" SQL type to our custom UUID type for direct lookups
34
- ActiveRecord::Base.connection.send(:type_map).register_type "uuid" do |sql_type|
35
- RailsUuidPk::Type::Uuid.new
36
- end
11
+ adapter_name = ActiveRecord::Base.connection.adapter_name
12
+ if %w[SQLite MySQL].include?(adapter_name)
13
+ RailsUuidPk::Railtie.register_uuid_type(adapter_name.downcase.to_sym)
37
14
  end
38
15
  end
39
16
  end
40
17
 
41
18
  initializer "rails-uuid-pk.native_types" do
42
- require "active_record/connection_adapters/sqlite3_adapter"
43
- ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend(RailsUuidPk::Sqlite3AdapterExtension)
44
- end
45
-
46
- initializer "rails-uuid-pk.mysql_native_types" do
47
- begin
48
- require "active_record/connection_adapters/mysql2_adapter"
49
- ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend(RailsUuidPk::Mysql2AdapterExtension)
50
- rescue LoadError
51
- # MySQL adapter not available, skip MySQL-specific initialization
19
+ ActiveSupport.on_load(:active_record) do
20
+ case ActiveRecord::Base.connection.adapter_name
21
+ when "SQLite"
22
+ require "active_record/connection_adapters/sqlite3_adapter"
23
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend(RailsUuidPk::Sqlite3AdapterExtension)
24
+ when "MySQL"
25
+ begin
26
+ require "active_record/connection_adapters/mysql2_adapter"
27
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend(RailsUuidPk::Mysql2AdapterExtension)
28
+ rescue LoadError
29
+ # MySQL adapter not available
30
+ end
31
+ end
52
32
  end
53
33
  end
54
34
 
55
-
56
-
57
35
  initializer "rails-uuid-pk.schema_format" do |app|
58
- # Ensure schema_format is set to :ruby for SQLite (default in Rails)
59
36
  app.config.active_record.schema_format ||= :ruby
60
37
  end
61
38
 
@@ -69,10 +46,21 @@ module RailsUuidPk
69
46
  ActiveSupport.on_load(:active_record) do
70
47
  require "rails_uuid_pk/migration_helpers"
71
48
 
72
- # Include migration helpers for all database adapters
73
49
  ActiveRecord::ConnectionAdapters::TableDefinition.prepend(RailsUuidPk::MigrationHelpers::References)
74
50
  ActiveRecord::ConnectionAdapters::Table.prepend(RailsUuidPk::MigrationHelpers::References)
51
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(RailsUuidPk::MigrationHelpers::References)
75
52
  end
76
53
  end
54
+
55
+ def self.register_uuid_type(adapter)
56
+ ActiveRecord::Type.register(:uuid, RailsUuidPk::Type::Uuid, adapter: adapter)
57
+
58
+ # Get the connection-specific type map
59
+ type_map = ActiveRecord::Base.connection.send(:type_map)
60
+ # Map varchar(36) or varchar SQL type to our custom UUID type
61
+ type_map.register_type(/varchar/i) { RailsUuidPk::Type::Uuid.new }
62
+ # Also map "uuid" SQL type for direct lookups
63
+ type_map.register_type("uuid") { RailsUuidPk::Type::Uuid.new }
64
+ end
77
65
  end
78
66
  end
@@ -1,3 +1,3 @@
1
1
  module RailsUuidPk
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-uuid-pk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joon Lee