Commit 73be26e8 authored by Peter Leitzen's avatar Peter Leitzen

DB: Add `users.bot_type` and index it

Stop using `users.support_bot` which we will remove in a upcoming
release.
parent 3bfb7df6
......@@ -3243,8 +3243,10 @@ ActiveRecord::Schema.define(version: 20190404231137) do
t.string "commit_email"
t.integer "group_view"
t.integer "managing_group_id"
t.integer "bot_type", limit: 2
t.index ["accepted_term_id"], name: "index_users_on_accepted_term_id", using: :btree
t.index ["admin"], name: "index_users_on_admin", using: :btree
t.index ["bot_type"], name: "index_users_on_bot_type", using: :btree
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree
t.index ["created_at"], name: "index_users_on_created_at", using: :btree
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
......@@ -3258,6 +3260,7 @@ ActiveRecord::Schema.define(version: 20190404231137) do
t.index ["name"], name: "index_users_on_name_trigram", using: :gin, opclasses: {"name"=>"gin_trgm_ops"}
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
t.index ["state"], name: "index_users_on_state", using: :btree
t.index ["state"], name: "index_users_on_state_and_internal", where: "((ghost <> true) AND (bot_type IS NULL))", using: :btree
t.index ["state"], name: "index_users_on_state_and_internal_attrs", where: "((ghost <> true) AND (support_bot <> true))", using: :btree
t.index ["support_bot"], name: "index_users_on_support_bot", using: :btree
t.index ["username"], name: "index_users_on_username", using: :btree
......
# frozen_string_literal: true
class AddUsersBotType < ActiveRecord::Migration[5.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
change_table :users do |t|
t.integer :bot_type, limit: 2
end
end
end
# frozen_string_literal: true
class IndexUsersBotType < ActiveRecord::Migration[5.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
class User < ActiveRecord::Base
enum bot_type: { support_bot: 1 }
end
def up
add_concurrent_index :users, :bot_type
add_concurrent_index :users, :state,
name: internal_index,
where: 'ghost <> true AND bot_type IS NULL'
User
.where(support_bot: true)
.update_all(bot_type: User.bot_types[:support_bot])
end
def down
User
.where(bot_type: User.bot_types[:support_bot])
.update_all(support_bot: true)
remove_concurrent_index :users, :state, name: internal_index
remove_concurrent_index :users, :bot_type
end
private
def internal_index
'index_users_on_state_and_internal'
end
end
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('ee', 'db', 'migrate', '20190401150746_index_users_bot_type.rb')
describe IndexUsersBotType, :migration do
BOT_TYPE = {
support_bot: 1
}.freeze
let(:migration) { described_class.new }
let(:users) { table(:users) }
let!(:user) { create_user(username: 'test') }
describe '#up' do
let!(:support_bot) do
create_user(username: 'support_bot', support_bot: true)
end
it 'converts support_bot column to enum and adds indexes' do
migration.up
expect(user.reload.bot_type).to be_nil
expect(support_bot.reload.support_bot).to eq(true)
expect(support_bot.reload.bot_type).to eq(BOT_TYPE[:support_bot])
expect(index_exists?(:bot_type)).to eq(true)
expect(index_exists?(:state, name: internal_index)).to eq(true)
end
end
describe '#down' do
let!(:support_bot) do
create_user(username: 'support_bot', bot_type: BOT_TYPE[:support_bot])
end
it 'converts support_bot column to enum and removes indexes' do
migration.down
expect(user.reload.support_bot).to be_nil
expect(support_bot.reload.support_bot).to eq(true)
expect(index_exists?(:bot_type)).to eq(false)
expect(index_exists?(:state, name: internal_index)).to eq(false)
end
end
private
def create_user(username:, **params)
users.create!(
email: "#{username}@example.com",
projects_limit: 0,
username: username,
**params
)
end
def index_exists?(column, options = {})
migration.index_exists?(:users, column, options)
end
def internal_index
'index_users_on_state_and_internal'
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment