rails_type_id 0.1.0 → 0.2.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 +4 -4
- data/README.md +53 -3
- data/lib/rails_type_id/concern.rb +8 -24
- data/lib/rails_type_id/version.rb +1 -1
- data/lib/tapioca/dsl/compilers/rails_type_id_compiler.rb +0 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0781fbf1b175f2edfe7b66fc82d03d09f08762d084cfc4e0681831d39d6f90b
|
4
|
+
data.tar.gz: 162be97b603d6b6480dcf27461bcf19541f43b30a5c65fcab5b1b46de1c04bd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a2d68e71437f6c62f2c183aaf749a4fddfba9ea61c6708f66c433dfa708ebfd2218ea2bfc74d05b1349cf2f65bbe1cc55b6434688cb5a6d2cb4c7defe823b94
|
7
|
+
data.tar.gz: bbd6f63fd939e57f0dbb46280e249f1d2ed8a929aeb9f1541e49c732bd742b7f81f624a4bf6ed51ca399fe733064a2d58e1a8fbc4778796e1f89915fdeb4c59f
|
data/README.md
CHANGED
@@ -12,8 +12,13 @@ bundle add rails_type_id
|
|
12
12
|
|
13
13
|
## Usage
|
14
14
|
|
15
|
+
### Database requirements
|
16
|
+
|
17
|
+
- ActiveRecord models should have an `id` field that is either a string-like type (`TEXT`, `VARCHAR`) .
|
18
|
+
See [Migration](#migrating-existing-ids) if you have an existing `id` field.
|
19
|
+
|
15
20
|
### Declaring on models
|
16
|
-
Add `RailsTypeId::Concern` to the model. This model's `id` field should have
|
21
|
+
Add `RailsTypeId::Concern` to the model. This model's `id` field should have a string-like database column type.
|
17
22
|
|
18
23
|
```ruby
|
19
24
|
# app/models/my_model.rb
|
@@ -33,9 +38,54 @@ Model instances will have a `type_id` field of type `TypeID`.
|
|
33
38
|
|
34
39
|
```ruby
|
35
40
|
my_model = MyModel.create!(..)
|
36
|
-
my_model.id #=> "
|
41
|
+
my_model.id #=> "mm_01k1kzwngff50tfvc4e9hsrype"
|
37
42
|
my_model.type_id #=> #<TypeID mm_01k1kzwngff50tfvc4e9hsrype>
|
38
|
-
|
43
|
+
```
|
44
|
+
|
45
|
+
### Testing
|
46
|
+
|
47
|
+
`lib/rails_type_id/test_helpers.rb` contains some helper methods for writing wholesome tests.
|
48
|
+
|
49
|
+
### Migrating existing IDs
|
50
|
+
|
51
|
+
For models with an existing Rails `id` field (usually an auto-incrementing integer), you'll need to
|
52
|
+
migrate these to either a string-like column type. Below is an example migration for SQLite
|
53
|
+
using a `text` type for a Users model that has an associated Session.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class MigrateUserToUUID < ActiveRecord::Migration[8.0]
|
57
|
+
def change
|
58
|
+
add_column :users, :uuid, :text, null: true
|
59
|
+
add_column :sessions, :user_uuid, :text, null: true
|
60
|
+
|
61
|
+
Users.find_each do |u|
|
62
|
+
u.update(uuid: SecureRandom.uuid_v7)
|
63
|
+
end
|
64
|
+
Session.find_each do |s|
|
65
|
+
s.update(user_uuid: s.user.uuid)
|
66
|
+
end
|
67
|
+
change_column_null :users, :uuid, false
|
68
|
+
change_column_null :sessions, :user_uuid, false
|
69
|
+
|
70
|
+
remove_foreign_key :sessions, :users
|
71
|
+
|
72
|
+
rename_column :users, :id, :integer_id
|
73
|
+
rename_column :users, :uuid, :id
|
74
|
+
|
75
|
+
rename_column :sessions, :user_id, :integer_user_id
|
76
|
+
rename_column :sessions, :user_uuid, :user_id
|
77
|
+
change_column_null :session, :integer_user_id, true
|
78
|
+
|
79
|
+
execute "ALTER TABLE users DROP CONSTRAINT users_pkey;"
|
80
|
+
execute "ALTER_TABLE users ADD PRIMARY KEY (id);"
|
81
|
+
|
82
|
+
execute "ALTER TABLE ONLY users ALTER COLUMN integer_id DROP DEFAULT"
|
83
|
+
change_column_null :users, :integer_id, true
|
84
|
+
execute "DROP SEQUENCE IF EXISTS users_id_seq"
|
85
|
+
|
86
|
+
add_foreign_key :sessions, :users
|
87
|
+
end
|
88
|
+
end
|
39
89
|
```
|
40
90
|
|
41
91
|
## Development
|
@@ -42,27 +42,21 @@ module RailsTypeId
|
|
42
42
|
included do
|
43
43
|
attribute :id # Postgres UUID field
|
44
44
|
|
45
|
-
before_create :
|
45
|
+
before_create :generate_type_id
|
46
46
|
validates_with Validator
|
47
47
|
|
48
48
|
# Returns the TypeID for the model
|
49
49
|
# @return [TypeID]
|
50
50
|
define_method :type_id do
|
51
51
|
Helpers.validate_type_id_prefix!(self.class.type_id_prefix)
|
52
|
-
TypeID.
|
52
|
+
TypeID.from_string(id)
|
53
53
|
end
|
54
54
|
|
55
|
-
# If `id` is unset, generates a new UUID v7 and sets
|
55
|
+
# If `id` is unset, generates a new UUID v7 TypeID and sets the `id` field
|
56
56
|
# @return [void]
|
57
|
-
define_method :
|
58
|
-
|
59
|
-
|
60
|
-
self.id ||= SecureRandom.uuid_v7
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
define_method :to_param do
|
65
|
-
type_id.to_s
|
57
|
+
define_method :generate_type_id do
|
58
|
+
Helpers.validate_type_id_prefix!(self.class.type_id_prefix)
|
59
|
+
self.id ||= TypeID.from_uuid(self.class.type_id_prefix, SecureRandom.uuid_v7).to_s
|
66
60
|
end
|
67
61
|
end
|
68
62
|
|
@@ -88,9 +82,8 @@ module RailsTypeId
|
|
88
82
|
klasses = lookup_model(type_id)
|
89
83
|
return if klasses.nil?
|
90
84
|
|
91
|
-
id = type_id # TODO: parse out the uuid part
|
92
85
|
klasses.each do |klass|
|
93
|
-
result = klass.find_by(id:
|
86
|
+
result = klass.find_by(id: type_id)
|
94
87
|
return result unless result.nil?
|
95
88
|
end
|
96
89
|
|
@@ -98,23 +91,14 @@ module RailsTypeId
|
|
98
91
|
end
|
99
92
|
|
100
93
|
def lookup_model(type_id)
|
101
|
-
prefix =
|
94
|
+
prefix = TypeID.from_string(type_id).prefix
|
102
95
|
return if prefix.nil?
|
103
96
|
|
104
97
|
prefix_map[prefix]
|
105
98
|
end
|
106
99
|
|
107
|
-
def get_prefix(id)
|
108
|
-
prefix, = parse_id(id)
|
109
|
-
prefix
|
110
|
-
end
|
111
|
-
|
112
100
|
private
|
113
101
|
|
114
|
-
def parse_id(id)
|
115
|
-
id.split("_")
|
116
|
-
end
|
117
|
-
|
118
102
|
def prefix_map
|
119
103
|
Rails.application.eager_load!
|
120
104
|
# This has to be group_by and not index_by because we can have multiple
|