/
schema.rb
190 lines (150 loc) · 4.27 KB
/
schema.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# frozen_string_literal: true
module RuboCop
module Rails
module SchemaLoader
# Represent db/schema.rb
class Schema
attr_reader :tables, :add_indicies
def initialize(ast)
@tables = []
@add_indicies = []
build!(ast)
end
def table_by(name:)
tables.find do |table|
table.name == name
end
end
def add_indicies_by(table_name:)
add_indicies.select do |add_index|
add_index.table_name == table_name
end
end
private
def build!(ast)
raise "Unexpected type: #{ast.type}" unless ast.block_type?
each_table(ast) do |table_def|
@tables << Table.new(table_def)
end
# Compatibility for Rails 4.2.
each_add_index(ast) do |add_index_def|
@add_indicies << AddIndex.new(add_index_def)
end
end
def each_table(ast)
case ast.body.type
when :begin
ast.body.children.each do |node|
next unless node.block_type? && node.method?(:create_table)
yield(node)
end
else
yield ast.body
end
end
def each_add_index(ast)
ast.body.children.each do |node|
next if !node&.send_type? || !node.method?(:add_index)
yield(node)
end
end
end
# Represent a table
class Table
attr_reader :name, :columns, :indices
def initialize(node)
@name = node.send_node.first_argument.value
@columns = build_columns(node)
@indices = build_indices(node)
end
def with_column?(name:)
@columns.any? { |c| c.name == name }
end
private
def build_columns(node)
each_content(node).map do |child|
next unless child&.send_type?
next if child.method?(:index)
Column.new(child)
end.compact
end
def build_indices(node)
each_content(node).map do |child|
next unless child&.send_type?
next unless child.method?(:index)
Index.new(child)
end.compact
end
def each_content(node, &block)
return enum_for(__method__, node) unless block_given?
case node.body&.type
when :begin
node.body.children.each(&block)
else
yield(node.body)
end
end
end
# Represent a column
class Column
attr_reader :name, :type, :not_null
def initialize(node)
@name = node.first_argument.value
@type = node.method_name
@not_null = nil
analyze_keywords!(node)
end
private
def analyze_keywords!(node)
pairs = node.arguments.last
return unless pairs.hash_type?
pairs.each_pair do |k, v|
if k.value == :null
@not_null = v.true_type? ? false : true
end
end
end
end
# Represent an index
class Index
attr_reader :name, :columns, :expression, :unique
def initialize(node)
@columns, @expression = build_columns_or_expr(node.first_argument)
@unique = nil
analyze_keywords!(node)
end
private
def build_columns_or_expr(columns)
if columns.array_type?
[columns.values.map(&:value), nil]
else
[[], columns.value]
end
end
def analyze_keywords!(node)
pairs = node.arguments.last
return unless pairs.hash_type?
pairs.each_pair do |k, v|
case k.value
when :name
@name = v.value
when :unique
@unique = true
end
end
end
end
# Represent an `add_index`
class AddIndex < Index
attr_reader :table_name
def initialize(node)
super(node)
@table_name = node.first_argument.value
@columns, @expression = build_columns_or_expr(node.arguments[1])
@unique = nil
analyze_keywords!(node)
end
end
end
end
end