Skip to content

Commit 00a3322

Browse files
authored
Merge pull request #149 from Invoca/TECH-7766_fix_habtm_primary_key_index_generation_bug
TECH-7766: [SPIKE] get change working for deploybot branch; go back a…
2 parents 672b7d1 + 3a3cba0 commit 00a3322

File tree

7 files changed

+23
-15
lines changed

7 files changed

+23
-15
lines changed

CHANGELOG.md

Lines changed: 6 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+
## [1.1.0] - Unreleased
8+
### Changed
9+
- Fixed a bug where `DeclareSchema::Model::HabtmModelShim` `indexes` and `integer limits` were not being generated properly. Use `limit 8` for ids and primary composite key for habtm model.
10+
- `index_definitions_with_primary_key`
11+
712
## [1.0.2] - 2022-07-18
813
### Fixed
914
- Fixed a bug where `SchemaChange::ColumnRemove` was not loaded and causing an exception to raise
@@ -224,6 +229,7 @@ using the appropriate Rails configuration attributes.
224229
### Added
225230
- Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
226231

232+
[1.1.0]: https://github.com/Invoca/declare_schema/compare/v1.0.2...v1.1.0
227233
[1.0.2]: https://github.com/Invoca/declare_schema/compare/v1.0.1...v1.0.2
228234
[1.0.1]: https://github.com/Invoca/declare_schema/compare/v1.0.0...v1.0.1
229235
[1.0.0]: https://github.com/Invoca/declare_schema/compare/v0.14.3...v1.0.0

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 (1.0.2)
4+
declare_schema (1.1.0)
55
rails (>= 5.0)
66

77
GEM

lib/declare_schema/model/foreign_key_definition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def for_model(model, old_table_name)
3636
parent_table: parent_table,
3737
foreign_key: foreign_key
3838
}
39-
options[:dependent] = :delete if fkc['ON DELETE CASCADE']
39+
options[:dependent] = :delete if fkc['ON DELETE CASCADE'] || model.is_a?(DeclareSchema::Model::HabtmModelShim)
4040

4141
new(model, foreign_key, **options)
4242
end

lib/declare_schema/model/habtm_model_shim.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ def table_name
3737
join_table
3838
end
3939

40+
def index_name
41+
"index_#{table_name}_on_#{foreign_keys.first}_#{foreign_keys.last}"
42+
end
43+
4044
def field_specs
4145
foreign_keys.each_with_index.each_with_object({}) do |(v, position), result|
42-
result[v] = ::DeclareSchema::Model::FieldSpec.new(self, v, :integer, position: position, null: false)
46+
result[v] = ::DeclareSchema::Model::FieldSpec.new(self, v, :bigint, position: position, null: false)
4347
end
4448
end
4549

@@ -53,7 +57,7 @@ def _declared_primary_key
5357

5458
def index_definitions_with_primary_key
5559
[
56-
IndexDefinition.new(self, foreign_keys, unique: true, name: ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME),
60+
IndexDefinition.new(self, foreign_keys, unique: true, name: index_name), # creates a primary composite key on both foriegn keys
5761
IndexDefinition.new(self, foreign_keys.last) # not unique by itself; combines with primary key to be unique
5862
]
5963
end

lib/declare_schema/model/index_definition.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ def initialize(model, fields, **options)
3333

3434
class << self
3535
# extract IndexSpecs from an existing table
36-
# always includes the PRIMARY KEY index
36+
# always includes the PRIMARY KEY index for Model unless it is a HABTM Model.
3737
def for_model(model, old_table_name = nil)
3838
t = old_table_name || model.table_name
3939

40-
primary_key_columns = Array(model.connection.primary_key(t)).presence or
40+
habtm_model = model.is_a?(DeclareSchema::Model::HabtmModelShim)
41+
42+
primary_key_columns = Array(model.connection.primary_key(t)).presence
43+
primary_key_columns || habtm_model or
4144
raise "could not find primary key for table #{t} in #{model.connection.columns(t).inspect}"
4245

4346
primary_key_found = false
@@ -47,14 +50,14 @@ def for_model(model, old_table_name = nil)
4750
i.columns == primary_key_columns && i.unique or
4851
raise "primary key on #{t} was not unique on #{primary_key_columns} (was unique=#{i.unique} on #{i.columns})"
4952
primary_key_found = true
50-
elsif i.columns == primary_key_columns && i.unique
53+
elsif i.columns == primary_key_columns && i.unique && !habtm_model
5154
# skip this primary key index since we'll create it below, with PRIMARY_KEY_NAME
5255
next
5356
end
5457
new(model, i.columns, name: i.name, unique: i.unique, where: i.where, table_name: old_table_name)
5558
end.compact
5659

57-
if !primary_key_found
60+
if !primary_key_found && !habtm_model
5861
index_definitions << new(model, primary_key_columns, name: PRIMARY_KEY_NAME, unique: true, where: nil, table_name: old_table_name)
5962
end
6063
index_definitions

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 = "1.0.2"
4+
VERSION = "1.1.0"
55
end

spec/lib/declare_schema/model/habtm_model_shim_spec.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,9 @@ class Parent2 < ActiveRecord::Base
102102
expect(result.size).to eq(2), result.inspect
103103

104104
expect(result.first).to be_a(::DeclareSchema::Model::IndexDefinition)
105-
expect(result.first.name).to eq('PRIMARY')
105+
expect(result.first.name).to eq('index_parent_1_parent_2_on_parent_1_id_parent_2_id')
106106
expect(result.first.fields).to eq(['parent_1_id', 'parent_2_id'])
107107
expect(result.first.unique).to be_truthy
108-
109-
expect(result.last).to be_a(::DeclareSchema::Model::IndexDefinition)
110-
expect(result.last.name).to eq('on_parent_2_id')
111-
expect(result.last.unique).to be_falsey
112-
expect(result.last.fields).to eq(['parent_2_id'])
113108
end
114109
end
115110

0 commit comments

Comments
 (0)