From db2aab7a110da617b11e69ba86562c6007da8574 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Francisco=20Javier=20L=C3=B3pez?= <fjlopez@gitlab.com>
Date: Tue, 22 Mar 2022 11:12:08 +0100
Subject: [PATCH] Remove obsolete wiki notes

It seems there are really old wiki notes in the production database.
There is no way to have wiki notes right now with the current
codebase. Besides, it's impossible to create them or interact
with them.

Changelog: other
---
 .../20220322094410_remove_wiki_notes.rb       | 20 +++++++++++
 db/schema_migrations/20220322094410           |  1 +
 spec/migrations/remove_wiki_notes_spec.rb     | 33 +++++++++++++++++++
 3 files changed, 54 insertions(+)
 create mode 100644 db/post_migrate/20220322094410_remove_wiki_notes.rb
 create mode 100644 db/schema_migrations/20220322094410
 create mode 100644 spec/migrations/remove_wiki_notes_spec.rb

diff --git a/db/post_migrate/20220322094410_remove_wiki_notes.rb b/db/post_migrate/20220322094410_remove_wiki_notes.rb
new file mode 100644
index 000000000000..c3705e1e20fe
--- /dev/null
+++ b/db/post_migrate/20220322094410_remove_wiki_notes.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class RemoveWikiNotes < Gitlab::Database::Migration[1.0]
+  disable_ddl_transaction!
+
+  class Note < ApplicationRecord
+    self.table_name = 'notes'
+    self.inheritance_column = :_type_disabled
+  end
+
+  def up
+    return unless Gitlab.dev_or_test_env? || Gitlab.staging? || Gitlab.com?
+
+    Note.where(noteable_type: 'Wiki', id: [97, 98, 110, 242, 272]).delete_all
+  end
+
+  def down
+    # NO-OP
+  end
+end
diff --git a/db/schema_migrations/20220322094410 b/db/schema_migrations/20220322094410
new file mode 100644
index 000000000000..1ad1682e22ce
--- /dev/null
+++ b/db/schema_migrations/20220322094410
@@ -0,0 +1 @@
+09722b398f82651c433f6b05962827351e6e7c0841f2a6414feb206bb831e523
\ No newline at end of file
diff --git a/spec/migrations/remove_wiki_notes_spec.rb b/spec/migrations/remove_wiki_notes_spec.rb
new file mode 100644
index 000000000000..2ffebdee1063
--- /dev/null
+++ b/spec/migrations/remove_wiki_notes_spec.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require_migration!
+
+RSpec.describe RemoveWikiNotes, :migration do
+  let(:notes) { table(:notes) }
+
+  it 'removes all wiki notes' do
+    notes.create!(id: 97, note: 'Wiki note', noteable_type: 'Wiki')
+    notes.create!(id: 98, note: 'Commit note', noteable_type: 'Commit')
+    notes.create!(id: 110, note: 'Issue note', noteable_type: 'Issue')
+    notes.create!(id: 242, note: 'MergeRequest note', noteable_type: 'MergeRequest')
+
+    expect(notes.where(noteable_type: 'Wiki').size).to eq(1)
+
+    expect { migrate! }.to change { notes.count }.by(-1)
+
+    expect(notes.where(noteable_type: 'Wiki').size).to eq(0)
+  end
+
+  context 'when not staging nor com' do
+    it 'does not remove notes' do
+      allow(::Gitlab).to receive(:com?).and_return(false)
+      allow(::Gitlab).to receive(:dev_or_test_env?).and_return(false)
+      allow(::Gitlab).to receive(:staging?).and_return(false)
+
+      notes.create!(id: 97, note: 'Wiki note', noteable_type: 'Wiki')
+
+      expect { migrate! }.not_to change { notes.count }
+    end
+  end
+end
-- 
GitLab