From 9ee8a01c3aa963e8d4fb27f71854fc952cf5d32f Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Thu, 21 Jan 2021 12:59:50 +1000 Subject: [PATCH] FIX: Change default for IncomingEmail#created_via to 0 (unknown) and make NOT NULL (#11782) Follow up to https://review.discourse.org/t/dev-add-created-via-column-to-incomingemail-pr-11751/18366/6 --- app/models/incoming_email.rb | 5 ++++- ...001720_change_incoming_email_created_at_null.rb | 14 ++++++++++++++ lib/email/receiver.rb | 2 +- spec/fabricators/incoming_email_fabricator.rb | 1 + 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20210121001720_change_incoming_email_created_at_null.rb diff --git a/app/models/incoming_email.rb b/app/models/incoming_email.rb index 66d7053fddb..0eec96f162f 100644 --- a/app/models/incoming_email.rb +++ b/app/models/incoming_email.rb @@ -6,6 +6,8 @@ class IncomingEmail < ActiveRecord::Base belongs_to :post belongs_to :group, foreign_key: :imap_group_id, class_name: 'Group' + validates :created_via, presence: true + scope :errored, -> { where("NOT is_bounce AND error IS NOT NULL") } scope :addressed_to, -> (email) do @@ -33,6 +35,7 @@ class IncomingEmail < ActiveRecord::Base def self.created_via_types @types ||= Enum.new( + unknown: 0, handle_mail: 1, pop3_poll: 2, imap: 3, @@ -106,7 +109,7 @@ end # imap_uid :integer # imap_sync :boolean # imap_group_id :bigint -# created_via :integer +# created_via :integer default(0), not null # # Indexes # diff --git a/db/migrate/20210121001720_change_incoming_email_created_at_null.rb b/db/migrate/20210121001720_change_incoming_email_created_at_null.rb new file mode 100644 index 00000000000..b751a927769 --- /dev/null +++ b/db/migrate/20210121001720_change_incoming_email_created_at_null.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class ChangeIncomingEmailCreatedAtNull < ActiveRecord::Migration[6.0] + def up + # 0 signifies unknown + DB.exec("UPDATE incoming_emails SET created_via = 0 WHERE created_via IS NULL") + change_column_default :incoming_emails, :created_via, 0 + change_column_null :incoming_emails, :created_via, false + end + + def down + change_column_null :incoming_emails, :created_via, true + end +end diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 2b9ecfe5140..ac0dc509db5 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -153,7 +153,7 @@ module Email imap_uid: @opts[:imap_uid], imap_group_id: @opts[:imap_group_id], imap_sync: false, - created_via: @opts[:source].present? ? IncomingEmail.created_via_types[@opts[:source]] : nil + created_via: IncomingEmail.created_via_types[@opts[:source] || :unknown] ) end diff --git a/spec/fabricators/incoming_email_fabricator.rb b/spec/fabricators/incoming_email_fabricator.rb index 06a08b129de..de76343f815 100644 --- a/spec/fabricators/incoming_email_fabricator.rb +++ b/spec/fabricators/incoming_email_fabricator.rb @@ -6,6 +6,7 @@ Fabricator(:incoming_email) do from_address "foo@example.com" to_addresses "someone@else.com" imap_sync false + created_via 0 raw <<~RAW Return-Path: