Skip to content

Instantly share code, notes, and snippets.

@compwron
Created April 14, 2023 22:49
Show Gist options
  • Select an option

  • Save compwron/0ea4b6a90e2dc4f8d35896a9aca26fae to your computer and use it in GitHub Desktop.

Select an option

Save compwron/0ea4b6a90e2dc4f8d35896a9aca26fae to your computer and use it in GitHub Desktop.

Revisions

  1. compwron created this gist Apr 14, 2023.
    31 changes: 31 additions & 0 deletions paranoid_lint.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    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