foreign_key_checker 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b355b8130d2225136101b32202f457ee9615c33621730fa693fcbcb969af3fcf
4
- data.tar.gz: ee8b977be65abc39da0914c804f612bb2fb2a33a98a46c922eedde3866147da1
3
+ metadata.gz: 61d367175ce6fba75c746ebc5425f463c20d903e31885edd04e407a100dbe72e
4
+ data.tar.gz: 1a6cb3b2f4d0123e884f432a7d6a4c48acf2d9e16a7fb3439ddfd4f06df198db
5
5
  SHA512:
6
- metadata.gz: 8f34d020417ddcaefc15a6faec34ffb495f6383157104b660f28013f85a57d8a390cee15c47613ffed64a884ffc581c4c0c5557c97a36d9e2fadf0ff54021d7a
7
- data.tar.gz: a4b0f8032dc6adc78f4780c7c939acc661bb2e9dde713cddee43a5080f59e2882a1433550964f26abb4120491752a83e596f5cc37821561c6ba118cd52f2ad16
6
+ metadata.gz: 0437adb8468a7018a11e77d5e77fec0cd7fcd0b5cf37ccfb8d31a2da35da3ebd4a706a5d373ebb8f50ae91e3e698c73b1cd5cd4e28049b520156e90d9e65b619
7
+ data.tar.gz: 91f2dda0e5a8036cdc330a673a8d2aaba235435f870b02c99ea828140fca646456d555d979447fc78c3f9174c53ad23ff2c295084588c2f54ac211f606f684ac
@@ -1,3 +1,3 @@
1
1
  module ForeignKeyChecker
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require "foreign_key_checker/railtie"
2
2
 
3
3
  module ForeignKeyChecker
4
+ class TypeMismatch < StandardError; end
4
5
  class Result
5
6
  attr_reader :model, :association
6
7
  def initialize(data)
@@ -34,6 +35,11 @@ module ForeignKeyChecker
34
35
  def inspect
35
36
  "#<#{self.class.name}:#{self.object_id} #{message}>"
36
37
  end
38
+
39
+ def nullable?
40
+ model.columns_hash[from_column.to_s].null
41
+ end
42
+
37
43
  end
38
44
 
39
45
  class ForeignKeyResult < Result
@@ -152,6 +158,12 @@ module ForeignKeyChecker
152
158
 
153
159
  end
154
160
 
161
+ def check_types(model, association)
162
+ type_from = model.columns_hash[association.foreign_key.to_s].sql_type
163
+ type_to = association.klass.columns_hash[association.klass.primary_key.to_s].sql_type
164
+ raise TypeMismatch, "TypeMissMatch #{type_from} != #{type_to}" if type_from != type_to
165
+ end
166
+
155
167
  def check_foreign_key_bt_association(model, association)
156
168
  return if model.name.starts_with?('HABTM_')
157
169
  related = association.klass
@@ -161,6 +173,8 @@ module ForeignKeyChecker
161
173
  "#{related.quoted_table_name}.#{related.quoted_primary_key} IS NULL AND #{model.quoted_table_name}.#{column_name} IS NOT NULL"
162
174
  )
163
175
 
176
+ check_types(model, association)
177
+
164
178
  if zombies
165
179
  number = scope.count
166
180
  if number > 0
@@ -174,6 +188,7 @@ module ForeignKeyChecker
174
188
  end
175
189
 
176
190
  if foreign_keys && !model.connection.foreign_key_exists?(model.table_name, related.table_name)
191
+ scope.first
177
192
  @result[:foreign_keys] << ForeignKeyResult.new(
178
193
  model: model,
179
194
  association: association,
@@ -187,7 +202,7 @@ module ForeignKeyChecker
187
202
  association: association,
188
203
  )
189
204
  end
190
- rescue ActiveRecord::InverseOfAssociationNotFoundError, ActiveRecord::StatementInvalid => error
205
+ rescue ActiveRecord::InverseOfAssociationNotFoundError, ActiveRecord::StatementInvalid, TypeMismatch => error
191
206
  @result[:broken] << BrokenRelationResult.new(
192
207
  model: model,
193
208
  association: association,
@@ -14,7 +14,7 @@ module ForeignKeyChecker
14
14
  @done = Dir.glob(Rails.root.join('db', 'migrate', '*.rb')).map do |path|
15
15
  File.open(path).readlines.find { |line| line.include?('ActiveRecord::Migration') && line.include?(@class_name) }
16
16
  end.compact.size
17
- @checks = ForeignKeyChecker.check(zombies: false, indexes: false) unless @behavior == :revoke
17
+ @checks = ForeignKeyChecker.check(zombies: false) unless @behavior == :revoke
18
18
  if @behavior == :revoke
19
19
  @file_suffix = "_v#{@done}" if @done > 1
20
20
  @class_suffix = "V#{@done}" if @done > 1
@@ -3,8 +3,13 @@ class <%= @class_name %><%= @class_suffix %> < ActiveRecord::Migration[<%= Activ
3
3
  <% @checks[:foreign_keys].each do |fk| %>
4
4
  reversible do |dir|
5
5
  dir.up do
6
- # <%= fk.to_zombie.migration(set_null: true) %>
6
+ <% if fk.nullable? %>
7
+ # <%= fk.to_zombie.migration %>
8
+ <%= fk.to_zombie.migration(set_null: true) %>
9
+ <% else %>
10
+ # column <%= fk.from_column %> can't be set to null
7
11
  <%= fk.to_zombie.migration %>
12
+ <% end %>
8
13
  end
9
14
  end
10
15
  <%= fk.migration %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreign_key_checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - AnatolyShirykalov