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

squasher.rb « migrations « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 98fdf873aa5a78931c5c47b6dc8b637d2bd72ba0 (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
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

require 'set'

module Gitlab
  module Database
    module Migrations
      class Squasher
        RSPEC_FILENAME_REGEXP = /\A([0-9]+_)?([_a-z0-9]*)\.rb\z/

        def initialize(git_output)
          @migration_data = migration_files_from_git(git_output).filter_map do |mf|
            basename = Pathname(mf).basename.to_s
            file_name_match = ActiveRecord::Migration::MigrationFilenameRegexp.match(basename)
            slug = file_name_match[2]
            unless slug == 'init_schema'
              {
                path: mf,
                basename: basename,
                timestamp: file_name_match[1],
                slug: slug
              }
            end
          end
        end

        def files_to_delete
          @migration_data.pluck(:path) + schema_migrations + find_migration_specs
        end

        private

        def schema_migrations
          @migration_data.map { |m| "db/schema_migrations/#{m[:timestamp]}" }
        end

        def find_migration_specs
          @file_slugs = Set.new @migration_data.pluck(:slug)
          (migration_specs + ee_migration_specs).select { |f| file_has_slug?(f) }
        end

        def migration_files_from_git(body)
          body.chomp
              .split("\n")
              .select { |fn| fn.end_with?('.rb') }
        end

        def match_file_slug(filename)
          m = RSPEC_FILENAME_REGEXP.match(filename)
          return if m.nil?

          m[2].sub(/_spec$/, '')
        end

        def file_has_slug?(filename)
          spec_slug = match_file_slug(Pathname(filename).basename.to_s)
          return false if spec_slug.nil?

          @file_slugs.include?(spec_slug)
        end

        def migration_specs
          Dir.glob(Rails.root.join('spec/migrations/*.rb'))
        end

        def ee_migration_specs
          Dir.glob(Rails.root.join('ee/spec/migrations/*.rb'))
        end
      end
    end
  end
end