From 100e7f8746da72119163a57a195ca56d160f3905 Mon Sep 17 00:00:00 2001
From: Ravi Kumar <rkumar@gitlab.com>
Date: Tue, 9 Apr 2024 10:13:09 +0000
Subject: [PATCH] Add a migration to backfill zoekt_settings in
 application_settings

Set the values of zoekt_settings in the application_settings from the
feature flags.

Changelog: other
MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/148014
EE: true
---
 ..._zoekt_settings_in_application_settings.rb | 30 +++++++++++++++++
 db/schema_migrations/20240327110521           |  1 +
 ...t_settings_in_application_settings_spec.rb | 33 +++++++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 db/migrate/20240327110521_migrate_zoekt_settings_in_application_settings.rb
 create mode 100644 db/schema_migrations/20240327110521
 create mode 100644 ee/spec/migrations/20240327110521_migrate_zoekt_settings_in_application_settings_spec.rb

diff --git a/db/migrate/20240327110521_migrate_zoekt_settings_in_application_settings.rb b/db/migrate/20240327110521_migrate_zoekt_settings_in_application_settings.rb
new file mode 100644
index 000000000000..b36a2e655405
--- /dev/null
+++ b/db/migrate/20240327110521_migrate_zoekt_settings_in_application_settings.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+class MigrateZoektSettingsInApplicationSettings < Gitlab::Database::Migration[2.2]
+  restrict_gitlab_migration gitlab_schema: :gitlab_main
+  milestone '16.11'
+
+  class ApplicationSetting < MigrationRecord
+    self.table_name = 'application_settings'
+  end
+
+  def up
+    return unless Gitlab.ee? # zoekt_settings available only in EE version
+
+    ApplicationSetting.reset_column_information
+
+    application_setting = ApplicationSetting.last
+    return if application_setting.nil? || application_setting.zoekt_settings.any?
+
+    zoekt_settings = {
+      zoekt_indexing_enabled: Feature.enabled?(:index_code_with_zoekt),
+      zoekt_indexing_paused: Feature.enabled?(:zoekt_pause_indexing, type: :ops),
+      zoekt_search_enabled: Feature.enabled?(:search_code_with_zoekt)
+    }
+    application_setting.update!(zoekt_settings: zoekt_settings)
+  end
+
+  def down
+    # No op
+  end
+end
diff --git a/db/schema_migrations/20240327110521 b/db/schema_migrations/20240327110521
new file mode 100644
index 000000000000..54e1c1d7ef40
--- /dev/null
+++ b/db/schema_migrations/20240327110521
@@ -0,0 +1 @@
+616c39306e8a1352f63f37d5c0953521654760c9dacf76ef56125686392c882f
\ No newline at end of file
diff --git a/ee/spec/migrations/20240327110521_migrate_zoekt_settings_in_application_settings_spec.rb b/ee/spec/migrations/20240327110521_migrate_zoekt_settings_in_application_settings_spec.rb
new file mode 100644
index 000000000000..dcc385665b31
--- /dev/null
+++ b/ee/spec/migrations/20240327110521_migrate_zoekt_settings_in_application_settings_spec.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require_migration!
+
+RSpec.describe MigrateZoektSettingsInApplicationSettings, feature_category: :global_search do
+  let!(:application_setting) { table(:application_settings).create! }
+
+  describe '#up' do
+    context 'when zoekt_settings is not already set' do
+      it 'migrates zoekt_settings from the feature flags in the application_settings successfully' do
+        expected_zoekt_settings = {
+          'zoekt_indexing_enabled' => Feature.enabled?(:index_code_with_zoekt),
+          'zoekt_indexing_paused' => Feature.enabled?(:zoekt_pause_indexing, type: :ops),
+          'zoekt_search_enabled' => Feature.enabled?(:search_code_with_zoekt)
+        }
+        expect { migrate! }.to change { application_setting.reload.zoekt_settings }.from({}).to(expected_zoekt_settings)
+      end
+    end
+
+    context 'when zoekt_settings is already set' do
+      before do
+        application_setting.update!(zoekt_settings: { zoekt_indexing_enabled: false,
+                                                      zoekt_indexing_paused: false, zoekt_search_enabled: false })
+      end
+
+      it 'does not update the zoekt_settings' do
+        expect(application_setting.zoekt_settings).not_to eq({})
+        expect { migrate! }.not_to change { application_setting.reload.zoekt_settings }
+      end
+    end
+  end
+end
-- 
GitLab