summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@gitlab.com>2017-05-18 23:31:03 +0200
committerBob Van Landuyt <bob@gitlab.com>2017-05-19 07:21:35 +0200
commitdf5c3f364a3d415e35a2da462f044b08b854285b (patch)
treebbbd98f2b9690576cc8e861deb00db25c37e92ff
parente4eec191565a053f687911d80865ad43241453f8 (diff)
downloadgitlab-ce-df5c3f364a3d415e35a2da462f044b08b854285b.tar.gz
Rename users that had their namespace renamed
-rw-r--r--db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb43
-rw-r--r--db/schema.rb2
-rw-r--r--spec/migrations/rename_users_with_renamed_namespace_spec.rb19
3 files changed, 63 insertions, 1 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..a0444f4cc62
--- /dev/null
+++ b/db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb
@@ -0,0 +1,43 @@
+# 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
+ namespace_table = Arel::Table.new('namespaces')
+ users_table = Arel::Table.new('users')
+ matching_path = namespace_table.project(namespace_table[:path])
+ .join(users_table).on(users_table[:id].eq(namespace_table[:owner_id]))
+ .where(users_table[:username].not_eq(namespace_table[:path]))
+ path_name = Arel::Nodes::SqlLiteral.new("matching_path.path FROM (#{matching_path.to_sql}) as matching_path")
+
+ update_column_in_batches(:users, :username, path_name) do |table, query|
+ query.where(table[:username].matches_any(DISALLOWED_ROOT_PATHS))
+ end
+ end
+
+ def down
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 294e0b531eb..8c7da682807 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170516183131) do
+ActiveRecord::Schema.define(version: 20170518200835) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
diff --git a/spec/migrations/rename_users_with_renamed_namespace_spec.rb b/spec/migrations/rename_users_with_renamed_namespace_spec.rb
new file mode 100644
index 00000000000..aefa539094f
--- /dev/null
+++ b/spec/migrations/rename_users_with_renamed_namespace_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+require Rails.root.join('db', 'post_migrate', '20170518200835_rename_users_with_renamed_namespace.rb')
+
+describe RenameUsersWithRenamedNamespace, truncate: true do
+ it 'renames a user that had his namespace renamed to the namespace path' do
+ other_user = create(:user, username: 'kodingu')
+
+ user = create(:user, username: "Users0")
+ user.update_attribute(:username, 'Users')
+ user1 = create(:user, username: "import0")
+ user1.update_attribute(:username, 'import')
+
+ described_class.new.up
+
+ expect(user.reload.username).to eq('Users0')
+ expect(user1.reload.username).to eq('import0')
+ expect(other_user.reload.username).to eq('kodingu')
+ end
+end