2023-04-23 21:23:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module SchemaValidation
|
|
|
|
class StructureSql
|
2023-05-27 22:25:52 +05:30
|
|
|
DEFAULT_SCHEMA = 'public'
|
|
|
|
|
|
|
|
def initialize(structure_file_path, schema_name = DEFAULT_SCHEMA)
|
2023-04-23 21:23:45 +05:30
|
|
|
@structure_file_path = structure_file_path
|
2023-05-27 22:25:52 +05:30
|
|
|
@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?
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def indexes
|
2023-05-27 22:25:52 +05:30
|
|
|
@indexes ||= map_with_default_schema(index_statements, SchemaObjects::Index)
|
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def triggers
|
|
|
|
@triggers ||= map_with_default_schema(trigger_statements, SchemaObjects::Trigger)
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
attr_reader :structure_file_path, :schema_name
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
def index_statements
|
2023-05-27 22:25:52 +05:30
|
|
|
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
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def parsed_structure_file
|
|
|
|
PgQuery.parse(File.read(structure_file_path))
|
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
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
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|