summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb')
-rw-r--r--db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb50
1 files changed, 50 insertions, 0 deletions
diff --git a/db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb b/db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb
new file mode 100644
index 00000000000..da0fcda87a6
--- /dev/null
+++ b/db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb
@@ -0,0 +1,50 @@
+# 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
+ 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