[deploy] Add missing foreign keys to ahoy_messages and articles (#10321)

* Add FK between ahoy_messages and feedback_messages

* Add FKs between articles.second_user_id, articles.third_user_id and users

* Fix associations and specs

* Add more validations

* Remove FKs to articles.second_user_id and .third_user_id as they are about to be removed
This commit is contained in:
rhymes 2020-09-16 19:47:03 +02:00 committed by GitHub
parent e7e31f4d10
commit 5ba8c44b05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 65 additions and 12 deletions

View file

@ -19,12 +19,13 @@ class Article < ApplicationRecord
delegate :name, to: :user, prefix: true
delegate :username, to: :user, prefix: true
belongs_to :user
belongs_to :organization, optional: true
# touch: true was removed because when an article is updated, the associated collection
# is touched along with all its articles(including this one). This causes eventually a deadlock.
belongs_to :collection, optional: true
belongs_to :organization, optional: true
belongs_to :user
counter_culture :user
counter_culture :organization

View file

@ -1,4 +1,6 @@
class EmailMessage < Ahoy::Message
belongs_to :feedback_message, optional: true
# So far this is mostly used to be compatible with administrate gem,
# which doesn't seem to play nicely with namespaces. But there could be other
# reasons to define behavior here, similar to how we use the Tag model.

View file

@ -4,6 +4,8 @@ class FeedbackMessage < ApplicationRecord
belongs_to :offender, class_name: "User", optional: true, inverse_of: :offender_feedback_messages
belongs_to :reporter, class_name: "User", optional: true, inverse_of: :reporter_feedback_messages
belongs_to :affected, class_name: "User", optional: true, inverse_of: :affected_feedback_messages
has_one :email_message, dependent: :nullify
has_many :notes, as: :noteable, inverse_of: :noteable, dependent: :destroy
validates :feedback_type, :message, presence: true

View file

@ -0,0 +1,5 @@
class AddMissingForeignKeysToAhoyMessages < ActiveRecord::Migration[6.0]
def change
add_foreign_key :ahoy_messages, :feedback_messages, on_delete: :nullify, validate: false
end
end

View file

@ -0,0 +1,5 @@
class ValidateAddMissingForeignKeysToAhoyMessages < ActiveRecord::Migration[6.0]
def change
validate_foreign_key :ahoy_messages, :feedback_messages
end
end

View file

@ -0,0 +1,5 @@
class AddMissingForeignKeysToArticles < ActiveRecord::Migration[6.0]
def change
add_foreign_key :articles, :collections, on_delete: :nullify, validate: false
end
end

View file

@ -0,0 +1,5 @@
class ValidateAddMissingForeignKeysToArticles < ActiveRecord::Migration[6.0]
def change
validate_foreign_key :articles, :collections
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_09_11_140318) do
ActiveRecord::Schema.define(version: 2020_09_14_145500) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
@ -1379,9 +1379,11 @@ ActiveRecord::Schema.define(version: 2020_09_11_140318) do
add_foreign_key "ahoy_events", "ahoy_visits", column: "visit_id", on_delete: :cascade
add_foreign_key "ahoy_events", "users", on_delete: :cascade
add_foreign_key "ahoy_messages", "feedback_messages", on_delete: :nullify
add_foreign_key "ahoy_messages", "users", on_delete: :cascade
add_foreign_key "ahoy_visits", "users", on_delete: :cascade
add_foreign_key "api_secrets", "users", on_delete: :cascade
add_foreign_key "articles", "collections", on_delete: :nullify
add_foreign_key "articles", "organizations", on_delete: :nullify
add_foreign_key "articles", "users", on_delete: :cascade
add_foreign_key "audit_logs", "users"

View file

@ -0,0 +1,8 @@
FactoryBot.define do
factory :email_message do
to { Faker::Internet.email }
subject { Faker::Lorem.sentence }
content { Faker::Lorem.paragraph }
end
end

View file

@ -14,9 +14,9 @@ RSpec.describe Article, type: :model do
it_behaves_like "UserSubscriptionSourceable"
describe "validations" do
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:organization).optional }
it { is_expected.to belong_to(:collection).optional }
it { is_expected.to belong_to(:organization).optional }
it { is_expected.to belong_to(:user) }
it { is_expected.to have_many(:buffer_updates).dependent(:destroy) }
it { is_expected.to have_many(:comments).dependent(:nullify) }

View file

@ -0,0 +1,9 @@
require "rails_helper"
RSpec.describe EmailMessage, type: :model do
describe "validations" do
subject { create(:email_message) }
it { is_expected.to belong_to(:feedback_message).optional }
end
end

View file

@ -3,6 +3,8 @@ require "rails_helper"
RSpec.describe FeedbackMessage, type: :model do
subject(:feedback_message) { create(:feedback_message) }
it { is_expected.to have_one(:email_message).dependent(:nullify).optional }
it { is_expected.to validate_presence_of(:feedback_type) }
it { is_expected.to validate_presence_of(:message) }
it { is_expected.to validate_length_of(:reported_url).is_at_most(250) }

View file

@ -9,21 +9,28 @@ RSpec.describe "/admin/articles", type: :request do
context "when updating an Article via /admin/articles" do
let(:super_admin) { create(:user, :super_admin) }
let(:article) { create(:article) }
let(:second_user) { create(:user) }
let(:third_user) { create(:user) }
before { sign_in super_admin }
it "allows an Admin to add a co-author to an individual article" do
get request
expect do
article.update_columns(second_user_id: 1)
end.to change(article, :second_user_id).from(nil).to(1)
article.update_columns(second_user_id: second_user.id)
end.to change(article, :second_user_id).from(nil).to(second_user.id)
end
it "allows an Admin to add co-authora to an individual article" do
article.update_columns(second_user_id: 2, third_user_id: 3)
it "allows an Admin to add multiple co-authors to an individual article" do
article.update_columns(second_user_id: second_user.id, third_user_id: third_user.id)
get request
expect(article.second_user_id).to eq(2)
expect(article.third_user_id).to eq(3)
article.reload
expect(article.second_user_id).to eq(second_user.id)
expect(article.third_user_id).to eq(third_user.id)
end
end
end

View file

@ -163,7 +163,7 @@ RSpec.describe Users::Delete, type: :service do
described_class.call(user)
aggregate_failures "associations should not exist" do
user_associations.each do |user_association|
expect { user_association.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { user_association.reload }.to raise_error(ActiveRecord::RecordNotFound), user_association
end
end
end