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
This commit is contained in:
Martin Brennan 2021-01-21 12:59:50 +10:00 committed by GitHub
parent 9d287f5ff9
commit 9ee8a01c3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 2 deletions

View File

@ -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
#

View File

@ -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

View File

@ -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

View File

@ -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: <foo@example.com>