From 8130d7026077a7a347e2d97d3ddacc436f60190c Mon Sep 17 00:00:00 2001
From: Hordur Freyr Yngvason <hfyngvason@gitlab.com>
Date: Tue, 18 May 2021 01:15:02 +0000
Subject: [PATCH] Backfill clusters_integration_elastic_stack.enabled

---
 ...sters-integration-elatic-stack-enabled.yml |  5 +++
 ...sters_integration_elastic_stack_enabled.rb | 34 +++++++++++++++++++
 db/schema_migrations/20210518001450           |  1 +
 3 files changed, 40 insertions(+)
 create mode 100644 changelogs/unreleased/backfill-clusters-integration-elatic-stack-enabled.yml
 create mode 100644 db/post_migrate/20210518001450_backfill_clusters_integration_elastic_stack_enabled.rb
 create mode 100644 db/schema_migrations/20210518001450

diff --git a/changelogs/unreleased/backfill-clusters-integration-elatic-stack-enabled.yml b/changelogs/unreleased/backfill-clusters-integration-elatic-stack-enabled.yml
new file mode 100644
index 0000000000000..a37a470cb67ab
--- /dev/null
+++ b/changelogs/unreleased/backfill-clusters-integration-elatic-stack-enabled.yml
@@ -0,0 +1,5 @@
+---
+title: Backfill clusters_integration_elastic_stack.enabled
+merge_request: 61521
+author:
+type: changed
diff --git a/db/post_migrate/20210518001450_backfill_clusters_integration_elastic_stack_enabled.rb b/db/post_migrate/20210518001450_backfill_clusters_integration_elastic_stack_enabled.rb
new file mode 100644
index 0000000000000..6d21945af0003
--- /dev/null
+++ b/db/post_migrate/20210518001450_backfill_clusters_integration_elastic_stack_enabled.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+class BackfillClustersIntegrationElasticStackEnabled < ActiveRecord::Migration[6.0]
+  include Gitlab::Database::MigrationHelpers
+
+  disable_ddl_transaction!
+
+  def up
+    ApplicationRecord.connection.execute(<<~SQL.squish)
+      INSERT INTO clusters_integration_elasticstack(
+        cluster_id,
+        enabled,
+        chart_version,
+        created_at,
+        updated_at
+      )
+        SELECT
+          cluster_id,
+          true,
+          version,
+          TIMEZONE('UTC', NOW()),
+          TIMEZONE('UTC', NOW())
+        FROM clusters_applications_elastic_stacks
+        WHERE status IN (3, 11)
+      ON CONFLICT(cluster_id) DO UPDATE SET
+        enabled = true,
+        updated_at = TIMEZONE('UTC', NOW())
+    SQL
+  end
+
+  def down
+    # Irreversible
+  end
+end
diff --git a/db/schema_migrations/20210518001450 b/db/schema_migrations/20210518001450
new file mode 100644
index 0000000000000..5f3694d8b9351
--- /dev/null
+++ b/db/schema_migrations/20210518001450
@@ -0,0 +1 @@
+212a2db8b1183eb4ae313eacae7d54f2bec26c5ea3f961981d34e57ca1420ad9
\ No newline at end of file
-- 
GitLab