Commit 986f470d authored by Andreas Brandl's avatar Andreas Brandl

Improve migration robustness and speed.

parent 7a22a589
...@@ -9,8 +9,6 @@ class CreateUserContributedProjectsTable < ActiveRecord::Migration ...@@ -9,8 +9,6 @@ class CreateUserContributedProjectsTable < ActiveRecord::Migration
create_table :user_contributed_projects, id: false do |t| create_table :user_contributed_projects, id: false do |t|
t.references :user, null: false t.references :user, null: false
t.references :project, null: false t.references :project, null: false
t.index [:user_id, :project_id], unique: true
end end
add_concurrent_foreign_key :user_contributed_projects, :users, column: :user_id, on_delete: :cascade add_concurrent_foreign_key :user_contributed_projects, :users, column: :user_id, on_delete: :cascade
......
...@@ -12,10 +12,14 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration ...@@ -12,10 +12,14 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration
else else
MysqlStrategy.new MysqlStrategy.new
end.up end.up
add_concurrent_index :user_contributed_projects, [:project_id, :user_id], unique: true
end end
def down def down
execute "TRUNCATE user_contributed_projects" execute "TRUNCATE user_contributed_projects"
remove_concurrent_index_by_name :user_contributed_projects, 'index_user_contributed_projects_on_project_id_and_user_id'
end end
private private
...@@ -24,7 +28,7 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration ...@@ -24,7 +28,7 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
BATCH_SIZE = 100_000 BATCH_SIZE = 100_000
SLEEP_TIME = 30 SLEEP_TIME = 5
def up def up
with_index(:events, [:author_id, :project_id], name: 'events_user_contributions_temp', where: 'project_id IS NOT NULL') do with_index(:events, [:author_id, :project_id], name: 'events_user_contributions_temp', where: 'project_id IS NOT NULL') do
...@@ -44,10 +48,14 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration ...@@ -44,10 +48,14 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration
records += result.cmd_tuples records += result.cmd_tuples
Rails.logger.info "Building user_contributed_projects table, batch ##{iteration} complete, created #{records} overall" Rails.logger.info "Building user_contributed_projects table, batch ##{iteration} complete, created #{records} overall"
Kernel.sleep(SLEEP_TIME) if result.cmd_tuples > 0 Kernel.sleep(SLEEP_TIME) if result.cmd_tuples > 0
rescue ActiveRecord::InvalidForeignKey => e
Rails.logger.info "Retry on InvalidForeignKey: #{e}"
retry
end while result.cmd_tuples > 0 end while result.cmd_tuples > 0
end end
execute "ANALYZE user_contributed_projects" execute "ANALYZE user_contributed_projects"
end end
private private
......
...@@ -1820,7 +1820,7 @@ ActiveRecord::Schema.define(version: 20180304204842) do ...@@ -1820,7 +1820,7 @@ ActiveRecord::Schema.define(version: 20180304204842) do
t.integer "project_id", null: false t.integer "project_id", null: false
end end
add_index "user_contributed_projects", ["user_id", "project_id"], name: "index_user_contributed_projects_on_user_id_and_project_id", unique: true, using: :btree add_index "user_contributed_projects", ["project_id", "user_id"], name: "index_user_contributed_projects_on_project_id_and_user_id", unique: true, using: :btree
create_table "user_custom_attributes", force: :cascade do |t| create_table "user_custom_attributes", force: :cascade do |t|
t.datetime_with_timezone "created_at", null: false t.datetime_with_timezone "created_at", null: false
......
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