database_consistency 0.8.4 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/database_consistency.rb +5 -0
- data/lib/database_consistency/checkers/association_checkers/foreign_key_type_checker.rb +106 -0
- data/lib/database_consistency/databases/factory.rb +27 -0
- data/lib/database_consistency/databases/types/base.rb +22 -0
- data/lib/database_consistency/databases/types/sqlite.rb +22 -0
- data/lib/database_consistency/processors/associations_processor.rb +2 -1
- data/lib/database_consistency/rescue_error.rb +1 -1
- data/lib/database_consistency/version.rb +1 -1
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed9a2103f6e588628f52a00ddd933b0dc899fac0d6e8752a3bf1111a3894fb17
|
4
|
+
data.tar.gz: d8bf6a551c55fc3e4a33d837354f1b3d777c6171b48aa84d2cce5bbba478e0d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1c59e4de257fdaa7aae63725abcc82f7da1f4ea98f51fc804162b8c5488021c3c665f7e7298b135eaddd50560415b563f0cd608fdaaee81519e44af74966bb2
|
7
|
+
data.tar.gz: 516f5102afb3234eb88d8a8039b584c9210b33d78401290423032cbd017df5218aa6a455ae5faeffb54dd9bad7ea82a17a40f1729255c7ea2ed988924bad3ea9
|
data/lib/database_consistency.rb
CHANGED
@@ -10,10 +10,15 @@ require 'database_consistency/rescue_error'
|
|
10
10
|
require 'database_consistency/writers/base_writer'
|
11
11
|
require 'database_consistency/writers/simple_writer'
|
12
12
|
|
13
|
+
require 'database_consistency/databases/factory'
|
14
|
+
require 'database_consistency/databases/types/base'
|
15
|
+
require 'database_consistency/databases/types/sqlite'
|
16
|
+
|
13
17
|
require 'database_consistency/checkers/base_checker'
|
14
18
|
|
15
19
|
require 'database_consistency/checkers/association_checkers/association_checker'
|
16
20
|
require 'database_consistency/checkers/association_checkers/missing_index_checker'
|
21
|
+
require 'database_consistency/checkers/association_checkers/foreign_key_type_checker'
|
17
22
|
|
18
23
|
require 'database_consistency/checkers/column_checkers/column_checker'
|
19
24
|
require 'database_consistency/checkers/column_checkers/null_constraint_checker'
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Checkers
|
5
|
+
# This class checks if association's foreign key type is the same as associated model's primary key
|
6
|
+
class ForeignKeyTypeChecker < AssociationChecker
|
7
|
+
INCONSISTENT_TYPE = 'associated model key (%a_f) with type (%a_t) mismatches key (%b_f) with type (%b_t)'
|
8
|
+
|
9
|
+
TYPES = {
|
10
|
+
'serial' => 'integer',
|
11
|
+
'integer' => 'integer',
|
12
|
+
'int' => 'integer',
|
13
|
+
'bigserial' => 'bigint',
|
14
|
+
'bigint' => 'bigint',
|
15
|
+
'bigint unsigned' => 'bigint unsigned'
|
16
|
+
}.freeze
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def preconditions
|
21
|
+
!association.polymorphic? && association.klass.present?
|
22
|
+
rescue NameError
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
26
|
+
# Table of possible statuses
|
27
|
+
# | type | status |
|
28
|
+
# | ------------ | ------ |
|
29
|
+
# | consistent | ok |
|
30
|
+
# | inconsistent | fail |
|
31
|
+
def check
|
32
|
+
if converted_type(base_column) == converted_type(associated_column)
|
33
|
+
report_template(:ok)
|
34
|
+
else
|
35
|
+
report_template(:fail, render_text)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [String]
|
40
|
+
def render_text
|
41
|
+
INCONSISTENT_TYPE
|
42
|
+
.gsub('%a_t', type(associated_column))
|
43
|
+
.gsub('%a_f', associated_key)
|
44
|
+
.gsub('%b_t', type(base_column))
|
45
|
+
.gsub('%b_f', base_key)
|
46
|
+
end
|
47
|
+
|
48
|
+
# @return [String]
|
49
|
+
def base_key
|
50
|
+
@base_key ||= belongs_to_association? ? association.foreign_key : association.active_record_primary_key
|
51
|
+
end
|
52
|
+
|
53
|
+
# @return [String]
|
54
|
+
def associated_key
|
55
|
+
@associated_key ||= belongs_to_association? ? association.active_record_primary_key : association.foreign_key
|
56
|
+
end
|
57
|
+
|
58
|
+
# @return [ActiveRecord::ConnectionAdapters::Column]
|
59
|
+
def base_column
|
60
|
+
@base_column ||= column(association.active_record, base_key)
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [ActiveRecord::ConnectionAdapters::Column]
|
64
|
+
def associated_column
|
65
|
+
@associated_column ||= column(association.klass, associated_key)
|
66
|
+
end
|
67
|
+
|
68
|
+
# @return [DatabaseConsistency::Databases::Factory]
|
69
|
+
def database_factory
|
70
|
+
@database_factory ||= Databases::Factory.new(association.active_record.connection.adapter_name)
|
71
|
+
end
|
72
|
+
|
73
|
+
# @param [ActiveRecord::Base] model
|
74
|
+
# @param [String] column_name
|
75
|
+
#
|
76
|
+
# @return [ActiveRecord::ConnectionAdapters::Column]
|
77
|
+
def column(model, column_name)
|
78
|
+
model.connection.columns(model.table_name).find { |column| column.name == column_name }
|
79
|
+
end
|
80
|
+
|
81
|
+
# @param [ActiveRecord::ConnectionAdapters::Column] column
|
82
|
+
#
|
83
|
+
# @return [String]
|
84
|
+
def type(column)
|
85
|
+
column.sql_type
|
86
|
+
end
|
87
|
+
|
88
|
+
# @param [ActiveRecord::ConnectionAdapters::Column]
|
89
|
+
#
|
90
|
+
# @return [String]
|
91
|
+
def converted_type(column)
|
92
|
+
database_factory.type(type(column)).convert
|
93
|
+
end
|
94
|
+
|
95
|
+
# @return [Boolean]
|
96
|
+
def supported_type?(column)
|
97
|
+
database_factory.type(type(column)).supported?
|
98
|
+
end
|
99
|
+
|
100
|
+
# @return [Boolean]
|
101
|
+
def belongs_to_association?
|
102
|
+
association.macro == :belongs_to
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Databases
|
5
|
+
# Factory for database adapters
|
6
|
+
class Factory
|
7
|
+
attr_reader :adapter
|
8
|
+
|
9
|
+
# @param [String] adapter
|
10
|
+
def initialize(adapter)
|
11
|
+
@adapter = adapter
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [DatabaseConsistency::Databases::Types::Base]
|
15
|
+
def type(type)
|
16
|
+
sqlite? ? Types::Sqlite.new(type) : Types::Base.new(type)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
# @return [Boolean]
|
22
|
+
def sqlite?
|
23
|
+
adapter == 'SQLite'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Databases
|
5
|
+
module Types
|
6
|
+
# Base wrapper for database types
|
7
|
+
class Base
|
8
|
+
attr_reader :type
|
9
|
+
|
10
|
+
# @param [String] type
|
11
|
+
def initialize(type)
|
12
|
+
@type = type
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [String]
|
16
|
+
def convert
|
17
|
+
type
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Databases
|
5
|
+
module Types
|
6
|
+
# Wraps types for SQLite database
|
7
|
+
class Sqlite < Base
|
8
|
+
TYPES = {
|
9
|
+
'bigserial' => 'bigint',
|
10
|
+
'bigint' => 'bigint',
|
11
|
+
'serial' => 'integer',
|
12
|
+
'integer' => 'integer'
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
# @return [String]
|
16
|
+
def convert
|
17
|
+
TYPES[type]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
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: 0.8.
|
4
|
+
version: 0.8.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evgeniy Demin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -133,6 +133,7 @@ files:
|
|
133
133
|
- bin/database_consistency
|
134
134
|
- lib/database_consistency.rb
|
135
135
|
- lib/database_consistency/checkers/association_checkers/association_checker.rb
|
136
|
+
- lib/database_consistency/checkers/association_checkers/foreign_key_type_checker.rb
|
136
137
|
- lib/database_consistency/checkers/association_checkers/missing_index_checker.rb
|
137
138
|
- lib/database_consistency/checkers/base_checker.rb
|
138
139
|
- lib/database_consistency/checkers/column_checkers/column_checker.rb
|
@@ -145,6 +146,9 @@ files:
|
|
145
146
|
- lib/database_consistency/checkers/validators_fraction_checkers/column_presence_checker.rb
|
146
147
|
- lib/database_consistency/checkers/validators_fraction_checkers/validators_fraction_checker.rb
|
147
148
|
- lib/database_consistency/configuration.rb
|
149
|
+
- lib/database_consistency/databases/factory.rb
|
150
|
+
- lib/database_consistency/databases/types/base.rb
|
151
|
+
- lib/database_consistency/databases/types/sqlite.rb
|
148
152
|
- lib/database_consistency/helper.rb
|
149
153
|
- lib/database_consistency/processors/associations_processor.rb
|
150
154
|
- lib/database_consistency/processors/base_processor.rb
|
@@ -175,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
179
|
- !ruby/object:Gem::Version
|
176
180
|
version: '0'
|
177
181
|
requirements: []
|
178
|
-
rubygems_version: 3.
|
182
|
+
rubygems_version: 3.0.8
|
179
183
|
signing_key:
|
180
184
|
specification_version: 4
|
181
185
|
summary: Provide an easy way to check the consistency of the database constraints
|