database_consistency 2.1.1 → 2.1.2

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: '0932d2df82273397f2389dada3d7934fa796f9bb9e3b4c07382b629682c7b19d'
4
- data.tar.gz: 1d752233498fad2addba69e7b37f5f3a6364493c2bec3883a821901eee2f7061
3
+ metadata.gz: 7fefd921170f4ae51c6f27495defe6d7cfd65f8d4a8bf4e02085aa437ecd2c7a
4
+ data.tar.gz: f25b779dbe86f98399e8da406212e2dc421a376af6c8de20dced43d185f62de5
5
5
  SHA512:
6
- metadata.gz: c0cbca026d510fa349ce5a69c39bbc0b0164ee27bc93e3f75870cdb8b026ca59b5fdbd3466b1eec15b89c921d4f917b7a2f12900d6326e30da33a426aadbcfd3
7
- data.tar.gz: b2cd0dc46bc459f3dd2b96e7041b7b7de1ab13e5b2eab347283d8d3d89fc64e82cd94051cae052fe0e52c55b517afc510b4cd1e7fda261b420daf518adc5be49
6
+ metadata.gz: 3b7e471c020f59578808c600557a55cc5f86bae0ce0e48ad54880d0eb5844c68a498d0b52fe43e2d7c87dcf236922dfb0dc6b880f5305d32fc81cb5299d14c35
7
+ data.tar.gz: 8ec0910740a62dcc7674a70f292e6156686557f0d9e91fc70e0a98af16b045671d054acbe8140cc5922534a044255a51164586f8a7528f7b2740eca48ecc11fa
@@ -26,7 +26,7 @@ module DatabaseConsistency
26
26
 
27
27
  def foreign_key_exists? # rubocop:disable Metrics/AbcSize
28
28
  model.connection.foreign_keys(model.table_name).any? do |foreign_key|
29
- (Helper.extract_columns(association.foreign_key.to_s) - Array.wrap(foreign_key.column)).empty? &&
29
+ (Helper.extract_columns(association.foreign_key) - Array.wrap(foreign_key.column)).empty? &&
30
30
  foreign_key.to_table == association.klass.table_name
31
31
  end
32
32
  end
@@ -3,7 +3,7 @@
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
5
  # This class checks if association's foreign key type covers associated model's primary key (same or bigger)
6
- class ForeignKeyTypeChecker < AssociationChecker
6
+ class ForeignKeyTypeChecker < AssociationChecker # rubocop:disable Metrics/ClassLength
7
7
  Report = ReportBuilder.define(
8
8
  DatabaseConsistency::Report,
9
9
  :pk_name,
@@ -36,10 +36,11 @@ module DatabaseConsistency
36
36
  # | ------------- | ------ |
37
37
  # | covers | ok |
38
38
  # | doesn't cover | fail |
39
- def check # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
40
- if converted_type(associated_column).cover?(converted_type(primary_column))
41
- report_template(:ok)
42
- elsif !(converted_type(associated_column).numeric? && converted_type(primary_column).numeric?)
39
+ def check # rubocop:disable Metrics/MethodLength
40
+ associated_columns_converted_types = associated_columns.map { |column| converted_type(column) }
41
+ primary_columns_converted_types = primary_columns.map { |primary_column| converted_type(primary_column) }
42
+
43
+ if covers?(associated_columns_converted_types, primary_columns_converted_types)
43
44
  report_template(:ok)
44
45
  else
45
46
  report_template(:fail, error_slug: :inconsistent_types)
@@ -53,17 +54,17 @@ module DatabaseConsistency
53
54
  )
54
55
  end
55
56
 
56
- def report_template(status, error_slug: nil) # rubocop:disable Metrics/MethodLength
57
+ def report_template(status, error_slug: nil) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
57
58
  Report.new(
58
59
  status: status,
59
60
  error_slug: error_slug,
60
61
  error_message: nil,
61
62
  table_to_change: table_to_change,
62
- type_to_set: converted_type(primary_column).convert,
63
- fk_type: converted_type(associated_column).type,
64
- fk_name: associated_key,
65
- pk_type: converted_type(primary_column).type,
66
- pk_name: primary_key,
63
+ type_to_set: primary_columns.map { |primary_column| converted_type(primary_column).convert },
64
+ fk_type: associated_columns.map { |column| converted_type(column).type }.join('+'),
65
+ fk_name: associated_keys.join('+'),
66
+ pk_type: primary_columns.map { |primary_column| converted_type(primary_column).type }.join('+'),
67
+ pk_name: primary_keys.join('+'),
67
68
  **report_attributes
68
69
  )
