rails-uuid-pk 0.2.0 → 0.3.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: 9a1e3ae77afb9d7050fab7a90ea7596b18894cb00006e36e0dd391bc3a1b1194
4
- data.tar.gz: b28a4b0918db5a7285ad8db6af0e0243a862f781c6476daf1954c282b97475ae
3
+ metadata.gz: d9552cdab44f5a5a6a4b699e12dc31ee13917ff74f0752f32e823dffed24e2e0
4
+ data.tar.gz: 6ddd7862945d8b13939be92990241539d93eaf754d6c47f2733d66eb2e29597e
5
5
  SHA512:
6
- metadata.gz: 63f0882a6e7e8f50b35811b81b9efbd75a25ebaa7eadf2b20a75610a14eeb25a4e10635bd39565756d428585609989aabf418dd5486bc361199f49d0fa9eaffb
7
- data.tar.gz: 7055f26c0c87e83e3af48e3cb08b75cc2582af0de4c3c5706af1ca595468bbf77ba4fa3a9eddc2da05c4f471233c5d1d3debfed2cb0e935ecd1679fb41b97d51
6
+ metadata.gz: 72fb9c050567562c0d01793d801d7633d45b26851734cb069b2e70ee74d8bf3387e515f50cb771fdbb46f8ad9b4cb767058739bad45050134ba710a8f6290201
7
+ data.tar.gz: ed3b396026fe6edc6485720d6d80af4d5fd11138652fea133515fcb8d931d9d38cf981553a46689121201922ee41b6fad5829c42c06783109b930b06acf41fda
data/CHANGELOG.md CHANGED
@@ -5,6 +5,23 @@ 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.3.0] - 2026-01-09
9
+
10
+ ### Added
11
+ - Migration helpers that automatically detect and set UUID foreign key types
12
+ - Smart `references` method that inspects database schema to determine primary key types
13
+ - Automatic UUID type assignment for polymorphic associations when using global UUID primary keys
14
+ - Comprehensive test coverage for migration helper functionality
15
+
16
+ ### Changed
17
+ - Eliminated need for manual editing of Action Text and Active Storage migrations
18
+ - Updated install generator message to reflect automatic foreign key type handling
19
+ - Updated README.md to document the improved zero-config experience
20
+ - Changed "Zero config after install" status from "Yes (mostly)" to "Yes"
21
+
22
+ ### Removed
23
+ - Manual migration editing requirement for Action Text & Active Storage compatibility
24
+
8
25
  ## [0.2.0] - 2026-01-07
9
26
 
10
27
  ### Added
data/README.md CHANGED
@@ -13,6 +13,8 @@ Works great with **PostgreSQL 18+** and **SQLite 3.51+** — zero extra extensio
13
13
  - Automatically sets `:uuid` as default primary key type
14
14
  - Adds reliable `before_create` callback for UUIDv7 generation
15
15
  - Works perfectly on **both PostgreSQL 18+** and **SQLite** (and older PostgreSQL versions too)
16
+ - **PostgreSQL**: Uses native `UUID` column types with full database support
17
+ - **SQLite**: Uses `VARCHAR(36)` with custom ActiveRecord type handling
16
18
  - Zero database extensions needed
17
19
  - Minimal and maintainable — no monkey-patching hell
18
20
 
@@ -21,7 +23,7 @@ Works great with **PostgreSQL 18+** and **SQLite 3.51+** — zero extra extensio
21
23
  Add to your `Gemfile`:
22
24
 
23
25
  ```ruby
24
- gem "rails-uuid-pk", "~> 0.1"
26
+ gem "rails-uuid-pk", "~> 0.3"
25
27
  ```
26
28
 
27
29
  Then run:
@@ -57,8 +59,6 @@ User.create!(name: "Alice") # ← id is automatically a proper UUIDv7
57
59
 
58
60
  ### Action Text & Active Storage
59
61
 
60
- **These Rails engines do NOT automatically respect the `primary_key_type: :uuid` setting** when generating their install migrations.
61
-
62
62
  When you run:
63
63
 
64
64
  ```bash
@@ -66,37 +66,25 @@ rails action_text:install
66
66
  rails active_storage:install
67
67
  ```
68
68
 
69
- You **MUST** manually edit the generated migration and change:
70
-
71
- ```ruby
72
- t.references :record, null: false, polymorphic: true, index: false
73
- # to
74
- t.references :record, null: false, polymorphic: true, index: false, type: :uuid
75
- ```
76
-
77
- Same applies to `active_storage_attachments.record_id`.
78
-
79
- **Without this change** you will get:
69
+ The generated migrations will **automatically use the correct foreign key types** when referencing tables with UUID primary keys. No manual editing required!
80
70
 
81
- - `PG::DatatypeMismatch` errors
82
- - Duplicate key violations on uniqueness indexes
83
- - Association failures
71
+ This works because rails-uuid-pk includes smart migration helpers that detect the primary key type of referenced tables and automatically set `type: :uuid` for foreign keys.
84
72
 
85
- This is a **long-standing Rails limitation** (still present in Rails 8+).
86
- The gem shows a big warning during installation — but double-check every time you install these engines.
73
+ ### Polymorphic associations
87
74
 
