summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Artur <felipefac@gmail.com>2019-04-29 16:20:14 -0300
committerFelipe Artur <felipefac@gmail.com>2019-04-29 16:20:14 -0300
commit8b3db3dafcd11c060be73f01ce0e89eef79c347e (patch)
treeaa58f506da024c75aa78cd0addf6832eeb4961c0
parent3d7279378c4965aa3235962200579475c292a39e (diff)
downloadgitlab-ce-issue_57906.tar.gz
Add post migrationissue_57906
-rw-r--r--db/migrate/20190424180246_add_state_id_indexes.rb2
-rw-r--r--db/post_migrate/20190429112834_remove_state_from_issues_and_merge_requests.rb88
-rw-r--r--db/schema.rb11
-rw-r--r--spec/migrations/remove_state_from_issues_and_merge_requests.rb47
4 files changed, 137 insertions, 11 deletions
diff --git a/db/migrate/20190424180246_add_state_id_indexes.rb b/db/migrate/20190424180246_add_state_id_indexes.rb
index d548c0bc95c..ea6a08a6adc 100644
--- a/db/migrate/20190424180246_add_state_id_indexes.rb
+++ b/db/migrate/20190424180246_add_state_id_indexes.rb
@@ -21,7 +21,7 @@ class AddStateIdIndexes < ActiveRecord::Migration[5.1]
:issues,
[:project_id, :due_date, :id, :state_id],
name: "idx_issues_on_project_id_due_date_id_and_state_id",
- where: "(due_date IS NOT NULL)",
+ where: "(due_date IS NOT NULL)"
)
add_concurrent_index(
diff --git a/db/post_migrate/20190429112834_remove_state_from_issues_and_merge_requests.rb b/db/post_migrate/20190429112834_remove_state_from_issues_and_merge_requests.rb
new file mode 100644
index 00000000000..42f590a591a
--- /dev/null
+++ b/db/post_migrate/20190429112834_remove_state_from_issues_and_merge_requests.rb
@@ -0,0 +1,88 @@
+class RemoveStateFromIssuesAndMergeRequests < ActiveRecord::Migration[5.1]
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ disable_ddl_transaction!
+
+ def issuable_states
+ { "opened" => 1, "closed" => 2, "merged" => 3, "locked" => 4 }
+ end
+
+ def up
+ remove_column :issues, :state
+ remove_column :merge_requests, :state
+ end
+
+ def down
+ add_column :issues, :state, :string
+ add_column :merge_requests, :state, :string
+
+ issuable_states.each do |state_name, state_id|
+ # "merged" and "locked" states does not apply for issues
+ if state_id < 3
+ update_column_in_batches(:issues, :state, state_name) do |table, query|
+ query.where(table[:state_id].eq(state_id))
+ end
+ end
+
+ update_column_in_batches(:merge_requests, :state, state_name) do |table, query|
+ query.where(table[:state_id].eq(state_id))
+ end
+ end
+
+ # Only required for merge requests
+ change_column_null :merge_requests, :state, true
+ change_column_default :merge_requests, :state, "opened"
+
+ rebuild_indexes
+ end
+
+ def rebuild_indexes
+ add_concurrent_index(
+ :issues,
+ [:project_id, :created_at, :id, :state],
+ name: "index_issues_on_project_id_and_created_at_and_id_and_state"
+ )
+
+ add_concurrent_index(
+ :issues,
+ [:project_id, :due_date, :id, :state],
+ name: "idx_issues_on_project_id_and_due_date_and_id_and_state_partial",
+ where: "(due_date IS NOT NULL)"
+ )
+
+ add_concurrent_index(
+ :issues,
+ [:project_id, :updated_at, :id, :state],
+ name: "index_issues_on_project_id_and_updated_at_and_id_and_state"
+ )
+
+ add_concurrent_index(
+ :issues,
+ :state,
+ name: "index_issues_on_state"
+ )
+
+ add_concurrent_index(
+ :merge_requests,
+ [:id, :merge_jid],
+ where: "merge_jid IS NOT NULL and state = 'locked'",
+ name: "index_merge_requests_on_id_and_merge_jid"
+ )
+
+ add_concurrent_index(
+ :merge_requests,
+ [:source_project_id, :source_branch],
+ where: "state = 'opened'",
+ name: "index_merge_requests_on_source_project_and_branch_state_opened"
+ )
+
+ add_concurrent_index(
+ :merge_requests,
+ [:target_project_id, :iid],
+ where: "state = 'opened'",
+ name: "index_merge_requests_on_target_project_id_and_iid_opened"
+ )
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index cb503f2bb17..3be529e2955 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20190424180246) do
+ActiveRecord::Schema.define(version: 20190429112834) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -1062,7 +1062,6 @@ ActiveRecord::Schema.define(version: 20190424180246) do
t.datetime "updated_at"
t.text "description"
t.integer "milestone_id"
- t.string "state"
t.integer "iid"
t.integer "updated_by_id"
t.boolean "confidential", default: false, null: false
@@ -1086,15 +1085,11 @@ ActiveRecord::Schema.define(version: 20190424180246) do
t.index ["description"], name: "index_issues_on_description_trigram", using: :gin, opclasses: {"description"=>"gin_trgm_ops"}
t.index ["milestone_id"], name: "index_issues_on_milestone_id", using: :btree
t.index ["moved_to_id"], name: "index_issues_on_moved_to_id", where: "(moved_to_id IS NOT NULL)", using: :btree
- t.index ["project_id", "created_at", "id", "state"], name: "index_issues_on_project_id_and_created_at_and_id_and_state", using: :btree
t.index ["project_id", "created_at", "id", "state_id"], name: "idx_issues_on_project_id_created_at_id_and_state_id", using: :btree
- t.index ["project_id", "due_date", "id", "state"], name: "idx_issues_on_project_id_and_due_date_and_id_and_state_partial", where: "(due_date IS NOT NULL)", using: :btree
t.index ["project_id", "due_date", "id", "state_id"], name: "idx_issues_on_project_id_due_date_id_and_state_id", where: "(due_date IS NOT NULL)", using: :btree
t.index ["project_id", "iid"], name: "index_issues_on_project_id_and_iid", unique: true, using: :btree
- t.index ["project_id", "updated_at", "id", "state"], name: "index_issues_on_project_id_and_updated_at_and_id_and_state", using: :btree
t.index ["project_id", "updated_at", "id", "state_id"], name: "idx_issues_on_project_id_and_updated_at_and_id_and_state_id", using: :btree
t.index ["relative_position"], name: "index_issues_on_relative_position", using: :btree
- t.index ["state"], name: "index_issues_on_state", using: :btree
t.index ["state_id"], name: "idx_issues_on_state_id", using: :btree
t.index ["title"], name: "index_issues_on_title_trigram", using: :gin, opclasses: {"title"=>"gin_trgm_ops"}
t.index ["updated_at"], name: "index_issues_on_updated_at", using: :btree
@@ -1306,7 +1301,6 @@ ActiveRecord::Schema.define(version: 20190424180246) do
t.datetime "created_at"
t.datetime "updated_at"
t.integer "milestone_id"
- t.string "state", default: "opened", null: false
t.string "merge_status", default: "unchecked", null: false
t.integer "target_project_id", null: false
t.integer "iid"
@@ -1339,18 +1333,15 @@ ActiveRecord::Schema.define(version: 20190424180246) do
t.index ["description"], name: "index_merge_requests_on_description_trigram", using: :gin, opclasses: {"description"=>"gin_trgm_ops"}
t.index ["head_pipeline_id"], name: "index_merge_requests_on_head_pipeline_id", using: :btree
t.index ["id", "merge_jid"], name: "idx_merge_requests_on_id_merge_jid_state_id_locked", where: "((merge_jid IS NOT NULL) AND (state_id = 4))", using: :btree
- t.index ["id", "merge_jid"], name: "index_merge_requests_on_id_and_merge_jid", where: "((merge_jid IS NOT NULL) AND ((state)::text = 'locked'::text))", using: :btree
t.index ["latest_merge_request_diff_id"], name: "index_merge_requests_on_latest_merge_request_diff_id", using: :btree
t.index ["merge_user_id"], name: "index_merge_requests_on_merge_user_id", where: "(merge_user_id IS NOT NULL)", using: :btree
t.index ["milestone_id"], name: "index_merge_requests_on_milestone_id", using: :btree
t.index ["source_branch"], name: "index_merge_requests_on_source_branch", using: :btree
t.index ["source_project_id", "source_branch"], name: "idx_merge_requests_on_source_project_and_branch_state_id_opened", where: "(state_id = 1)", using: :btree
- t.index ["source_project_id", "source_branch"], name: "index_merge_requests_on_source_project_and_branch_state_opened", where: "((state)::text = 'opened'::text)", using: :btree
t.index ["source_project_id", "source_branch"], name: "index_merge_requests_on_source_project_id_and_source_branch", using: :btree
t.index ["target_branch"], name: "index_merge_requests_on_target_branch", using: :btree
t.index ["target_project_id", "iid"], name: "idx_merge_requests_on_target_project_id_and_iid_state_id_opened", where: "(state_id = 1)", using: :btree
t.index ["target_project_id", "iid"], name: "index_merge_requests_on_target_project_id_and_iid", unique: true, using: :btree
- t.index ["target_project_id", "iid"], name: "index_merge_requests_on_target_project_id_and_iid_opened", where: "((state)::text = 'opened'::text)", using: :btree
t.index ["target_project_id", "merge_commit_sha", "id"], name: "index_merge_requests_on_tp_id_and_merge_commit_sha_and_id", using: :btree
t.index ["title"], name: "index_merge_requests_on_title", using: :btree
t.index ["title"], name: "index_merge_requests_on_title_trigram", using: :gin, opclasses: {"title"=>"gin_trgm_ops"}
diff --git a/spec/migrations/remove_state_from_issues_and_merge_requests.rb b/spec/migrations/remove_state_from_issues_and_merge_requests.rb
new file mode 100644
index 00000000000..48dd2ff702a
--- /dev/null
+++ b/spec/migrations/remove_state_from_issues_and_merge_requests.rb
@@ -0,0 +1,47 @@
+require 'spec_helper'
+require Rails.root.join('db', 'post_migrate', '20190429112834_remove_state_from_issues_and_merge_requests.rb')
+
+describe RemoveStateFromIssuesAndMergeRequests, :migration do
+ let(:namespaces) { table(:namespaces) }
+ let(:projects) { table(:projects) }
+ let(:merge_requests) { table(:merge_requests) }
+ let(:issues) { table(:issues) }
+ let(:migration) { described_class.new }
+ let(:group) { namespaces.create!(name: 'gitlab', path: 'gitlab') }
+ let(:project) { projects.create!(namespace_id: group.id) }
+
+ describe '#down' do
+ before do
+ migration.up
+ end
+
+ context 'issues' do
+ it 'migrates state_id column to old state' do
+ #expect(migration.index_exists?(:project_features, :project_id, unique: true, name: 'index_project_features_on_project_id')).to be true
+ opened_issue = issues.create!(description: 'first', state_id: 1)
+ closed_issue = issues.create!(description: 'second', state_id: 2)
+
+ migration.down
+
+ expect(opened_issue.reload.state).to eq('opened')
+ expect(closed_issue.reload.state).to eq('closed')
+ end
+ end
+
+ context 'merge requests' do
+ it 'migrates state_id column to old state' do
+ opened_merge_request = merge_requests.create!(state_id: 1, target_project_id: project.id, target_branch: 'feature1', source_branch: 'master')
+ closed_merge_request = merge_requests.create!(state_id: 2, target_project_id: project.id, target_branch: 'feature2', source_branch: 'master')
+ merged_merge_request = merge_requests.create!(state_id: 3, target_project_id: project.id, target_branch: 'feature3', source_branch: 'master')
+ locked_merge_request = merge_requests.create!(state_id: 4, target_project_id: project.id, target_branch: 'feature4', source_branch: 'master')
+
+ migration.down
+
+ expect(opened_merge_request.reload.state).to eq('opened')
+ expect(closed_merge_request.reload.state).to eq('closed')
+ expect(merged_merge_request.reload.state).to eq('merged')
+ expect(locked_merge_request.reload.state).to eq('locked')
+ end
+ end
+ end
+end