about summary refs log tree commit diff
diff options
context:
space:
mode:
authornullkal <nullkal@nil.nu>2017-10-10 20:12:17 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-10-10 13:12:17 +0200
commit6c54d2e5837de941457371e9afffd05606d88180 (patch)
tree9f1b3d466755ef356ebf1bb1c3a943ba28fce75a
parent96c942e8abf7bf04e6c3dbcc48cbec07138c486c (diff)
foreign_key, non-nullable, dependent: destroy in account_moderation_notes (#5294)
* Add foreign key constraint to column `account` in `account_moderation_notes`

* Change account_id and target_account_id to non-nullable in account_moderation_notes

* Add dependent: :destroy to account and target_account in account_moderation_notes
-rw-r--r--app/models/account.rb4
-rw-r--r--app/models/account_moderation_note.rb5
-rw-r--r--db/migrate/20171010023049_add_foreign_key_to_account_moderation_notes.rb5
-rw-r--r--db/migrate/20171010025614_change_accounts_nonnullable_in_account_moderation_notes.rb6
-rw-r--r--db/schema.rb7
5 files changed, 19 insertions, 8 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 88f16026d..3dc2a95ab 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -91,8 +91,8 @@ class Account < ApplicationRecord
   has_many :targeted_reports, class_name: 'Report', foreign_key: :target_account_id
 
   # Moderation notes
-  has_many :account_moderation_notes
-  has_many :targeted_moderation_notes, class_name: 'AccountModerationNote', foreign_key: :target_account_id
+  has_many :account_moderation_notes, dependent: :destroy
+  has_many :targeted_moderation_notes, class_name: 'AccountModerationNote', foreign_key: :target_account_id, dependent: :destroy
 
   scope :remote, -> { where.not(domain: nil) }
   scope :local, -> { where(domain: nil) }
diff --git a/app/models/account_moderation_note.rb b/app/models/account_moderation_note.rb
index be52d10b6..3ac9b1ac1 100644
--- a/app/models/account_moderation_note.rb
+++ b/app/models/account_moderation_note.rb
@@ -1,13 +1,12 @@
 # frozen_string_literal: true
-
 # == Schema Information
 #
 # Table name: account_moderation_notes
 #
 #  id                :integer          not null, primary key
 #  content           :text             not null
-#  account_id        :integer
-#  target_account_id :integer
+#  account_id        :integer          not null
+#  target_account_id :integer          not null
 #  created_at        :datetime         not null
 #  updated_at        :datetime         not null
 #
diff --git a/db/migrate/20171010023049_add_foreign_key_to_account_moderation_notes.rb b/db/migrate/20171010023049_add_foreign_key_to_account_moderation_notes.rb
new file mode 100644
index 000000000..fc1e1ab91
--- /dev/null
+++ b/db/migrate/20171010023049_add_foreign_key_to_account_moderation_notes.rb
@@ -0,0 +1,5 @@
+class AddForeignKeyToAccountModerationNotes < ActiveRecord::Migration[5.1]
+  def change
+    add_foreign_key :account_moderation_notes, :accounts
+  end
+end
diff --git a/db/migrate/20171010025614_change_accounts_nonnullable_in_account_moderation_notes.rb b/db/migrate/20171010025614_change_accounts_nonnullable_in_account_moderation_notes.rb
new file mode 100644
index 000000000..747e5a826
--- /dev/null
+++ b/db/migrate/20171010025614_change_accounts_nonnullable_in_account_moderation_notes.rb
@@ -0,0 +1,6 @@
+class ChangeAccountsNonnullableInAccountModerationNotes < ActiveRecord::Migration[5.1]
+  def change
+    change_column_null :account_moderation_notes, :account_id, false
+    change_column_null :account_moderation_notes, :target_account_id, false
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 91f1b1acb..f9722ccda 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20171006142024) do
+ActiveRecord::Schema.define(version: 20171010025614) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -25,8 +25,8 @@ ActiveRecord::Schema.define(version: 20171006142024) do
 
   create_table "account_moderation_notes", force: :cascade do |t|
     t.text "content", null: false
-    t.bigint "account_id"
-    t.bigint "target_account_id"
+    t.bigint "account_id", null: false
+    t.bigint "target_account_id", null: false
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
     t.index ["account_id"], name: "index_account_moderation_notes_on_account_id"
@@ -459,6 +459,7 @@ ActiveRecord::Schema.define(version: 20171006142024) do
   end
 
   add_foreign_key "account_domain_blocks", "accounts", name: "fk_206c6029bd", on_delete: :cascade
+  add_foreign_key "account_moderation_notes", "accounts"
   add_foreign_key "account_moderation_notes", "accounts", column: "target_account_id"
   add_foreign_key "blocks", "accounts", column: "target_account_id", name: "fk_9571bfabc1", on_delete: :cascade
   add_foreign_key "blocks", "accounts", name: "fk_4269e03e65", on_delete: :cascade