Class: Gitlab::Database::RenameReservedPathsMigration::V1::RenameBase

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb

Direct Known Subclasses

RenameNamespaces, RenameProjects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths, migration) ⇒ RenameBase

Returns a new instance of RenameBase.


17
18
19
20
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 17

def initialize(paths, migration)
  @paths = paths
  @migration = migration
end

Instance Attribute Details

#migrationObject (readonly)

Returns the value of attribute migration


8
9
10
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 8

def migration
  @migration
end

#pathsObject (readonly)

Returns the value of attribute paths


8
9
10
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 8

def paths
  @paths
end

Instance Method Details

#file_storage?Boolean

Returns:

  • (Boolean)

181
182
183
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 181

def file_storage?
  CarrierWave::Uploader::Base.storage == CarrierWave::Storage::File
end

#join_routable_path(namespace_path, top_level) ⇒ Object


87
88
89
90
91
92
93
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 87

def join_routable_path(namespace_path, top_level)
  if namespace_path.present?
    File.join(namespace_path, top_level)
  else
    top_level
  end
end

#move_folders(directory, old_relative_path, new_relative_path) ⇒ Object


109
110
111
112
113
114
115
116
117
118
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 109

def move_folders(directory, old_relative_path, new_relative_path)
  old_path = File.join(directory, old_relative_path)
  unless File.directory?(old_path)
    say "#{old_path} doesn't exist, skipping"
    return
  end

  new_path = File.join(directory, new_relative_path)
  FileUtils.mv(old_path, new_path)
end

#move_pages(old_path, new_path) ⇒ Object


99
100
101
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 99

def move_pages(old_path, new_path)
  move_folders(pages_dir, old_path, new_path)
end

#move_uploads(old_path, new_path) ⇒ Object


103
104
105
106
107
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 103

def move_uploads(old_path, new_path)
  return unless file_storage?

  move_folders(uploads_dir, old_path, new_path)
end

#pages_dirObject


189
190
191
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 189

def pages_dir
  Settings.pages.path
end

#path_patternsObject


22
23
24
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 22

def path_patterns
  @path_patterns ||= paths.flat_map { |path| ["%/#{path}", path] }
end

#perform_rename(routable, old_full_path, new_full_path) ⇒ Object


39
40
41
42
43
44
45
46
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 39

def perform_rename(routable, old_full_path, new_full_path)
  # skips callbacks & validations
  new_path = new_full_path.split('/').last
  routable.class.where(id: routable)
    .update_all(path: new_path)

  rename_routes(old_full_path, new_full_path)
end

#redis_key_for_type(type) ⇒ Object


177
178
179
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 177

def redis_key_for_type(type)
  "rename:#{migration.name}:#{type}"
end

#remove_cached_html_for_projects(project_ids) ⇒ Object


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 120

def remove_cached_html_for_projects(project_ids)
  project_ids.each do |project_id|
    update_column_in_batches(:projects, :description_html, nil) do |table, query|
      query.where(table[:id].eq(project_id))
    end

    update_column_in_batches(:issues, :description_html, nil) do |table, query|
      query.where(table[:project_id].eq(project_id))
    end

    update_column_in_batches(:merge_requests, :description_html, nil) do |table, query|
      query.where(table[:target_project_id].eq(project_id))
    end

    update_column_in_batches(:notes, :note_html, nil) do |table, query|
      query.where(table[:project_id].eq(project_id))
    end

    update_column_in_batches(:milestones, :description_html, nil) do |table, query|
      query.where(table[:project_id].eq(project_id))
    end
  end
end

#remove_last_occurrence(string, pattern) ⇒ Object


83
84
85
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 83

def remove_last_occurrence(string, pattern)
  string.reverse.sub(pattern.reverse, "").reverse
end

#rename_path(namespace_path, path_was) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 71

def rename_path(namespace_path, path_was)
  counter = 0
  path = "#{path_was}#{counter}"

  while route_exists?(join_routable_path(namespace_path, path))
    counter += 1
    path = "#{path_was}#{counter}"
  end

  path
end

#rename_path_for_routable(routable) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 26

def rename_path_for_routable(routable)
  old_path = routable.path
  old_full_path = routable.full_path
  # Only remove the last occurrence of the path name to get the parent namespace path
  namespace_path = remove_last_occurrence(old_full_path, old_path)
  new_path = rename_path(namespace_path, old_path)
  new_full_path = join_routable_path(namespace_path, new_path)

  perform_rename(routable, old_full_path, new_full_path)

  [old_full_path, new_full_path]
end

#rename_routes(old_full_path, new_full_path) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 48

def rename_routes(old_full_path, new_full_path)
  routes = Route.arel_table

  quoted_old_full_path = quote_string(old_full_path)
  quoted_old_wildcard_path = quote_string("#{old_full_path}/%")

  filter =
    "routes.id IN "\
    "( SELECT routes.id FROM routes WHERE lower(routes.path) = lower('#{quoted_old_full_path}') "\
    "UNION SELECT routes.id FROM routes WHERE routes.path ILIKE '#{quoted_old_wildcard_path}' )"

  replace_statement = replace_sql(Route.arel_table[:path],
                                  old_full_path,
                                  new_full_path)

  update = Arel::UpdateManager.new
    .table(routes)
    .set([[routes[:path], replace_statement]])
    .where(Arel::Nodes::SqlLiteral.new(filter))

  execute(update.to_sql)
end

#reverts_for_type(type) ⇒ Object


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 153

def reverts_for_type(type)
  key = redis_key_for_type(type)

  Gitlab::Redis::SharedState.with do |redis|
    failed_reverts = []

    while rename_info = redis.lpop(key)
      path_before_rename, path_after_rename = Gitlab::Json.parse(rename_info)
      say "renaming #{type} from #{path_after_rename} back to #{path_before_rename}"
      begin
        yield(path_before_rename, path_after_rename)
      rescue StandardError => e
        failed_reverts << rename_info
        say "Renaming #{type} from #{path_after_rename} back to "\
            "#{path_before_rename} failed. Review the error and try "\
            "again by running the `down` action. \n"\
            "#{e.message}: \n #{e.backtrace.join("\n")}"
      end
    end

    failed_reverts.each { |rename_info| redis.lpush(key, rename_info) }
  end
end

#route_exists?(full_path) ⇒ Boolean

Returns:

  • (Boolean)

95
96
97
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 95

def route_exists?(full_path)
  MigrationClasses::Route.where(Route.arel_table[:path].matches(full_path)).any?
end

#track_rename(type, old_path, new_path) ⇒ Object


144
145
146
147
148
149
150
151
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 144

def track_rename(type, old_path, new_path)
  key = redis_key_for_type(type)
  Gitlab::Redis::SharedState.with do |redis|
    redis.lpush(key, [old_path, new_path].to_json)
    redis.expire(key, 2.weeks.to_i)
  end
  say "tracked rename: #{key}: #{old_path} -> #{new_path}"
end

#uploads_dirObject


185
186
187
# File 'lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb', line 185

def uploads_dir
  File.join(CarrierWave.root, "uploads")
end