2020-06-23 00:09:42 +05:30
|
|
|
# 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::Cop
|
|
|
|
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)
|
2020-07-28 23:09:34 +05:30
|
|
|
return if down_method?(node)
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
node.each_descendant(:send) do |send_node|
|
|
|
|
next unless offensible?(send_node)
|
|
|
|
|
|
|
|
add_offense(send_node, location: :selector)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def down_method?(node)
|
|
|
|
node.method?(:down)
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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
|