88
- ### Other polymorphic associations
75
+ **Polymorphic associations** (like Action Text's `record` references) will automatically use the correct foreign key type based on the referenced table's primary key. If the parent model uses UUID primary keys, the foreign key will be UUID type.
89
76
 
90
- Any **custom polymorphic** association you create manually should also explicitly use `type: :uuid` if the parent models use UUID primary keys.
77
+ For **custom polymorphic associations**, the migration helpers will also automatically detect and set the correct type:
91
78
 
92
79
  ```ruby
93
- # Good
94
- has_many :comments, as: :commentable, foreign_key: { type: :uuid }
95
-
96
- # Risky (may cause type mismatch)
97
- has_many :comments, as: :commentable
80
+ # This will automatically use type: :uuid if the parent models have UUID primary keys
81
+ create_table :comments do |t|
82
+ t.references :commentable, polymorphic: true
83
+ end
98
84
  ```
99
85
 
86
+ No manual `type: :uuid` specification needed!
87
+
100
88
  ## Features / Trade-offs
101
89
 
102
90
  | Feature | Status | Notes |
@@ -105,7 +93,7 @@ has_many :comments, as: :commentable
105
93
  | PostgreSQL 18+ native `uuidv7()` | Not used | Fallback approach — more universal, no extensions needed |
106
94
  | SQLite support | Full | No native function → app-side generation works great |
107
95
  | Index locality / performance | Very good | UUIDv7 is monotonic → almost as good as sequential IDs |
108
- | Zero config after install | Yes (mostly) | Except Action Text / Active Storage migrations |
96
+ | Zero config after install | Yes | Migration helpers automatically handle foreign key types |
109
97
  | Works with Rails 7.1 – 8+ | Yes | Tested conceptually up to Rails 8.1+ |
110
98
 
111
99
  ## Why not use native PostgreSQL `uuidv7()`?
@@ -13,18 +13,15 @@ module RailsUuidPk
13
13
  def show_next_steps
14
14
  say "\nrails-uuid-pk was successfully installed!", :green
15
15
 
16
- say "\n⚠️ IMPORTANT - Action Text & Active Storage compatibility", :red
16
+ say "\n Action Text & Active Storage compatibility", :green
17
17
  say "─────────────────────────────────────────────────────────────"
18
- say "When you run any of these commands:"
18
+ say "Migration helpers now automatically handle foreign key types!"
19
+ say "When you run:"
19
20
  say " rails action_text:install"
20
21
  say " rails active_storage:install"
21
22
  say ""
22
- say "You **MUST** manually edit the generated migration and add:"
23
- say ""
24
- say " t.references :record, null: false, polymorphic: true, index: false, type: :uuid"
25
- say ""
26
- say "Without this change you'll get type mismatch errors (bigint vs uuid)."
27
- say "This is a Rails limitation - see Rails guides for details."
23
+ say "The generated migrations will automatically use the correct UUID types"
24
+ say "for foreign keys. No manual editing required!"
28
25
  say "─────────────────────────────────────────────────────────────\n"
29
26
 
30
27
  say "\nRecommended next steps:", :yellow
@@ -0,0 +1,45 @@
1
+ module RailsUuidPk
2
+ module MigrationHelpers
3
+ module References
4
+ def references(*args, **options)
5
+ ref_name = args.first
6
+ ref_table = options.delete(:to_table) || ref_name.to_s.pluralize
7
+
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
12
+ options[:type] = :uuid
13
+ end
14
+
15
+ super
16
+ end
17
+
18
+ def application_uses_uuid_primary_keys?
19
+ # Check if the application is configured to use UUID primary keys globally
20
+ Rails.application.config.generators.options[:active_record]&.[](:primary_key_type) == :uuid
21
+ end
22
+
23
+ alias_method :belongs_to, :references
24
+
25
+ private
26
+
27
+ def uuid_primary_key?(table_name)
28
+ conn = @conn || @base || (respond_to?(:connection) ? connection : nil)
29
+ return false unless conn&.table_exists?(table_name)
30
+
31
+ pk_column = find_primary_key_column(table_name, conn)
32
+ return false unless pk_column
33
+
34
+ pk_column.sql_type.downcase.match?(/\A(uuid|varchar\(36\))\z/)
35
+ end
36
+
37
+ def find_primary_key_column(table_name, conn)
38
+ pk_name = conn.primary_key(table_name)
39
+ return nil unless pk_name
40
+
41
+ conn.columns(table_name).find { |c| c.name == pk_name }
42
+ end
43
+ end
44
+ end
45
+ end
@@ -42,5 +42,15 @@ module RailsUuidPk
42
42
  ActiveRecord::Base.include RailsUuidPk::HasUuidv7PrimaryKey
43
43
  end
44
44
  end
45
+
46
+ initializer "rails-uuid-pk.migration_helpers" do
47
+ ActiveSupport.on_load(:active_record) do
48
+ require "rails_uuid_pk/migration_helpers"
49
+
50
+ # Include migration helpers for all database adapters
51
+ ActiveRecord::ConnectionAdapters::TableDefinition.prepend(RailsUuidPk::MigrationHelpers::References)
52
+ ActiveRecord::ConnectionAdapters::Table.prepend(RailsUuidPk::MigrationHelpers::References)
53
+ end
54
+ end
45
55
  end
46
56
  end
@@ -1,3 +1,3 @@
1
1
  module RailsUuidPk
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joon Lee
@@ -68,6 +68,7 @@ files:
68
68
  - lib/generators/rails_uuid_pk/install/templates/has_uuidv7_primary_key.rb
69
69
  - lib/rails_uuid_pk.rb
70
70
  - lib/rails_uuid_pk/concern.rb
71
+ - lib/rails_uuid_pk/migration_helpers.rb
71
72
  - lib/rails_uuid_pk/railtie.rb
72
73
  - lib/rails_uuid_pk/sqlite3_adapter_extension.rb
73
74
  - lib/rails_uuid_pk/type.rb