declare_schema 1.2.3.pre.ga.2 → 1.2.3.pre.ga.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b2501407fb1875c42fda5d9c77ab78ca1f966c780e0888716e6e71769ea53b1
4
- data.tar.gz: 535e054f1ad938d271334fdc6054108778dde1f72e59335d778f11ab73d08c14
3
+ metadata.gz: aacc7a857e82fc857a7694a7c32afe7b9bee2a8949dd455f6506eac066658353
4
+ data.tar.gz: 513a4171b54d053b3e9cc58111256010040634e287cf2d7f1a33b278cad92548
5
5
  SHA512:
6
- metadata.gz: 6f5c2dea9bd05f536f05f058ce193a4003384f754649d5b922bbce6532bfe25f60e8c987e2b1136b603899cc0cb9217f0dcc8b59e6bbf974f9dc57aa600abf36
7
- data.tar.gz: 17fe43628304597208f5063fc907ae3bd85586732a26985c0d709004d4411af1ff89dfab79b41334073c7860231b0c560ae5d6eff605a72335748335696da5d8
6
+ metadata.gz: b5075ebac4ebd80def763e46df74d506e3c2df43d4c0601e35fe335be7c3db8faf5b280fd4417dc54ff2f6608162de5ba69082af4353df6f3d7b0ff449b20486
7
+ data.tar.gz: 48534ddd177e9061390b26db9142842968cf28f2637f1cc7d091e28205ace5d55c0ddb0669c7b31b370d4d27d18e83ecc09d0c4b8451f79c4230ef0667c89d4f
data/CHANGELOG.md CHANGED
@@ -4,6 +4,10 @@ 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
+ ## [1.2.3] - Unreleased
8
+ ### Changed
9
+ - Fixed a bug where renaming a foreign key resulted in an incorrect migration
10
+
7
11
  ## [1.2.2] - 2023-01-27
8
12
  ### Changed
9
13
  - Documented `belongs_to` and `has_and_belongs_to_many` behavior
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (1.2.3.pre.ga.2)
4
+ declare_schema (1.2.3.pre.ga.5)
5
5
  rails (>= 5.0)
6
6
 
7
7
  GEM
@@ -12,7 +12,7 @@ module DeclareSchema
12
12
 
13
13
  def initialize(model, foreign_key, **options)
14
14
  @model = model
15
- @foreign_key = foreign_key.to_s.presence
15
+ @foreign_key = foreign_key.to_s.presence or raise ArgumentError "Foreign key cannot be empty: #{foreign_key.inspect}"
16
16
  @options = options
17
17
 
18
18
  @child_table_name = model.table_name # unless a table rename, which would happen when a class is renamed??
@@ -57,7 +57,7 @@ module DeclareSchema
57
57
 
58
58
  def index_definitions_with_primary_key
59
59
  [
60
- IndexDefinition.new(self, foreign_keys, unique: true, name: index_name), # creates a primary composite key on both foriegn keys
60
+ IndexDefinition.new(self, foreign_keys, unique: true, name: index_name), # creates a primary composite key on both foreign keys
61
61
  IndexDefinition.new(self, foreign_keys.last) # not unique by itself; combines with primary key to be unique
62
62
  ]
63
63
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "1.2.3.pre.ga.2"
4
+ VERSION = "1.2.3.pre.ga.5"
5
5
  end
@@ -454,21 +454,7 @@ module Generators
454
454
  indexes_to_drop = existing_indexes_without_primary_key - model_indexes_without_primary_key
455
455
  indexes_to_add = model_indexes_without_primary_key - existing_indexes_without_primary_key
456
456
 
457
- renamed_indexes_to_drop = []
458
- renamed_indexes_to_add = []
459
-
460
- indexes_to_drop.each { |index_to_drop|
461
- renamed_columns = index_to_drop.columns.map do |column|
462
- to_rename[column]
463
- end
464
-
465
- indexes_to_add.each { |index_to_add|
466
- if renamed_columns == index_to_add.columns
467
- renamed_indexes_to_drop.append(index_to_drop)
468
- renamed_indexes_to_add.append(index_to_add)
469
- end
470
- }
471
- }
457
+ renamed_indexes_to_drop, renamed_indexes_to_add = index_changes_due_to_column_renames(indexes_to_drop, indexes_to_add, to_rename)
472
458
 
