File: drop_table.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (57 lines) | stat: -rw-r--r-- 1,494 bytes parent folder | download
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

require_relative '../../migration_helpers'

module RuboCop
  module Cop
    module Migration
      # Cop that checks if `drop_table` is called in deployment migrations.
      # Calling it in deployment migrations can cause downtimes as there still may be code using the target tables.
      class DropTable < RuboCop::Cop::Base
        include MigrationHelpers

        MSG = <<-MESSAGE.delete("\n").squeeze
          `drop_table` in deployment migrations requires downtime.
          Drop tables in post-deployment migrations instead.
        MESSAGE

        def on_def(node)
          return unless in_deployment_migration?(node)
          return if down_method?(node)

          node.each_descendant(:send) do |send_node|
            next unless offensible?(send_node)

            add_offense(send_node.loc.selector)
          end
        end

        private

        def down_method?(node)
          node.method?(:down)
        end

        def offensible?(node)
          drop_table?(node) || drop_table_in_execute?(node)
        end

        def drop_table?(node)
          node.children[1] == :drop_table
        end

        def drop_table_in_execute?(node)
          execute?(node) && drop_table_in_execute_sql?(node)
        end

        def execute?(node)
          node.children[1] == :execute
        end

        def drop_table_in_execute_sql?(node)
          node.children[2].to_s.match?(/drop\s+table/i)
        end
      end
    end
  end
end