diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-10 21:07:43 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-10 21:07:43 +0300 |
commit | 6f0f893bd87535b61e0ecb1ce069eaa7fcb9e5be (patch) | |
tree | 8af92b29c838e9af2fd70f9a4a2314a08f4af922 /db | |
parent | 8b1228b0d409d7751f01d9fb72ebfbbf62399486 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'db')
4 files changed, 123 insertions, 2 deletions
diff --git a/db/migrate/20200107172020_add_timestamp_softwarelicensespolicy.rb b/db/migrate/20200107172020_add_timestamp_softwarelicensespolicy.rb new file mode 100644 index 00000000000..0d62f3051ce --- /dev/null +++ b/db/migrate/20200107172020_add_timestamp_softwarelicensespolicy.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class AddTimestampSoftwarelicensespolicy < ActiveRecord::Migration[5.2] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + def up + add_timestamps_with_timezone(:software_license_policies, null: true) + end + + def down + remove_timestamps(:software_license_policies) + end +end diff --git a/db/migrate/20200108233040_remove_index_project_mirror_data_on_jid.rb b/db/migrate/20200108233040_remove_index_project_mirror_data_on_jid.rb new file mode 100644 index 00000000000..86a69275cb7 --- /dev/null +++ b/db/migrate/20200108233040_remove_index_project_mirror_data_on_jid.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveIndexProjectMirrorDataOnJid < ActiveRecord::Migration[5.2] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + remove_concurrent_index :project_mirror_data, :jid + end + + def down + add_concurrent_index :project_mirror_data, :jid + end +end diff --git a/db/post_migrate/20191220102807_patch_prometheus_services_for_shared_cluster_applications.rb b/db/post_migrate/20191220102807_patch_prometheus_services_for_shared_cluster_applications.rb new file mode 100644 index 00000000000..68361f7b176 --- /dev/null +++ b/db/post_migrate/20191220102807_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 89bace22df6..97b2ffb2e76 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_08_155731) do +ActiveRecord::Schema.define(version: 2020_01_08_233040) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" @@ -3199,7 +3199,6 @@ ActiveRecord::Schema.define(version: 2020_01_08_155731) do t.text "last_error" t.datetime_with_timezone "last_update_at" t.datetime_with_timezone "last_successful_update_at" - t.index ["jid"], name: "index_project_mirror_data_on_jid" t.index ["last_successful_update_at"], name: "index_project_mirror_data_on_last_successful_update_at" t.index ["last_update_at", "retry_count"], name: "index_project_mirror_data_on_last_update_at_and_retry_count" t.index ["next_execution_timestamp", "retry_count"], name: "index_mirror_data_on_next_execution_and_retry_count" @@ -3824,6 +3823,8 @@ ActiveRecord::Schema.define(version: 2020_01_08_155731) do t.integer "project_id", null: false t.integer "software_license_id", null: false t.integer "classification", default: 0, null: false + t.datetime_with_timezone "created_at" + t.datetime_with_timezone "updated_at" t.index ["project_id", "software_license_id"], name: "index_software_license_policies_unique_per_project", unique: true t.index ["software_license_id"], name: "index_software_license_policies_on_software_license_id" end |