From a2071f2b8bd0888b5067dfd0c538e336280c5f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D=C3=A1vila?= Date: Thu, 19 Apr 2018 19:05:53 -0500 Subject: Fix missing namespace for some internal users --- ..._create_missing_namespace_for_internal_users.rb | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 db/migrate/20180413022611_create_missing_namespace_for_internal_users.rb (limited to 'db') diff --git a/db/migrate/20180413022611_create_missing_namespace_for_internal_users.rb b/db/migrate/20180413022611_create_missing_namespace_for_internal_users.rb new file mode 100644 index 00000000000..8fc558be733 --- /dev/null +++ b/db/migrate/20180413022611_create_missing_namespace_for_internal_users.rb @@ -0,0 +1,66 @@ +class CreateMissingNamespaceForInternalUsers < ActiveRecord::Migration + DOWNTIME = false + + def up + connection.exec_query(users_query.to_sql).rows.each do |id, username| + create_namespace(id, username) + # When testing locally I've noticed that these internal users are missing + # the notification email, for more details visit the below link: + # https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/18357#note_68327560 + set_notification_email(id) + end + end + + def down + # no-op + end + + private + + def users + @users ||= Arel::Table.new(:users) + end + + def namespaces + @namespaces ||= Arel::Table.new(:namespaces) + end + + def users_query + condition = users[:ghost].eq(true) + + if column_exists?(:users, :support_bot) + condition = condition.or(users[:support_bot].eq(true)) + end + + users.join(namespaces, Arel::Nodes::OuterJoin) + .on(namespaces[:type].eq(nil).and(namespaces[:owner_id].eq(users[:id]))) + .where(namespaces[:owner_id].eq(nil)) + .where(condition) + .project(users[:id], users[:username]) + end + + def create_namespace(user_id, username) + path = Uniquify.new.string(username) do |str| + query = "SELECT id FROM namespaces WHERE parent_id IS NULL AND path='#{str}' LIMIT 1" + connection.exec_query(query).present? + end + + insert_query = "INSERT INTO namespaces(owner_id, path, name) VALUES(#{user_id}, '#{path}', '#{path}')" + namespace_id = connection.insert_sql(insert_query) + + create_route(namespace_id) + end + + def create_route(namespace_id) + return unless namespace_id + + row = connection.exec_query("SELECT id, path FROM namespaces WHERE id=#{namespace_id}").first + id, path = row.values_at('id', 'path') + + execute("INSERT INTO routes(source_id, source_type, path, name) VALUES(#{id}, 'Namespace', '#{path}', '#{path}')") + end + + def set_notification_email(user_id) + execute "UPDATE users SET notification_email = email WHERE notification_email IS NULL AND id = #{user_id}" + end +end -- cgit v1.2.1 From 023d4f6f2f3d88d0966fe01e6ef921fd03a309fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 29 Mar 2018 11:47:54 +0200 Subject: Move spec helpers/matchers/shared examples/contexts to their relevant folder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- db/fixtures/development/17_cycle_analytics.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'db') diff --git a/db/fixtures/development/17_cycle_analytics.rb b/db/fixtures/development/17_cycle_analytics.rb index d7be6f5950f..7b9a4bad449 100644 --- a/db/fixtures/development/17_cycle_analytics.rb +++ b/db/fixtures/development/17_cycle_analytics.rb @@ -1,5 +1,5 @@ require './spec/support/sidekiq' -require './spec/support/test_env' +require './spec/support/helpers/test_env' class Gitlab::Seeder::CycleAnalytics def initialize(project, perf: false) -- cgit v1.2.1