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

20240106000000_migrate_data_from_workspaces_url_column.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5f5ffc376e1050dd6493fa97f60331d31c7895e (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
# frozen_string_literal: true

class MigrateDataFromWorkspacesUrlColumn < Gitlab::Database::Migration[2.2]
  BATCH_SIZE = 500
  DEFAULT_PORT = 60001

  milestone '16.8'
  disable_ddl_transaction!

  restrict_gitlab_migration gitlab_schema: :gitlab_main

  def up
    each_batch_range('workspaces', scope: ->(table) { table.all }, of: BATCH_SIZE) do |min, max|
      execute(<<~SQL)
        UPDATE workspaces
        SET url_prefix = CONCAT('https://#{DEFAULT_PORT}-', name),
            dns_zone = remote_development_agent_configs.dns_zone,
            url_query_string = CASE
                                    WHEN POSITION('?' IN url) > 0
                                    THEN SUBSTRING(url FROM POSITION('?' IN url) + 1)
                                    ELSE ''
                                END
        FROM
            remote_development_agent_configs
        WHERE
            workspaces.cluster_agent_id = remote_development_agent_configs.cluster_agent_id
        AND url IS NOT NULL
        AND workspaces.id BETWEEN #{min} AND #{max}
      SQL

      execute(<<~SQL)
        UPDATE workspaces
        SET url = NULL
        WHERE workspaces.id BETWEEN #{min} AND #{max}
        AND url_prefix IS NOT NULL
        AND dns_zone IS NOT NULL
        AND url_query_string IS NOT NULL
      SQL
    end
  end

  def down
    each_batch_range('workspaces', scope: ->(table) { table.all }, of: BATCH_SIZE) do |min, max|
      execute(<<~SQL)
        UPDATE workspaces
        SET url = CONCAT(url_prefix, '.', dns_zone, '?', url_query_string)
        WHERE workspaces.id BETWEEN #{min} AND #{max}
      SQL

      execute(<<~SQL)
        UPDATE workspaces
        SET url_prefix = NULL,
            dns_zone = NULL,
            url_query_string = NULL
        WHERE workspaces.id BETWEEN #{min} AND #{max}
      SQL
    end
  end
end