blob: cb62af8d8b88f45e1b9179b50cf0631d7abf3f7b (
plain)
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
|
# frozen_string_literal: true
module Gitlab
module Database
module SchemaValidation
class StructureSql
DEFAULT_SCHEMA = 'public'
def initialize(structure_file_path, schema_name = DEFAULT_SCHEMA)
@structure_file_path = structure_file_path
@schema_name = schema_name
end
def index_exists?(index_name)
indexes.find { |index| index.name == index_name }.present?
end
def trigger_exists?(trigger_name)
triggers.find { |trigger| trigger.name == trigger_name }.present?
end
def indexes
@indexes ||= map_with_default_schema(index_statements, SchemaObjects::Index)
end
def triggers
@triggers ||= map_with_default_schema(trigger_statements, SchemaObjects::Trigger)
end
private
attr_reader :structure_file_path, :schema_name
def index_statements
statements.filter_map { |s| s.stmt.index_stmt }
end
def trigger_statements
statements.filter_map { |s| s.stmt.create_trig_stmt }
end
def statements
@statements ||= parsed_structure_file.tree.stmts
end
def parsed_structure_file
PgQuery.parse(File.read(structure_file_path))
end
def map_with_default_schema(statements, validation_class)
statements.map do |statement|
statement.relation.schemaname = schema_name if statement.relation.schemaname == ''
validation_class.new(statement)
end
end
end
end
end
end
|