Welcome to mirror list, hosted at ThFree Co, Russian Federation.

different_definition_tables.rb « validators « validation « schema « gitlab « lib « gitlab-schema-validation « gems - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f6892a76a12cdc74948480a9932fd92c8f8862c5 (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
# frozen_string_literal: true

module Gitlab
  module Schema
    module Validation
      module Validators
        class DifferentDefinitionTables < Base
          ERROR_MESSAGE = "The table %s has a different column statement between structure.sql and database"

          def execute
            structure_sql.tables.filter_map do |structure_sql_table|
              table_name = structure_sql_table.name
              database_table = database.fetch_table_by_name(table_name)

              next unless database_table

              db_diffs, structure_diffs = column_diffs(database_table, structure_sql_table.columns)

              if db_diffs.any?
                build_inconsistency(self.class,
                  SchemaObjects::Table.new(table_name, db_diffs),
                  SchemaObjects::Table.new(table_name, structure_diffs))
              end
            end
          end

          private

          def column_diffs(db_table, columns)
            db_diffs = []
            structure_diffs = []

            columns.each do |column|
              db_column = db_table.fetch_column_by_name(column.name)

              next unless db_column

              next if db_column.statement == column.statement

              db_diffs << db_column
              structure_diffs << column
            end

            [db_diffs, structure_diffs]
          end
        end
      end
    end
  end
end