declare_schema 1.2.3.pre.ga.2 → 1.2.3.pre.ga.4

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: 506475213508a2b48038ccb1e8d2b4398b03879d88a7760803ffbc9273463033
4
+ data.tar.gz: 6fac90c4db4626a52d98c71ad98c6f8615e8f041473874575f9b3ba2e2123589
5
5
  SHA512:
6
- metadata.gz: 6f5c2dea9bd05f536f05f058ce193a4003384f754649d5b922bbce6532bfe25f60e8c987e2b1136b603899cc0cb9217f0dcc8b59e6bbf974f9dc57aa600abf36
7
- data.tar.gz: 17fe43628304597208f5063fc907ae3bd85586732a26985c0d709004d4411af1ff89dfab79b41334073c7860231b0c560ae5d6eff605a72335748335696da5d8
6
+ metadata.gz: 9d9eda18aaa034a6b78239e76e2416b1a0f26a4094268b9a9fb1b4f807108a557d7163d7e623568cb3a6bf1bc4d5dee1d84068d7b391e5cc399b81de095aad6e
7
+ data.tar.gz: 36c6b8f67da4ece257360b4bded0dc79b08e9e31a3f9a3ba0745016ebd481ad76c13e7f8fb549e1d923c9e557056ceff10b60858bb0fa0743f61131af31f0a08
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.4)
5
5
  rails (>= 5.0)
6
6
 
7
7
  GEM
@@ -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.4"
5
5
  end
@@ -454,32 +454,38 @@ 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, renamed_indexes_to_add = index_changes_due_to_column_renames(indexes_to_drop, indexes_to_add, to_rename)
458
+
459
+ drop_indexes = (indexes_to_drop - renamed_indexes_to_drop).map do |i|
460
+ ::DeclareSchema::SchemaChange::IndexRemove.new(new_table_name, i.columns, unique: i.unique, where: i.where, name: i.name)
461
+ end
462
+
463
+ add_indexes = (indexes_to_add - renamed_indexes_to_add).map do |i|
464
+ ::DeclareSchema::SchemaChange::IndexAdd.new(new_table_name, i.columns, unique: i.unique, where: i.where, name: i.name)
465
+ end
466
+
467
+ # the order is important here - adding a :unique, for instance needs to remove then add
468
+ [Array(change_primary_key) + drop_indexes + add_indexes]
469
+ end
470
+
471
+ def index_changes_due_to_column_renames(indexes_to_drop, indexes_to_add, to_rename)
457
472
  renamed_indexes_to_drop = []
458
473
  renamed_indexes_to_add = []
459
474
 
460
475
  indexes_to_drop.each { |index_to_drop|
461
476
  renamed_columns = index_to_drop.columns.map do |column|
462
- to_rename[column]
477
+ to_rename.fetch(column, column)
463
478
  end
464
479
 
465
480
  indexes_to_add.each { |index_to_add|
466
- if renamed_columns == index_to_add.columns
481
+ if Set.new(renamed_columns) == Set.new(index_to_add.columns)
467
482
  renamed_indexes_to_drop.append(index_to_drop)
468
483
  renamed_indexes_to_add.append(index_to_add)
469
484
  end
470
485
  }
471
486
  }
472
487
 
473
- drop_indexes = (indexes_to_drop - renamed_indexes_to_drop).map do |i|
474
- ::DeclareSchema::SchemaChange::IndexRemove.new(new_table_name, i.columns, unique: i.unique, where: i.where, name: i.name)
475
- end
476
-
477
- add_indexes = (indexes_to_add - renamed_indexes_to_add).map do |i|
478
- ::DeclareSchema::SchemaChange::IndexAdd.new(new_table_name, i.columns, unique: i.unique, where: i.where, name: i.name)
479
- end
480
-
481
- # the order is important here - adding a :unique, for instance needs to remove then add
482
- [Array(change_primary_key) + drop_indexes + add_indexes]
488
+ [renamed_indexes_to_drop, renamed_indexes_to_add]
483
489
  end
484
490
 
485
491
  def change_foreign_key_constraints(model, old_table_name, to_rename)
@@ -492,6 +498,23 @@ module Generators
492
498
  fks_to_drop = existing_fks - model_fks
493
499
  fks_to_add = model_fks - existing_fks
494
500
 
501
+ renamed_fks_to_drop, renamed_fks_to_add = foreign_key_changes_due_to_column_renames(fks_to_drop, fks_to_add, to_rename)
502
+
503
+ drop_fks = (fks_to_drop - renamed_fks_to_drop).map do |fk|
504
+ ::DeclareSchema::SchemaChange::ForeignKeyRemove.new(fk.child_table_name, fk.parent_table_name,
505
+ column_name: fk.foreign_key_name, name: fk.constraint_name)
506
+ end
507
+
508
+ add_fks = (fks_to_add - renamed_fks_to_add).map do |fk|
509
+ # next if fk.parent.constantize.abstract_class || fk.parent == fk.model.class_name
510
+ ::DeclareSchema::SchemaChange::ForeignKeyAdd.new(fk.child_table_name, fk.parent_table_name,
511
+ column_name: fk.foreign_key_name, name: fk.constraint_name)
512
+ end
513
+
514
+ [drop_fks + add_fks]
515
+ end
516
+
517
+ def foreign_key_changes_due_to_column_renames(fks_to_drop, fks_to_add, to_rename)
495
518
  renamed_fks_to_drop = []
496
519
  renamed_fks_to_add = []
497
520
 
@@ -499,6 +522,7 @@ module Generators
499
522
  fks_to_add.each { |fk_to_add|
500
523
  if fk_to_add.child_table_name == fk_to_drop.child_table_name &&
501
524
  fk_to_add.parent_table_name == fk_to_drop.parent_table_name &&
525
+ !fk_to_add.foreign_key.nil? &&
502
526
  fk_to_add.foreign_key == to_rename[fk_to_drop.foreign_key]
503
527
 
504
528
  renamed_fks_to_drop.append(fk_to_drop)
@@ -507,18 +531,7 @@ module Generators
507
531
  }
508
532
  }
509
533
 
510
- drop_fks = (fks_to_drop - renamed_fks_to_drop).map do |fk|
511
- ::DeclareSchema::SchemaChange::ForeignKeyRemove.new(fk.child_table_name, fk.parent_table_name,
512
- column_name: fk.foreign_key_name, name: fk.constraint_name)
513
- end
514
-
515
- add_fks = (fks_to_add - renamed_fks_to_add).map do |fk|
516
- # next if fk.parent.constantize.abstract_class || fk.parent == fk.model.class_name
517
- ::DeclareSchema::SchemaChange::ForeignKeyAdd.new(fk.child_table_name, fk.parent_table_name,
518
- column_name: fk.foreign_key_name, name: fk.constraint_name)
519
- end
520
-
521
- [drop_fks + add_fks]
534
+ [renamed_fks_to_drop, renamed_fks_to_add]
522
535
  end
523
536
 
524
537
  def fk_field_options(model, field_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.4
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