Skip to content

Commit 97da4ba

Browse files
authored
TECH-16946: fix bug on new model with polymorphic belongs_to (#176)
* TECH-16946: fix bug where new model with polymorphic belongs_to referenced columns_hash (which does not exist) * TECH-16946: bump to v2.3.1; update CHANGELOG
1 parent e647b3f commit 97da4ba

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
44

55
Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [2.3.1] - Unreleased
8+
### Fixed
9+
- Fixed bug where a new model with `belongs_to :owner, polymorphic: true` would cause
10+
a "Mysql2::Error: Table '<new table>' doesn't exist:" exception when generating a migration.
11+
712
## [2.3.0] - 2024-10-31
813
### Updated
914
- Updated the `current_adapter` method to use `connection_db_config` for Rails 6.1 and higher, while retaining `connection_config` for earlier versions

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
declare_schema (2.3.0)
4+
declare_schema (2.3.1)
55
rails (>= 6.0)
66

77
GEM

lib/declare_schema/model.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def belongs_to(name, scope = nil, **options)
240240

241241
def _infer_fk_limit(foreign_key_column, reflection)
242242
if reflection.options[:polymorphic]
243-
if (foreign_key_column = columns_hash[foreign_key_column.to_s]) && foreign_key_column.type == :integer
243+
if (foreign_key_column = _column(foreign_key_column)) && foreign_key_column.type == :integer
244244
foreign_key_column.limit
245245
end
246246
else

lib/declare_schema/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module DeclareSchema
4-
VERSION = "2.3.0"
4+
VERSION = "2.3.1"
55
end

spec/lib/declare_schema/migration_generator_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,7 @@ class Fk < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
13051305
belongs_to :id_default
13061306
belongs_to :id4
13071307
belongs_to :id8
1308+
belongs_to :owner, polymorphic: true
13081309
end
13091310
end
13101311

@@ -1332,20 +1333,24 @@ class Fk < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
13321333
it 'infers the correct FK type from the create_table id: type' do
13331334
up = Generators::DeclareSchema::Migration::Migrator.run.first
13341335

1335-
create_fks = up.split("\n").grep(/t\.integer /).map { |command| command.gsub(', null: false', '').gsub(/^ +/, '') }
1336+
create_fks = up.split("\n").grep(/t\.integer|string /).map { |command| command.gsub(', null: false', '').gsub(/^ +/, '') }
13361337
if current_adapter == 'sqlite3'
13371338
create_fks.map! { |command| command.gsub(/limit: [a-z0-9]+/, 'limit: X') }
13381339
expect(create_fks).to eq([
13391340
't.integer :id_default_id, limit: X',
13401341
't.integer :id4_id, limit: X',
1341-
't.integer :id8_id, limit: X'
1342-
]), up
1342+
't.integer :id8_id, limit: X',
1343+
't.integer :owner_id, limit: X',
1344+
't.string :owner_type, limit: X'
1345+
])
13431346
else
13441347
expect(create_fks).to eq([
13451348
't.integer :id_default_id, limit: 8',
13461349
't.integer :id4_id, limit: 4',
1347-
't.integer :id8_id, limit: 8'
1348-
]), up
1350+
't.integer :id8_id, limit: 8',
1351+
't.integer :owner_id, limit: 8',
1352+
"t.string :owner_type, limit: 255#{charset_and_collation}"
1353+
])
13491354
end
13501355
end
13511356

0 commit comments

Comments
 (0)