Class: RuboCop::Cop::Neeto::UnsafeTableDeletion
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Neeto::UnsafeTableDeletion
- Defined in:
- lib/rubocop/cop/neeto/unsafe_table_deletion.rb
Overview
‘drop_table` is considered as a dangerous operation. If not used correctly, it could cause irreversible data loss. It is advised to rename the table to `table_name_deprecated_on_yyyy_mm_dd` instead. The renamed table can be safely dropped in a later migration. The cop permits dropping tables that are named in the above format.
Constant Summary collapse
- MAX_TABLE_NAME_LENGTH =
Length of “_deprecated_on_xxxx_xx_xx” = 25, Max permitted length of table: 63
38
- SAFE_TABLE_NAME_REGEX =
/\w+_deprecated_on_\d{4}_\d{2}_\d{2}/
- CURRENT_DATE =
DateTime.now.strftime("%Y_%m_%d")
- MSG =
"'drop_table' is a dangerous operation. If not used correctly, " \ "it could cause irreversible data loss. You must perform " \ "'rename_table :%{table_name}, :%{truncated_table_name}_deprecated_on_#{CURRENT_DATE}' " \ "instead. The renamed table can be safely dropped in a future migration."
- RESTRICT_ON_SEND =
%i[drop_table].freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rubocop/cop/neeto/unsafe_table_deletion.rb', line 54 def on_send(node) return unless unsafe_drop_table?(node) node.each_ancestor do |parent| return if down_method?(parent) end unsafe_drop_table?(node) do |table_name| truncated_table_name = table_name.slice(0, MAX_TABLE_NAME_LENGTH) = format(MSG, table_name:, truncated_table_name:) add_offense(node, message:) unless SAFE_TABLE_NAME_REGEX.match?(table_name) end end |