summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-15 18:08:34 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-15 18:08:34 +0000
commit571d993b49313dd806bd3f6af16d36c26d9d28ca (patch)
tree06bd12c4b56b97881aef8a00d4d46698de1eb63f /db
parent9044365a91112d426fbbfba07eca595652bbe2df (diff)
downloadgitlab-ce-571d993b49313dd806bd3f6af16d36c26d9d28ca.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'db')
-rw-r--r--db/fixtures/development/07_milestones.rb3
-rw-r--r--db/migrate/20200109085206_create_approval_project_rules_protected_branches.rb19
-rw-r--r--db/post_migrate/20200114112932_add_temporary_partial_index_on_project_id_to_services.rb22
-rw-r--r--db/post_migrate/20200114113341_patch_prometheus_services_for_shared_cluster_applications.rb88
-rw-r--r--db/schema.rb12
5 files changed, 141 insertions, 3 deletions
diff --git a/db/fixtures/development/07_milestones.rb b/db/fixtures/development/07_milestones.rb
index 8a282562335..880a1211c03 100644
--- a/db/fixtures/development/07_milestones.rb
+++ b/db/fixtures/development/07_milestones.rb
@@ -9,8 +9,7 @@ Gitlab::Seeder.quiet do
state: [:active, :closed].sample,
}
- milestone = Milestones::CreateService.new(
- project, project.team.users.sample, milestone_params).execute
+ Milestones::CreateService.new(project, project.team.users.sample, milestone_params).execute
print '.'
end
diff --git a/db/migrate/20200109085206_create_approval_project_rules_protected_branches.rb b/db/migrate/20200109085206_create_approval_project_rules_protected_branches.rb
new file mode 100644
index 00000000000..4e75f7d41fd
--- /dev/null
+++ b/db/migrate/20200109085206_create_approval_project_rules_protected_branches.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class CreateApprovalProjectRulesProtectedBranches < ActiveRecord::Migration[5.2]
+ DOWNTIME = false
+
+ def change
+ create_table :approval_project_rules_protected_branches, id: false do |t|
+ t.references :approval_project_rule,
+ null: false,
+ index: false,
+ foreign_key: { on_delete: :cascade }
+ t.references :protected_branch,
+ null: false,
+ index: { name: 'index_approval_project_rules_protected_branches_pb_id' },
+ foreign_key: { on_delete: :cascade }
+ t.index [:approval_project_rule_id, :protected_branch_id], name: 'index_approval_project_rules_protected_branches_unique', unique: true, using: :btree
+ end
+ end
+end
diff --git a/db/post_migrate/20200114112932_add_temporary_partial_index_on_project_id_to_services.rb b/db/post_migrate/20200114112932_add_temporary_partial_index_on_project_id_to_services.rb
new file mode 100644
index 00000000000..55494f1e4ac
--- /dev/null
+++ b/db/post_migrate/20200114112932_add_temporary_partial_index_on_project_id_to_services.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+# See http://doc.gitlab.com/ce/development/migration_style_guide.html
+# for more information on how to write migrations for GitLab.
+
+class AddTemporaryPartialIndexOnProjectIdToServices < ActiveRecord::Migration[5.2]
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+ INDEX_NAME = 'tmp_index_on_project_id_partial_with_prometheus_services'
+ PARTIAL_FILTER = "type = 'PrometheusService'"
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :services, :project_id, where: PARTIAL_FILTER, name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index :services, :project_id, where: PARTIAL_FILTER, name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20200114113341_patch_prometheus_services_for_shared_cluster_applications.rb b/db/post_migrate/20200114113341_patch_prometheus_services_for_shared_cluster_applications.rb
new file mode 100644
index 00000000000..68361f7b176
--- /dev/null
+++ b/db/post_migrate/20200114113341_patch_prometheus_services_for_shared_cluster_applications.rb
@@ -0,0 +1,88 @@
+# frozen_string_literal: true
+
+class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migration[5.2]
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+ MIGRATION = 'ActivatePrometheusServicesForSharedClusterApplications'.freeze
+ BATCH_SIZE = 500
+ DELAY = 2.minutes
+
+ disable_ddl_transaction!
+
+ module Migratable
+ module Applications
+ class Prometheus < ActiveRecord::Base
+ self.table_name = 'clusters_applications_prometheus'
+
+ enum status: {
+ errored: -1,
+ installed: 3,
+ updated: 5
+ }
+ end
+ end
+
+ class Project < ActiveRecord::Base
+ self.table_name = 'projects'
+ include ::EachBatch
+
+ scope :with_application_on_group_clusters, -> {
+ joins("INNER JOIN namespaces ON namespaces.id = projects.namespace_id")
+ .joins("INNER JOIN cluster_groups ON cluster_groups.group_id = namespaces.id")
+ .joins("INNER JOIN clusters ON clusters.id = cluster_groups.cluster_id AND clusters.cluster_type = #{Cluster.cluster_types['group_type']}")
+ .joins("INNER JOIN clusters_applications_prometheus ON clusters_applications_prometheus.cluster_id = clusters.id
+ AND clusters_applications_prometheus.status IN (#{Applications::Prometheus.statuses[:installed]}, #{Applications::Prometheus.statuses[:updated]})")
+ }
+
+ scope :without_active_prometheus_services, -> {
+ joins("LEFT JOIN services ON services.project_id = projects.id AND services.type = 'PrometheusService'")
+ .where("services.id IS NULL OR (services.active = FALSE AND services.properties = '{}')")
+ }
+ end
+
+ class Cluster < ActiveRecord::Base
+ self.table_name = 'clusters'
+
+ enum cluster_type: {
+ instance_type: 1,
+ group_type: 2
+ }
+
+ def self.has_prometheus_application?
+ joins("INNER JOIN clusters_applications_prometheus ON clusters_applications_prometheus.cluster_id = clusters.id
+ AND clusters_applications_prometheus.status IN (#{Applications::Prometheus.statuses[:installed]}, #{Applications::Prometheus.statuses[:updated]})").exists?
+ end
+ end
+ end
+
+ def up
+ projects_without_active_prometheus_service.group('projects.id').each_batch(of: BATCH_SIZE) do |batch, index|
+ bg_migrations_batch = batch.select('projects.id').map { |project| [MIGRATION, project.id] }
+ delay = index * DELAY
+ BackgroundMigrationWorker.bulk_perform_in(delay.seconds, bg_migrations_batch)
+ end
+ end
+
+ def down
+ # no-op
+ end
+
+ private
+
+ def projects_without_active_prometheus_service
+ scope = Migratable::Project.without_active_prometheus_services
+
+ return scope if migrate_instance_cluster?
+
+ scope.with_application_on_group_clusters
+ end
+
+ def migrate_instance_cluster?
+ if instance_variable_defined?('@migrate_instance_cluster')
+ @migrate_instance_cluster
+ else
+ @migrate_instance_cluster = Migratable::Cluster.instance_type.has_prometheus_application?
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 16b8bf9dda8..f6b815de8ba 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: 2020_01_13_133352) do
+ActiveRecord::Schema.define(version: 2020_01_14_113341) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
@@ -437,6 +437,13 @@ ActiveRecord::Schema.define(version: 2020_01_13_133352) do
t.index ["group_id"], name: "index_approval_project_rules_groups_2"
end
+ create_table "approval_project_rules_protected_branches", id: false, force: :cascade do |t|
+ t.bigint "approval_project_rule_id", null: false
+ t.bigint "protected_branch_id", null: false
+ t.index ["approval_project_rule_id", "protected_branch_id"], name: "index_approval_project_rules_protected_branches_unique", unique: true
+ t.index ["protected_branch_id"], name: "index_approval_project_rules_protected_branches_pb_id"
+ end
+
create_table "approval_project_rules_users", force: :cascade do |t|
t.bigint "approval_project_rule_id", null: false
t.integer "user_id", null: false
@@ -3769,6 +3776,7 @@ ActiveRecord::Schema.define(version: 2020_01_13_133352) do
t.string "description", limit: 500
t.boolean "comment_on_event_enabled", default: true, null: false
t.index ["project_id"], name: "index_services_on_project_id"
+ t.index ["project_id"], name: "tmp_index_on_project_id_partial_with_prometheus_services", where: "((type)::text = 'PrometheusService'::text)"
t.index ["template"], name: "index_services_on_template"
t.index ["type"], name: "index_services_on_type"
end
@@ -4448,6 +4456,8 @@ ActiveRecord::Schema.define(version: 2020_01_13_133352) do
add_foreign_key "approval_project_rules", "projects", on_delete: :cascade
add_foreign_key "approval_project_rules_groups", "approval_project_rules", on_delete: :cascade
add_foreign_key "approval_project_rules_groups", "namespaces", column: "group_id", on_delete: :cascade
+ add_foreign_key "approval_project_rules_protected_branches", "approval_project_rules", on_delete: :cascade
+ add_foreign_key "approval_project_rules_protected_branches", "protected_branches", on_delete: :cascade
add_foreign_key "approval_project_rules_users", "approval_project_rules", on_delete: :cascade
add_foreign_key "approval_project_rules_users", "users", on_delete: :cascade
add_foreign_key "approvals", "merge_requests", name: "fk_310d714958", on_delete: :cascade