summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/rename_table_helpers.rb
blob: e881c0e54555609d554647e27ff14db3d74e7cb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

module Gitlab
  module Database
    module RenameTableHelpers
      def rename_table_safely(old_table_name, new_table_name)
        transaction do
          rename_table(old_table_name, new_table_name)
          execute("CREATE VIEW #{old_table_name} AS SELECT * FROM #{new_table_name}")
        end
      end

      def undo_rename_table_safely(old_table_name, new_table_name)
        transaction do
          execute("DROP VIEW IF EXISTS #{old_table_name}")
          rename_table(new_table_name, old_table_name)
        end
      end

      def finalize_table_rename(old_table_name, new_table_name)
        transaction do
          execute("DROP VIEW IF EXISTS #{old_table_name}")
        end
      end

      def undo_finalize_table_rename(old_table_name, new_table_name)
        transaction do
          execute("CREATE VIEW #{old_table_name} AS SELECT * FROM #{new_table_name}")
        end
      end
    end
  end
end