Skip to content

Instantly share code, notes, and snippets.

@compwron
Created April 14, 2023 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save compwron/0ea4b6a90e2dc4f8d35896a9aca26fae to your computer and use it in GitHub Desktop.
Save compwron/0ea4b6a90e2dc4f8d35896a9aca26fae to your computer and use it in GitHub Desktop.
namespace :acts_as_paranoid do
desc "Verify that all models using acts_as_paranoid have scopes on their unique indexes and validations"
task lint: :environment do
if Rails.env.test?
currently_probably_buggy_classes_ignored = %w[
# too many things, redacted
]
Zeitwerk::Loader.eager_load_all
errors = []
allows_multiple_deleted = "(deleted_at IS NULL)"
ApplicationRecord.descendants.each do |clazz|
next unless clazz.paranoid?
unique_indexes = ActiveRecord::Base.connection.indexes(clazz.table_name).select { |x| x.unique }
next unless unique_indexes.any?
unique_indexes.each do |idx|
if idx in currently_probably_buggy_classes_ignored
puts "#{idx.name} does not pass rake acts_as_paranoid:lint but we are not worrying about that right now"
next
end
unless idx.where&.include?(allows_multiple_deleted)
errors << "#{clazz} uses acts_as_paranoid but has a unique index #{idx.name} without #{allows_multiple_deleted} but it does have: #{idx.where}"
end
end
end
raise errors.join("\n") unless errors.empty?
else
system("bundle exec rake acts_as_paranoid:lint RAILS_ENV='test'")
raise if $?.exitstatus.nonzero?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment