summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb
blob: 4ba78727cc3951f951e502741e8d438d21918d71 (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
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class RenameUsersWithRenamedNamespace < ActiveRecord::Migration[4.2]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  DISALLOWED_ROOT_PATHS = %w[
    abuse_reports
    api
    autocomplete
    explore
    health_check
    import
    invites
    jwt
    koding
    member
    notification_settings
    oauth
    sent_notifications
    unicorn_test
    uploads
    users
  ]

  def up
    DISALLOWED_ROOT_PATHS.each do |path|
      users = Arel::Table.new(:users)
      namespaces = Arel::Table.new(:namespaces)
      predicate = namespaces[:owner_id].eq(users[:id])
                    .and(namespaces[:type].eq(nil))
                    .and(users[:username].matches(path))

      update_sql = if Gitlab::Database.postgresql?
                     "UPDATE users SET username = namespaces.path "\
                     "FROM namespaces WHERE #{predicate.to_sql}"
                   else
                     "UPDATE users INNER JOIN namespaces "\
                     "ON namespaces.owner_id = users.id "\
                     "SET username = namespaces.path "\
                     "WHERE #{predicate.to_sql}"
                   end

      connection.execute(update_sql)
    end
  end

  def down
  end
end