declare_schema 1.2.2 → 1.2.3.pre.ga.0

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: 2d831ee175d229d9c618cffa6b89fa375eb15bf2e9121feb732d474e46048051
4
- data.tar.gz: 1ea6d18318ad4f02591e68491b526b062e4d3b0e628b5ac7512987ed0f5772d1
3
+ metadata.gz: 64db3ac9b4bea4625bbd2bdc40f9aea3296157c535d3bc773aab680fa33d3e9d
4
+ data.tar.gz: a5681f63baa97b4cc4409bf9784a0b2c45d58ac8fe251214771ebb269eb649ee
5
5
  SHA512:
6
- metadata.gz: 2bb053f279aab8ab554d7258abdd9781c2b8981e4086a03a527c6541dcb2dbff27fd2770370d907ff463f54417d11228b8040e0e738ee822f9588ff292c46b25
7
- data.tar.gz: 5504007956e98a03c051c7b28272fe92a09165d59e29dc395900cbeb726c90bb06f243d313f08f27e74d4efe0c65a53ca92a9761f4fe020a00ad2e3794de27a2
6
+ metadata.gz: 8e4dd03780302f015725770a734a58d369f9655c7717e64497dbb24ce4602cd379bc7b8db15d89780adb55ec342e485110e8e34fdf0ef522e4925a927433891b
7
+ data.tar.gz: 61f05f815b21f47fa358950c25b38411dcde90b59b73fbca1a373291e0552dcc1d3c516e1c894c84c5bae5cbe66b2ca80a976ce537c1c42e8b4a2d8d06b3cf56
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (1.2.2)
4
+ declare_schema (1.2.3.pre.ga.0)
5
5
  rails (>= 5.0)
6
6
 
7
7
  GEM
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "1.2.2"
4
+ VERSION = "1.2.3.pre.ga.0"
5
5
  end
@@ -407,7 +407,7 @@ module Generators
407
407
  fk_changes = if ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/)
408
408
  []
409
409
  else
410
- change_foreign_key_constraints(model, current_table_name)
410
+ change_foreign_key_constraints(model, current_table_name, to_rename)
411
411
  end
412
412
  table_options_changes = if ActiveRecord::Base.connection.class.name.match?(/mysql/i)
413
413
  change_table_options(model, current_table_name)
@@ -451,11 +451,30 @@ module Generators
451
451
  end
452
452
  end
453
453
 
454
- drop_indexes = (existing_indexes_without_primary_key - model_indexes_without_primary_key).map do |i|
454
+ indexes_to_drop = existing_indexes_without_primary_key - model_indexes_without_primary_key
455
+ indexes_to_add = model_indexes_without_primary_key - existing_indexes_without_primary_key
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
+ }
472
+
473
+ drop_indexes = (indexes_to_drop - renamed_indexes_to_drop).map do |i|
455
474
  ::DeclareSchema::SchemaChange::IndexRemove.new(new_table_name, i.columns, unique: i.unique, where: i.where, name: i.name)
456
475
  end
457
476
 
458
- add_indexes = (model_indexes_without_primary_key - existing_indexes_without_primary_key).map do |i|
477
+ add_indexes = (indexes_to_add - renamed_indexes_to_add).map do |i|
459
478
  ::DeclareSchema::SchemaChange::IndexAdd.new(new_table_name, i.columns, unique: i.unique, where: i.where, name: i.name)
460
479
  end
461
480
 
@@ -463,19 +482,38 @@ module Generators
463
482
  [Array(change_primary_key) + drop_indexes + add_indexes]
464
483
  end
465
484
 
466
- def change_foreign_key_constraints(model, old_table_name)
485
+ def change_foreign_key_constraints(model, old_table_name, to_rename)
467
486
  ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/) and raise ArgumentError, 'SQLite does not support foreign keys'
468
487
  ::DeclareSchema.default_generate_foreign_keys or return []
469
488
 
470
489
  existing_fks = ::DeclareSchema::Model::ForeignKeyDefinition.for_model(model, old_table_name)
471
490
  model_fks = model.constraint_specs
472
491
 
473
- drop_fks = (existing_fks - model_fks).map do |fk|
492
+ fks_to_drop = existing_fks - model_fks
493
+ fks_to_add = model_fks - existing_fks
494
+
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 == fks_to_drop.child_table_name &&
501
+ fk_to_add.parent_table_name == fks_to_drop.parent_table_name &&
502
+ fk_to_add.constraint_name == fks_to_drop.constraint_name &&
503
+ fk_to_add.foreign_key == to_rename[fks_to_drop.foreign_key]
504
+
505
+ renamed_fks_to_drop.append(fk_to_drop)
506
+ renamed_fks_to_add.append(fk_to_add)
507
+ end
508
+ }
509
+ }
510
+
511
+ drop_fks = (fks_to_drop - renamed_fks_to_drop).map do |fk|
474
512
  ::DeclareSchema::SchemaChange::ForeignKeyRemove.new(fk.child_table_name, fk.parent_table_name,
475
513
  column_name: fk.foreign_key_name, name: fk.constraint_name)
476
514
  end
477
515
 
478
- add_fks = (model_fks - existing_fks).map do |fk|
516
+ add_fks = (fks_to_add - renamed_fks_to_add).map do |fk|
479
517
  # next if fk.parent.constantize.abstract_class || fk.parent == fk.model.class_name
480
518
  ::DeclareSchema::SchemaChange::ForeignKeyAdd.new(fk.child_table_name, fk.parent_table_name,
481
519
  column_name: fk.foreign_key_name, name: fk.constraint_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.2
4
+ version: 1.2.3.pre.ga.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: 2023-01-27 00:00:00.000000000 Z
11
+ date: 2023-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails