sqlite_crypto 2.0.2 → 2.1.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/CHANGELOG.md +11 -0
- data/README.md +21 -2
- data/lib/sqlite_crypto/model_extensions.rb +47 -0
- data/lib/sqlite_crypto/version.rb +1 -1
- 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: 31acfd48736d083cd848127dcffbfefc620be1e3694e3f69e19b0d0c09d3aad9
|
|
4
|
+
data.tar.gz: 59daf83e4a32c19a97e7dc2a311e395a46a70f40222963c9e271575d82ac4c04
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '03968e8bd73633bf3a5150a46b2fe72f84a11ece19f735c4be8406dd954a3168e38cca89394b547e8e8d3f728e6d211933b1fd13792a5bdcebdfc522287547c8'
|
|
7
|
+
data.tar.gz: bbbd062951f49856e4b74cd90c4bb8a67c5d2dab2547bebffb40643b1eb4b14c0a6c0f269ed8e53356b3e09922541d2c8c95fdc8d64a0e1aa0d6d82d09a752e7
|
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/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [2.1.0] - 2026-03-22
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- **Auto primary key generation**: Models with UUID or ULID primary keys now automatically generate
|
|
12
|
+
their primary key on create — no explicit `generates_uuid`/`generates_ulid` call needed
|
|
13
|
+
- `_sqlite_crypto_pk_type` class method for inspecting the detected primary key type (`:uuid`, `:ulid`, or `nil`).
|
|
14
|
+
Detection is based on column schema (string, limit 36 → `:uuid`; limit 26 → `:ulid`) and is cached per class
|
|
15
|
+
|
|
16
|
+
### Acknowledgements
|
|
17
|
+
- Thanks to [@joel](https://github.com/joel) for the contribution in [#20](https://github.com/bart-oz/sqlite_crypto/pull/20)
|
|
18
|
+
|
|
8
19
|
## [2.0.2] - 2026-02-11
|
|
9
20
|
|
|
10
21
|
### Fixed
|
data/README.md
CHANGED
|
@@ -127,9 +127,28 @@ create_table :posts do |t|
|
|
|
127
127
|
end
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
-
**
|
|
130
|
+
**Automatic Primary Key Generation**
|
|
131
131
|
|
|
132
|
-
|
|
132
|
+
When a table uses `id: :uuid` or `id: :ulid`, the gem automatically generates
|
|
133
|
+
primary key values on record creation. No model-level configuration is needed:
|
|
134
|
+
|
|
135
|
+
```ruby
|
|
136
|
+
# migration
|
|
137
|
+
create_table :users, id: :uuid do |t|
|
|
138
|
+
t.string :email
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# model — no extra code required
|
|
142
|
+
class User < ApplicationRecord
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
user = User.create!(email: "test@example.com")
|
|
146
|
+
user.id # => "018d3f91-8f4a-7000-9e7b-4a5c8d2e1f3a"
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**Non-Primary-Key Columns**
|
|
150
|
+
|
|
151
|
+
Generate UUIDs or ULIDs for additional columns with `generates_uuid` / `generates_ulid`:
|
|
133
152
|
|
|
134
153
|
```ruby
|
|
135
154
|
class User < ApplicationRecord
|
|
@@ -6,6 +6,25 @@ module SqliteCrypto
|
|
|
6
6
|
module ModelExtensions
|
|
7
7
|
extend ActiveSupport::Concern
|
|
8
8
|
|
|
9
|
+
included do
|
|
10
|
+
before_create :_sqlite_crypto_auto_generate_id
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def _sqlite_crypto_auto_generate_id
|
|
16
|
+
pk = self.class.primary_key
|
|
17
|
+
return unless pk
|
|
18
|
+
return if self[pk].present?
|
|
19
|
+
|
|
20
|
+
case self.class._sqlite_crypto_pk_type
|
|
21
|
+
when :uuid
|
|
22
|
+
self[pk] = SqliteCrypto::Generators::Uuid.generate
|
|
23
|
+
when :ulid
|
|
24
|
+
self[pk] = ULID.generate.to_s
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
9
28
|
module ClassMethods
|
|
10
29
|
def generates_uuid(attribute, unique: false)
|
|
11
30
|
before_create do
|
|
@@ -22,6 +41,34 @@ module SqliteCrypto
|
|
|
22
41
|
|
|
23
42
|
validates attribute, uniqueness: true if unique
|
|
24
43
|
end
|
|
44
|
+
|
|
45
|
+
# Detect if primary key is UUID or ULID based on column schema.
|
|
46
|
+
# Cached per class. Returns :uuid, :ulid, or nil.
|
|
47
|
+
def _sqlite_crypto_pk_type
|
|
48
|
+
return @_sqlite_crypto_pk_type if defined?(@_sqlite_crypto_pk_type)
|
|
49
|
+
@_sqlite_crypto_pk_type = _detect_sqlite_crypto_pk_type
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def _detect_sqlite_crypto_pk_type
|
|
55
|
+
return nil if abstract_class?
|
|
56
|
+
return nil unless table_exists?
|
|
57
|
+
|
|
58
|
+
pk = primary_key
|
|
59
|
+
return nil unless pk
|
|
60
|
+
|
|
61
|
+
column = columns_hash[pk]
|
|
62
|
+
return nil unless column
|
|
63
|
+
return nil unless column.type == :string
|
|
64
|
+
|
|
65
|
+
case column.limit
|
|
66
|
+
when 36 then :uuid
|
|
67
|
+
when 26 then :ulid
|
|
68
|
+
end
|
|
69
|
+
rescue ActiveRecord::StatementInvalid
|
|
70
|
+
nil
|
|
71
|
+
end
|
|
25
72
|
end
|
|
26
73
|
end
|
|
27
74
|
end
|