declare_schema 3.1.0.colin.3 → 4.0.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/.github/workflows/pipeline.yml +1 -1
- data/CHANGELOG.md +4 -2
- data/Gemfile.lock +1 -1
- data/lib/declare_schema/model.rb +23 -9
- data/lib/declare_schema/version.rb +1 -1
- data/lib/generators/declare_schema/migration/migrator.rb +10 -1
- data/spec/lib/generators/declare_schema/migration/migrator_default_pk_type_spec.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 929f250672103aa42777cb918340e5281d100fdaf19cedb5f7aaa96504e66a8c
|
|
4
|
+
data.tar.gz: 377a3f2aaae4fa4b6294a4c4a9edd672a8626a505be6a07e06b953a28cb78ffc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8d05a979184d7e93041a462b1269b7d71a6a675f37b8914e27728db0f2d28878d0436cc10eba4bd107f1cb57355c1321147659842367e9231ebb32eec5d14b79
|
|
7
|
+
data.tar.gz: 3c361cb45d57de4c4aac717155b354f34d0c654b4881c29742a9f611cf7ca8efd0c9297e70433dd7b2df4df70751bf3c1d0463824ccc8eb77f5f9a1d836e711d
|
data/CHANGELOG.md
CHANGED
|
@@ -4,9 +4,11 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
|
4
4
|
|
|
5
5
|
Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
-
## [
|
|
7
|
+
## [4.0.0] - 2026-05-05
|
|
8
8
|
### Added
|
|
9
|
-
-
|
|
9
|
+
- Generalized `belongs_to` foreign keys to always match the primary key they point at, including
|
|
10
|
+
in HABTM intersection tables. (Previously this was just a special case that matched up
|
|
11
|
+
:integer vs :bigint.)
|
|
10
12
|
|
|
11
13
|
### Removed
|
|
12
14
|
- Drop support for Rails 6.x. Minimum supported Rails is now 7.0.
|
data/Gemfile.lock
CHANGED
data/lib/declare_schema/model.rb
CHANGED
|
@@ -371,13 +371,15 @@ module DeclareSchema
|
|
|
371
371
|
|
|
372
372
|
# @param klass [Class] any ActiveRecord class
|
|
373
373
|
# @return [ActiveRecord::ConnectionAdapters::Column, nil] the live PK column, or
|
|
374
|
-
# nil when the table doesn't exist yet or can't otherwise be inspected
|
|
375
|
-
#
|
|
376
|
-
#
|
|
377
|
-
#
|
|
374
|
+
# nil when the table doesn't exist yet or can't otherwise be inspected, or
|
|
375
|
+
# when the model has a compound primary key (Array PK name misses
|
|
376
|
+
# `columns_hash`). Reads via `columns_hash` directly (not `_column`, whose
|
|
377
|
+
# `@table_exists` memoization can pin a stale answer) so a parent table
|
|
378
|
+
# being created in this same migration run is honored.
|
|
378
379
|
def _pk_column_for(klass)
|
|
379
|
-
pk_name = klass.
|
|
380
|
-
|
|
380
|
+
if (pk_name = klass.try(:_declared_primary_key) || klass.primary_key)
|
|
381
|
+
klass.columns_hash[pk_name]
|
|
382
|
+
end
|
|
381
383
|
rescue ActiveRecord::StatementInvalid, ActiveRecord::NoDatabaseError
|
|
382
384
|
nil
|
|
383
385
|
end
|
|
@@ -391,13 +393,25 @@ module DeclareSchema
|
|
|
391
393
|
# the column type doesn't round-trip cleanly
|
|
392
394
|
def _field_spec_options_from_pk_column(column)
|
|
393
395
|
if column&.type == :integer
|
|
394
|
-
column.limit == 8
|
|
396
|
+
if column.limit == 8
|
|
397
|
+
[:bigint, {}]
|
|
398
|
+
else
|
|
399
|
+
[:integer, { limit: column.limit || 4 }]
|
|
400
|
+
end
|
|
395
401
|
end
|
|
396
402
|
end
|
|
397
403
|
|
|
398
404
|
# `declare_schema id: ...` accepts either a Hash (`id: { type: :integer, limit: 4 }`)
|
|
399
|
-
# or a bare type Symbol (`id: :integer`).
|
|
400
|
-
#
|
|
405
|
+
# or a bare type Symbol (`id: :integer`). Normalizes both shapes -- plus the
|
|
406
|
+
# absent / unrecognized case -- into a uniform `[type, options]` pair so the
|
|
407
|
+
# caller (`_primary_key_field_spec_from_table_options`) can dispatch on
|
|
408
|
+
# `type.nil?` to decide whether to fall back to live-PK inspection or the
|
|
409
|
+
# gem default.
|
|
410
|
+
#
|
|
411
|
+
# @param value [Hash, Symbol, nil] the per-PK entry from `_table_options`
|
|
412
|
+
# @return [Array(Symbol, Hash), Array(nil, Hash)] `[type, options]` where
|
|
413
|
+
# `type` is nil when `value` is neither a Hash nor a Symbol (signal to
|
|
414
|
+
# caller to fall back). `options` always excludes `:type`.
|
|
401
415
|
def _parse_pk_table_options(value)
|
|
402
416
|
case value
|
|
403
417
|
when Hash then [value[:type], value.except(:type)]
|
|
@@ -80,7 +80,16 @@ module Generators
|
|
|
80
80
|
self.class.native_types
|
|
81
81
|
end
|
|
82
82
|
|
|
83
|
-
#
|
|
83
|
+
# Collect every HABTM reflection across the application, keyed by its join
|
|
84
|
+
# table name. Each join table is shared by both sides of a HABTM
|
|
85
|
+
# association, so iterating all descendants would yield two reflections
|
|
86
|
+
# per join table (one from each parent model). Keying by `join_table` and
|
|
87
|
+
# using `||=` keeps the first occurrence and silently drops the duplicate
|
|
88
|
+
# from the other side, so the migrator emits exactly one shim per join
|
|
89
|
+
# table.
|
|
90
|
+
#
|
|
91
|
+
# @return [Hash{String => ActiveRecord::Reflection::HasAndBelongsToManyReflection}]
|
|
92
|
+
# join table name -> the (first-seen) HABTM reflection that owns it
|
|
84
93
|
def habtm_tables
|
|
85
94
|
ActiveRecord::Base.send(:descendants).each_with_object({}) do |c, result|
|
|
86
95
|
c.reflect_on_all_associations(:has_and_belongs_to_many).each do |a|
|
|
@@ -12,11 +12,11 @@ RSpec.describe 'DeclareSchema.default_generated_primary_key_type integration wit
|
|
|
12
12
|
include_context 'prepare test app'
|
|
13
13
|
|
|
14
14
|
before do
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
# Cross-adapter DDL: `integer PRIMARY KEY NOT NULL` works on sqlite, mysql, and
|
|
16
|
+
# postgres. AUTOINCREMENT (sqlite) / AUTO_INCREMENT (mysql) / SERIAL (postgres)
|
|
17
|
+
# would all be valid, but the migrator inspects type/null, not auto-increment
|
|
18
|
+
# metadata, so we can skip the auto-increment dance entirely.
|
|
19
|
+
ActiveRecord::Base.connection.execute("CREATE TABLE foos (id integer PRIMARY KEY NOT NULL, name varchar(250))")
|
|
20
20
|
ActiveRecord::Base.connection.schema_cache.clear!
|
|
21
21
|
|
|
22
22
|
allow_any_instance_of(DeclareSchema::Support::ThorShell)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: declare_schema
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 4.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Invoca Development adapted from hobo_fields by Tom Locke
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-05-
|
|
11
|
+
date: 2026-05-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|