473
459
  drop_indexes = (indexes_to_drop - renamed_indexes_to_drop).map do |i|
474
460
  ::DeclareSchema::SchemaChange::IndexRemove.new(new_table_name, i.columns, unique: i.unique, where: i.where, name: i.name)
@@ -482,6 +468,19 @@ module Generators
482
468
  [Array(change_primary_key) + drop_indexes + add_indexes]
483
469
  end
484
470
 
471
+ def index_changes_due_to_column_renames(indexes_to_drop, indexes_to_add, to_rename)
472
+ indexes_to_drop.each_with_object([], []) do |index_to_drop, (renamed_indexes_to_drop, renamed_indexes_to_add)|
473
+ renamed_columns = index_to_drop.columns.map do |column|
474
+ to_rename.fetch(column, column)
475
+ end.sort
476
+
477
+ if (index_to_add = indexes_to_add.find { |index_to_add| renamed_columns == index_to_add.columns.sort })
478
+ renamed_indexes_to_drop << index_to_drop
479
+ renamed_indexes_to_add << index_to_add
480
+ end
481
+ end
482
+ end
483
+
485
484
  def change_foreign_key_constraints(model, old_table_name, to_rename)
486
485
  ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/) and raise ArgumentError, 'SQLite does not support foreign keys'
487
486
  ::DeclareSchema.default_generate_foreign_keys or return []
@@ -492,20 +491,7 @@ module Generators
492
491
  fks_to_drop = existing_fks - model_fks
493
492
  fks_to_add = model_fks - existing_fks
494
493
 
495
- renamed_fks_to_drop = []
496
- renamed_fks_to_add = []
497
-
498
- fks_to_drop.each { |fk_to_drop|
499
- fks_to_add.each { |fk_to_add|
500
- if fk_to_add.child_table_name == fk_to_drop.child_table_name &&
501
- fk_to_add.parent_table_name == fk_to_drop.parent_table_name &&
502
- fk_to_add.foreign_key == to_rename[fk_to_drop.foreign_key]
503
-
504
- renamed_fks_to_drop.append(fk_to_drop)
505
- renamed_fks_to_add.append(fk_to_add)
506
- end
507
- }
508
- }
494
+ renamed_fks_to_drop, renamed_fks_to_add = foreign_key_changes_due_to_column_renames(fks_to_drop, fks_to_add, to_rename)
509
495
 
510
496
  drop_fks = (fks_to_drop - renamed_fks_to_drop).map do |fk|
511
497
  ::DeclareSchema::SchemaChange::ForeignKeyRemove.new(fk.child_table_name, fk.parent_table_name,
@@ -521,6 +507,21 @@ module Generators
521
507
  [drop_fks + add_fks]
522
508
  end
523
509
 
510
+ def foreign_key_changes_due_to_column_renames(fks_to_drop, fks_to_add, to_rename)
511
+ fks_to_drop.each_with_object([], []) do |fk_to_drop, (renamed_fks_to_drop, renamed_fks_to_add)|
512
+ if (fks_to_add = fks_to_add.find do |fk_to_add|
513
+ fk_to_add.foreign_key.nil? and raise "Foreign key is not allowed to be nil for #{fk_to_add.inspect}"
514
+ if fk_to_add.child_table_name == fk_to_drop.child_table_name &&
515
+ fk_to_add.parent_table_name == fk_to_drop.parent_table_name &&
516
+ fk_to_add.foreign_key == to_rename[fk_to_drop.foreign_key]
517
+ end
518
+ end)
519
+ renamed_fks_to_drop << fk_to_drop
520
+ renamed_fks_to_add << fks_to_add
521
+ end
522
+ end
523
+ end
524
+
524
525
  def fk_field_options(model, field_name)
525
526
  foreign_key = model.constraint_specs.find { |fk| field_name == fk.foreign_key.to_s }
526
527
  if foreign_key && (parent_table = foreign_key.parent_table_name)
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: 1.2.3.pre.ga.2
4
+ version: 1.2.3.pre.ga.5
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: 2023-03-27 00:00:00.000000000 Z
11
+ date: 2023-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails