summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/schema_migrations/migrations.rb
blob: 3b16b7f1b81b4c9340e3b30a57fdaa214c2438c3 (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
# frozen_string_literal: true

module Gitlab
  module Database
    module SchemaMigrations
      class Migrations
        MIGRATION_VERSION_GLOB = '20[0-9][0-9]*'

        def initialize(context)
          @context = context
        end

        def touch_all
          return unless @context.versions_to_create.any?

          version_filepaths = version_filenames.map { |f| File.join(schema_directory, f) }
          FileUtils.rm(version_filepaths)

          @context.versions_to_create.each do |version|
            version_filepath = File.join(schema_directory, version)

            File.open(version_filepath, 'w') do |file|
              file << Digest::SHA256.hexdigest(version)
            end
          end
        end

        def load_all
          return if version_filenames.empty?

          values = version_filenames.map { |vf| "('#{@context.connection.quote_string(vf)}')" }

          @context.connection.execute(<<~SQL)
          INSERT INTO schema_migrations (version)
          VALUES #{values.join(',')}
          ON CONFLICT DO NOTHING
          SQL
        end

        private

        def schema_directory
          @context.schema_directory
        end

        def version_filenames
          @version_filenames ||= Dir.glob(MIGRATION_VERSION_GLOB, base: schema_directory)
        end
      end
    end
  end
end