69
70
  end
@@ -76,36 +77,40 @@ module DatabaseConsistency
76
77
  end
77
78
  end
78
79
 
79
- # @return [String]
80
- def primary_key
81
- @primary_key ||= if belongs_to_association?
82
- association.association_primary_key
83
- else
84
- association.active_record_primary_key
85
- end.to_s
80
+ # @return [Array<String>]
81
+ def primary_keys
82
+ @primary_keys ||= if belongs_to_association?
83
+ Helper.extract_columns(association.association_primary_key)
84
+ else
85
+ Helper.extract_columns(association.active_record_primary_key)
86
+ end
86
87
  end
87
88
 
88
- # @return [String]
89
- def associated_key
90
- @associated_key ||= association.foreign_key.to_s
89
+ # @return [Array<String>]
90
+ def associated_keys
91
+ @associated_keys ||= Helper.extract_columns(association.foreign_key)
91
92
  end
92
93
 
93
- # @return [ActiveRecord::ConnectionAdapters::Column]
94
- def primary_column
95
- @primary_column ||= if belongs_to_association?
96
- column(association.klass, primary_key)
97
- else
98
- column(association.active_record, primary_key)
99
- end
94
+ # @return [Array<ActiveRecord::ConnectionAdapters::Column>]
95
+ def primary_columns
96
+ @primary_columns ||= primary_keys.map do |primary_key|
97
+ if belongs_to_association?
98
+ column(association.klass, primary_key)
99
+ else
100
+ column(association.active_record, primary_key)
101
+ end
102
+ end
100
103
  end
101
104
 
102
- # @return [ActiveRecord::ConnectionAdapters::Column]
103
- def associated_column
104
- @associated_column ||= if belongs_to_association?
105
- column(association.active_record, associated_key)
106
- else
107
- column(association.klass, associated_key)
108
- end
105
+ # @return [Array<ActiveRecord::ConnectionAdapters::Column>]
106
+ def associated_columns
107
+ @associated_columns ||= associated_keys.map do |associated_key|
108
+ if belongs_to_association?
109
+ column(association.active_record, associated_key)
110
+ else
111
+ column(association.klass, associated_key)
112
+ end
113
+ end
109
114
  end
110
115
 
111
116
  # @return [DatabaseConsistency::Databases::Factory]
@@ -135,6 +140,12 @@ module DatabaseConsistency
135
140
  column.sql_type
136
141
  end
137
142
 
143
+ def covers?(associated_types, primary_types)
144
+ associated_types.zip(primary_types).all? do |associated_type, primary_type|
145
+ associated_type.cover?(primary_type)
146
+ end
147
+ end
148
+
138
149
  # @param [ActiveRecord::ConnectionAdapters::Column]
139
150
  #
140
151
  # @return [DatabaseConsistency::Databases::Types::Base]
@@ -128,7 +128,16 @@ module DatabaseConsistency
128
128
  end
129
129
 
130
130
  def extract_columns(str)
131
- str.scan(/(\w+)/).flatten
131
+ case str
132
+ when Array
133
+ str.map(&:to_s)
134
+ when String
135
+ str.scan(/(\w+)/).flatten
136
+ when Symbol
137
+ [str.to_s]
138
+ else
139
+ raise "Unexpected type for columns: #{str.class} with value: #{str}"
140
+ end
132
141
  end
133
142
 
134
143
  def foreign_key_or_attribute(model, attribute)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '2.1.1'
4
+ VERSION = '2.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_consistency
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-12-10 00:00:00.000000000 Z
11
+ date: 2026-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -275,7 +275,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
275
275
  - !ruby/object:Gem::Version
276
276
  version: '0'
277
277
  requirements: []
278
- rubygems_version: 3.3.22
278
+ rubygems_version: 3.2.33
279
279
  signing_key:
280
280
  specification_version: 4
281
281
  summary: Provide an easy way to check the consistency of the database constraints