sqlite_crypto 2.0.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7728bff2a398f6ed1fe0ce0888055586b7619d45954728240ef32ba33cd1650
4
- data.tar.gz: fb5715411594706283fd93969b4f97709f468870f428105c1cb5ebdefb8d5c56
3
+ metadata.gz: 31acfd48736d083cd848127dcffbfefc620be1e3694e3f69e19b0d0c09d3aad9
4
+ data.tar.gz: 59daf83e4a32c19a97e7dc2a311e395a46a70f40222963c9e271575d82ac4c04
5
5
  SHA512:
6
- metadata.gz: 7d0efcd9f607384d1e33ee2c55643408cae5d4a0382885dee40ef6bc0dc142f62d4085cc85f529fd2e76694ffc624651aa7802f85bc0de789716a0b3d7c1aa27
7
- data.tar.gz: 9954abb7f5068e1b85f9e636ce1abe0d921baa94b9b7394711745bba9bf349655611dcbd925b6ba0fe1c4d1405f2ae434bf863c7234ed864de17b5748b3efadd
6
+ metadata.gz: '03968e8bd73633bf3a5150a46b2fe72f84a11ece19f735c4be8406dd954a3168e38cca89394b547e8e8d3f728e6d211933b1fd13792a5bdcebdfc522287547c8'
7
+ data.tar.gz: bbbd062951f49856e4b74cd90c4bb8a67c5d2dab2547bebffb40643b1eb4b14c0a6c0f269ed8e53356b3e09922541d2c8c95fdc8d64a0e1aa0d6d82d09a752e7
data/CHANGELOG.md CHANGED
@@ -5,6 +5,30 @@ 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
+
19
+ ## [2.0.2] - 2026-02-11
20
+
21
+ ### Fixed
22
+ - **Generator autodiscovery**: Moved install generator to `lib/generators/sqlite_crypto/install/` to follow Rails convention. Previously `rails generate sqlite_crypto:install` could not find the generator (bug since v2.0.0)
23
+ - **Schema loading**: Moved `uuid`/`ulid` helper includes from require-time into railtie initializer. The previous `if defined?` guard ran before ActiveRecord was loaded, causing `NameError: undefined method 'uuid'` when loading schema.rb (bug since v2.0.1)
24
+ - **Rails 8+ compatibility**: Include schema helpers into `ActiveRecord::Schema::Definition` where schema.rb blocks are evaluated in Rails 8+
25
+
26
+ ### Added
27
+ - Schema round-trip integration test (dump, reload, verify)
28
+
29
+ ### Acknowledgements
30
+ - Thanks to [@yostinso](https://github.com/yostinso) for identifying both issues in [#17](https://github.com/bart-oz/sqlite_crypto/pull/17)
31
+
8
32
  ## [2.0.1] - 2026-01-14
9
33
 
10
34
  ### Security
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # SQLite Crypto
2
2
 
3
- [![Version](https://img.shields.io/badge/version-2.0.0-blue.svg)](https://github.com/bart-oz/sqlite_crypto/releases)
3
+ [![Version](https://img.shields.io/badge/version-2.0.2-blue.svg)](https://github.com/bart-oz/sqlite_crypto/releases)
4
4
  [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.txt)
5
5
  [![types supported](https://img.shields.io/badge/types-ULID,_UUIDv7/v4-brightgreen.svg)](https://github.com/bart-oz/sqlite_crypto)
6
6
  [![Tests](https://img.shields.io/badge/tests-passing-brightgreen.svg)](https://github.com/bart-oz/sqlite_crypto/actions)
@@ -127,9 +127,28 @@ create_table :posts do |t|
127
127
  end
128
128
  ```
129
129
 
130
- **Model-Level Generation**
130
+ **Automatic Primary Key Generation**
131
131
 
132
- Generate UUIDs or ULIDs for any column:
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
@@ -10,4 +10,4 @@ SqliteCrypto.configure do |config|
10
10
  # Use if you need Ruby 3.1/3.2 compatibility
11
11
  #
12
12
  config.uuid_version = :v7 # UUIDv7 is set as default
13
- end
13
+ end
@@ -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
@@ -29,6 +29,16 @@ module SqliteCrypto
29
29
  ActiveRecord::ConnectionAdapters::SQLite3::SchemaDumper.prepend(SqliteCrypto::SchemaDumper)
30
30
  end
31
31
 
32
+ initializer "sqlite_crypto.schema_definitions", after: "active_record.initialize_database" do
33
+ require "sqlite_crypto/schema_definitions"
34
+ ActiveRecord::Schema.include(SqliteCrypto::SchemaDefinitions)
35
+
36
+ # Rails 8+: schema.rb block is evaluated in Schema::Definition context
37
+ if defined?(ActiveRecord::Schema::Definition)
38
+ ActiveRecord::Schema::Definition.include(SqliteCrypto::SchemaDefinitions)
39
+ end
40
+ end
41
+
32
42
  initializer "sqlite_crypto.migration_helpers", after: "active_record.initialize_database" do
33
43
  require "sqlite_crypto/migration_helpers"
34
44
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Module to add uuid/ulid helper methods to schema loading context
4
3
  module SqliteCrypto
5
4
  module SchemaDefinitions
6
5
  def uuid
@@ -12,8 +11,3 @@ module SqliteCrypto
12
11
  end
13
12
  end
14
13
  end
15
-
16
- # Extend ActiveRecord::Schema context for schema.rb loading (not global Object)
17
- if defined?(ActiveRecord::Schema)
18
- ActiveRecord::Schema.include(SqliteCrypto::SchemaDefinitions)
19
- end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SqliteCrypto
4
- VERSION = "2.0.1"
4
+ VERSION = "2.1.0"
5
5
  RUBY_MINIMUM_VERSION = "3.1.0"
6
6
  RAILS_MINIMUM_VERSION = "7.1.0"
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlite_crypto
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - BartOz
@@ -64,10 +64,10 @@ files:
64
64
  - CHANGELOG.md
65
65
  - LICENSE.txt
66
66
  - README.md
67
+ - lib/generators/sqlite_crypto/install/install_generator.rb
68
+ - lib/generators/sqlite_crypto/install/templates/initializer.rb.tt
67
69
  - lib/sqlite_crypto.rb
68
70
  - lib/sqlite_crypto/configuration.rb
69
- - lib/sqlite_crypto/generators/install_generator.rb
70
- - lib/sqlite_crypto/generators/templates/initializer.rb.tt
71
71
  - lib/sqlite_crypto/generators/uuid.rb
72
72
  - lib/sqlite_crypto/migration_helpers.rb
73
73
  - lib/sqlite_crypto/model_extensions.rb