diff options
Diffstat (limited to 'db')
-rw-r--r-- | db/fixtures/development/09_issues.rb | 2 | ||||
-rw-r--r-- | db/migrate/20170320171632_create_issue_assignees_table.rb | 40 | ||||
-rw-r--r-- | db/migrate/20170320173259_migrate_assignees.rb | 52 | ||||
-rw-r--r-- | db/schema.rb | 10 |
4 files changed, 103 insertions, 1 deletions
diff --git a/db/fixtures/development/09_issues.rb b/db/fixtures/development/09_issues.rb index d93d133d157..0b32a461d56 100644 --- a/db/fixtures/development/09_issues.rb +++ b/db/fixtures/development/09_issues.rb @@ -8,7 +8,7 @@ Gitlab::Seeder.quiet do description: FFaker::Lorem.sentence, state: ['opened', 'closed'].sample, milestone: project.milestones.sample, - assignee: project.team.users.sample + assignees: [project.team.users.sample] } Issues::CreateService.new(project, project.team.users.sample, issue_params).execute diff --git a/db/migrate/20170320171632_create_issue_assignees_table.rb b/db/migrate/20170320171632_create_issue_assignees_table.rb new file mode 100644 index 00000000000..23b8da37b6d --- /dev/null +++ b/db/migrate/20170320171632_create_issue_assignees_table.rb @@ -0,0 +1,40 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class CreateIssueAssigneesTable < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + INDEX_NAME = 'index_issue_assignees_on_issue_id_and_user_id' + + # Set this constant to true if this migration requires downtime. + DOWNTIME = false + + # When a migration requires downtime you **must** uncomment the following + # constant and define a short and easy to understand explanation as to why the + # migration requires downtime. + # DOWNTIME_REASON = '' + + # When using the methods "add_concurrent_index" or "add_column_with_default" + # you must disable the use of transactions as these methods can not run in an + # existing transaction. When using "add_concurrent_index" make sure that this + # method is the _only_ method called in the migration, any other changes + # should go in a separate migration. This ensures that upon failure _only_ the + # index creation fails and can be retried or reverted easily. + # + # To disable transactions uncomment the following line and remove these + # comments: + # disable_ddl_transaction! + + def up + create_table :issue_assignees do |t| + t.references :user, foreign_key: { on_delete: :cascade }, index: true, null: false + t.references :issue, foreign_key: { on_delete: :cascade }, null: false + end + + add_index :issue_assignees, [:issue_id, :user_id], unique: true, name: INDEX_NAME + end + + def down + drop_table :issue_assignees + end +end diff --git a/db/migrate/20170320173259_migrate_assignees.rb b/db/migrate/20170320173259_migrate_assignees.rb new file mode 100644 index 00000000000..ba8edbd7d32 --- /dev/null +++ b/db/migrate/20170320173259_migrate_assignees.rb @@ -0,0 +1,52 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class MigrateAssignees < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + # Set this constant to true if this migration requires downtime. + DOWNTIME = false + + # When a migration requires downtime you **must** uncomment the following + # constant and define a short and easy to understand explanation as to why the + # migration requires downtime. + # DOWNTIME_REASON = '' + + # When using the methods "add_concurrent_index" or "add_column_with_default" + # you must disable the use of transactions as these methods can not run in an + # existing transaction. When using "add_concurrent_index" make sure that this + # method is the _only_ method called in the migration, any other changes + # should go in a separate migration. This ensures that upon failure _only_ the + # index creation fails and can be retried or reverted easily. + # + # To disable transactions uncomment the following line and remove these + # comments: + disable_ddl_transaction! + + def up + # Optimisation: this accounts for most of the invalid assignee IDs on GitLab.com + update_column_in_batches(:issues, :assignee_id, nil) do |table, query| + query.where(table[:assignee_id].eq(0)) + end + + users = Arel::Table.new(:users) + + update_column_in_batches(:issues, :assignee_id, nil) do |table, query| + query.where(table[:assignee_id].not_eq(nil)\ + .and( + users.project("true").where(users[:id].eq(table[:assignee_id])).exists.not + )) + end + + execute <<-EOF + INSERT INTO issue_assignees(issue_id, user_id) + SELECT id, assignee_id FROM issues WHERE assignee_id IS NOT NULL + EOF + end + + def down + execute <<-EOF + DELETE FROM issue_assignees + EOF + end +end diff --git a/db/schema.rb b/db/schema.rb index 132549ab34c..7e8ff9ed67f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -452,6 +452,14 @@ ActiveRecord::Schema.define(version: 20170503004425) do add_index "identities", ["user_id"], name: "index_identities_on_user_id", using: :btree + create_table "issue_assignees", force: :cascade do |t| + t.integer "user_id", null: false + t.integer "issue_id", null: false + end + + add_index "issue_assignees", ["issue_id", "user_id"], name: "index_issue_assignees_on_issue_id_and_user_id", unique: true, using: :btree + add_index "issue_assignees", ["user_id"], name: "index_issue_assignees_on_user_id", using: :btree + create_table "issue_metrics", force: :cascade do |t| t.integer "issue_id", null: false t.datetime "first_mentioned_in_commit_at" @@ -1386,6 +1394,8 @@ ActiveRecord::Schema.define(version: 20170503004425) do add_foreign_key "ci_trigger_requests", "ci_triggers", column: "trigger_id", name: "fk_b8ec8b7245", on_delete: :cascade add_foreign_key "ci_trigger_schedules", "ci_triggers", column: "trigger_id", name: "fk_90a406cc94", on_delete: :cascade add_foreign_key "ci_triggers", "users", column: "owner_id", name: "fk_e8e10d1964", on_delete: :cascade + add_foreign_key "issue_assignees", "issues", on_delete: :cascade + add_foreign_key "issue_assignees", "users", on_delete: :cascade add_foreign_key "container_repositories", "projects" add_foreign_key "issue_metrics", "issues", on_delete: :cascade add_foreign_key "label_priorities", "labels", on_delete: :cascade |