diff options
author | Stan Hu <stanhu@gmail.com> | 2017-09-19 17:11:59 +0000 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2017-09-19 14:31:20 -0400 |
commit | abebf1f536a15ff225811ae2acbb9221cf672555 (patch) | |
tree | 2ce529c8a9818e1aee5b1e6f10fbf6b24d4e551b | |
parent | e1e486ba7c2da606807df11ceb8a03095edd65bf (diff) | |
download | gitlab-ce-abebf1f536a15ff225811ae2acbb9221cf672555.tar.gz |
Merge branch 'reoganize-deployment-indexes' into 'master'
Reorganize indexes for the "deployments" table
Closes #36877
See merge request gitlab-org/gitlab-ce!14348
-rw-r--r-- | app/models/environment.rb | 4 | ||||
-rw-r--r-- | changelogs/unreleased/reoganize-deployment-indexes.yml | 5 | ||||
-rw-r--r-- | db/migrate/20170918222253_reorganize_deployments_indexes.rb | 28 | ||||
-rw-r--r-- | db/migrate/20170918223303_add_deployments_index_for_last_deployment.rb | 21 | ||||
-rw-r--r-- | db/schema.rb | 5 |
5 files changed, 58 insertions, 5 deletions
diff --git a/app/models/environment.rb b/app/models/environment.rb index 44e39e21442..b6868ccbe8f 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -6,9 +6,7 @@ class Environment < ActiveRecord::Base belongs_to :project, required: true, validate: true - has_many :deployments, - -> (env) { where(project_id: env.project_id) }, - dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent + has_many :deployments, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_one :last_deployment, -> { order('deployments.id DESC') }, class_name: 'Deployment' diff --git a/changelogs/unreleased/reoganize-deployment-indexes.yml b/changelogs/unreleased/reoganize-deployment-indexes.yml new file mode 100644 index 00000000000..87734b4fe4b --- /dev/null +++ b/changelogs/unreleased/reoganize-deployment-indexes.yml @@ -0,0 +1,5 @@ +--- +title: Reorganize indexes for the "deployments" table +merge_request: +author: +type: other diff --git a/db/migrate/20170918222253_reorganize_deployments_indexes.rb b/db/migrate/20170918222253_reorganize_deployments_indexes.rb new file mode 100644 index 00000000000..139427ed2b9 --- /dev/null +++ b/db/migrate/20170918222253_reorganize_deployments_indexes.rb @@ -0,0 +1,28 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class ReorganizeDeploymentsIndexes < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + add_index_if_not_exists :deployments, [:environment_id, :iid, :project_id] + remove_index_if_exists :deployments, [:project_id, :environment_id, :iid] + end + + def down + add_index_if_not_exists :deployments, [:project_id, :environment_id, :iid] + remove_index_if_exists :deployments, [:environment_id, :iid, :project_id] + end + + def add_index_if_not_exists(table, columns) + add_concurrent_index(table, columns) unless index_exists?(table, columns) + end + + def remove_index_if_exists(table, columns) + remove_concurrent_index(table, columns) if index_exists?(table, columns) + end +end diff --git a/db/migrate/20170918223303_add_deployments_index_for_last_deployment.rb b/db/migrate/20170918223303_add_deployments_index_for_last_deployment.rb new file mode 100644 index 00000000000..b91efb86d98 --- /dev/null +++ b/db/migrate/20170918223303_add_deployments_index_for_last_deployment.rb @@ -0,0 +1,21 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class AddDeploymentsIndexForLastDeployment < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + # Set this constant to true if this migration requires downtime. + DOWNTIME = false + + disable_ddl_transaction! + + TO_INDEX = [:deployments, %i[environment_id id]].freeze + + def up + add_concurrent_index(*TO_INDEX) + end + + def down + remove_concurrent_index(*TO_INDEX) + end +end diff --git a/db/schema.rb b/db/schema.rb index 1db50a87cfd..3ec430c0078 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170914135630) do +ActiveRecord::Schema.define(version: 20170918223303) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -506,7 +506,8 @@ ActiveRecord::Schema.define(version: 20170914135630) do end add_index "deployments", ["created_at"], name: "index_deployments_on_created_at", using: :btree - add_index "deployments", ["project_id", "environment_id", "iid"], name: "index_deployments_on_project_id_and_environment_id_and_iid", using: :btree + add_index "deployments", ["environment_id", "id"], name: "index_deployments_on_environment_id_and_id", using: :btree + add_index "deployments", ["environment_id", "iid", "project_id"], name: "index_deployments_on_environment_id_and_iid_and_project_id", using: :btree add_index "deployments", ["project_id", "iid"], name: "index_deployments_on_project_id_and_iid", unique: true, using: :btree create_table "emails", force: :cascade do |t| |