diff --git a/.gitignore b/.gitignore index d5615f7..9ad01f2 100644 --- a/.gitignore +++ b/.gitignore @@ -28,8 +28,6 @@ app/assets config/locales/static_pages/*.yml -# Ignore gemfile.lock -#Gemfile.lock # Ignore db schema.rb # db/schema.rb @@ -41,6 +39,12 @@ db/test.sqlite3 db/test.sqlite3-journal +# Ingore DB dump files +*.sql +*.psql +*.sql.gz +*.psql.gz + # Ignore the SimpleCov output coverage diff --git a/lib/data_cleanup/rules/plan/fix_blank_title.rb b/lib/data_cleanup/rules/plan/fix_blank_title.rb index 0c1f594..348da76 100644 --- a/lib/data_cleanup/rules/plan/fix_blank_title.rb +++ b/lib/data_cleanup/rules/plan/fix_blank_title.rb @@ -8,10 +8,9 @@ end def call - ids = ::Plan.where(title: [nil, '']).ids - ::Plan.find(ids).each do |plan| - log("Adding default title to Plan##{plan.id}") - plan.update(title: "My plan (#{plan.template.title})") + Plan.where(title: [nil, '']).each do |plan| + log("Updating title on Plan#{plan.id}") + plan.update!(title: "My plan (#{plan.template.title})") end end end diff --git a/lib/tasks/data_cleanup.rake b/lib/tasks/data_cleanup.rake index f84dfc0..ecab52e 100644 --- a/lib/tasks/data_cleanup.rake +++ b/lib/tasks/data_cleanup.rake @@ -36,8 +36,124 @@ end end + desc "Check that each of the known type of invalidation is fixed." + task :find_known_invalidations => :environment do + ## Annotation + + # Find with blank text + results = Annotation.where(text: [nil, ""]) + report_known_invalidations(results, "Annotation", "text is blank") + + # Find with duplicate type + results = Annotation.group(:question_id, :type, :org_id) + .count + .select { |k,v| v > 1 } + report_known_invalidations(results, "Annotation", "type is a duplicate") + + ## Answer + + # Fix blank user + results = Answer.joins("LEFT OUTER JOIN users ON users.id = answers.user_id") + .where(users: { id: nil }) + .includes(plan: { roles: :user }) + report_known_invalidations(results, "Answer", "user is blank") + + # Fix duplicate question + results = Answer.group(:question_id, :plan_id).count.select { |k,v| v > 1 } + report_known_invalidations(results, "Answer", "question is a duplicate") + + ## ExportedPlan + + # Fix blank plan + results = ExportedPlan + .joins("LEFT OUTER JOIN plans on plans.id = exported_plans.plan_id") + .where(plans: { id: nil }) + report_known_invalidations(results, "ExportedPlan", "plan is blank") + + ## Org + + # Fix blank abbreviation + results = Org.where(abbreviation: [nil, ""]) + report_known_invalidations(results, "Org", "abbreviation is blank") + + # Fix blank feedback_email_msg + results = Org.where(feedback_enabled: true, feedback_email_msg: [nil, ""]) + report_known_invalidations(results, "Org", "feedback_email_msg is blank") + + results = Org.where(feedback_enabled: true, feedback_email_subject: [nil, ""]) + report_known_invalidations(results, "Org", "feedback_email_subject is blank") + + results = Org.where(language: [nil, ""]) + report_known_invalidations(results, "Org", "language is blank") + + results = Org.where.not(contact_email: [nil, ""]) + .select { |o| o.contact_email !~ /[\w\d\.\-]+@[\w\d\.\-]/ } + report_known_invalidations(results, "Org", "contact_email is invalid") + + ## Phase + + # Fix duplicate number + results = Phase.group(:number, :template_id).count.select { |k,v| v > 1 } + report_known_invalidations(results, "Phase", "duplicate_number is invalid") + + ## Plan + + # Fix blank title + results = Plan.where(title: [nil, '']) + report_known_invalidations(results, "Plan", "title is blank") + + ## Question + + # Fix duplicate number + results = Question.group(:number, :section_id).count.select { |k,v| v > 1 } + report_known_invalidations(results, "Question", "number is duplicate") + + ## QuestionFormat + + # Fix blank description + results = QuestionFormat.where(description: ["", nil]) + report_known_invalidations(results, "QuestionFormat", "description is blank") + + ## Region + + # Fix blank description + results = Region.where(description: ["", nil]) + report_known_invalidations(results, "Region", "description is blank") + + ## Role + + # Fix blank plan + results = Role.joins("LEFT OUTER JOIN plans ON plans.id = roles.plan_id") + .where(plans: { id: nil }) + report_known_invalidations(results, "Role", "plan is blank") + + ## Section + + # Fix duplicate number + results = Section.group(:number, :phase_id).count.select { |k,v| v > 1 } + report_known_invalidations(results, "Section", "number is duplicate") + + ## Template + + # Fix blank locale + results = Template.where(locale: [nil, ""]) + report_known_invalidations(results, "Template", "locale is blank") + + ## UserIdentifier + + # Fix blank user + results = UserIdentifier + .joins("LEFT OUTER JOIN users ON users.id = user_identifiers.user_id") + .where(users: { id: nil }) + report_known_invalidations(results, "UserIdentifier", "user is blank") + end + private + def report_known_invalidations(results, model_name, validation_error) + DataCleanup.display "#{results.count} #{model_name.pluralize} with #{validation_error}", color: results.any? ? :red : :green + end + def rule_paths @rule_paths ||= Rails.root.join("lib", "data_cleanup", "rules", "*", "*.rb") end