Skip to content

Commit 5da697b

Browse files
committed
Fix migration to remove moved rows and post reference
1 parent 97c2649 commit 5da697b

3 files changed

Lines changed: 5 additions & 22 deletions

File tree

app/models/thread_follower.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
class ThreadFollower < ApplicationRecord
2-
belongs_to :comment_thread, optional: true
3-
belongs_to :post, optional: true
2+
belongs_to :comment_thread
43
belongs_to :user
5-
6-
validate :thread_or_post
7-
8-
private
9-
10-
def thread_or_post
11-
if comment_thread.nil? && post.nil?
12-
errors.add(:base, 'Must refer to either a comment thread or a post.')
13-
end
14-
end
154
end

db/migrate/20251203164131_create_new_thread_followers.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ def create_table_new_thread_followers
1212

1313
t.timestamps
1414
end
15-
add_index :new_thread_followers, :user_id
16-
add_index :new_thread_followers, :post_id
15+
add_index :new_thread_followers, [:user_id, :post_id]
1716
end
1817

1918
def move_rows_with_non_nil_post_id
2019
NewThreadFollower.insert_all(
2120
ThreadFollower.select(:user_id, :post_id, :created_at, :updated_at).where.not(post_id:nil)
2221
)
22+
ThreadFollower.where.not(post_id:nil).delete_all
2323
end
2424

2525
def remove_post_id_column_from_thread_followers
26-
change_table :thread_followers do |t|
27-
t.remove :post_id
28-
end
26+
remove_reference :thread_followers, :post, index: true, foreign_key: true
2927
end
3028
end

db/schema.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,7 @@
406406
t.bigint "post_id"
407407
t.datetime "created_at", null: false
408408
t.datetime "updated_at", null: false
409-
t.index ["post_id"], name: "index_new_thread_followers_on_post_id"
410-
t.index ["user_id"], name: "index_new_thread_followers_on_user_id"
409+
t.index ["user_id", "post_id"], name: "index_new_thread_followers_on_user_id_and_post_id"
411410
end
412411

413412
create_table "notifications", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
@@ -732,9 +731,7 @@
732731
t.bigint "user_id"
733732
t.datetime "created_at", precision: nil, null: false
734733
t.datetime "updated_at", precision: nil, null: false
735-
t.bigint "post_id"
736734
t.index ["comment_thread_id"], name: "index_thread_followers_on_comment_thread_id"
737-
t.index ["post_id"], name: "index_thread_followers_on_post_id"
738735
t.index ["user_id"], name: "index_thread_followers_on_user_id"
739736
end
740737

@@ -902,7 +899,6 @@
902899
add_foreign_key "tag_synonyms", "tags"
903900
add_foreign_key "tags", "communities"
904901
add_foreign_key "tags", "tags", column: "parent_id"
905-
add_foreign_key "thread_followers", "posts"
906902
add_foreign_key "user_abilities", "abilities"
907903
add_foreign_key "user_abilities", "community_users"
908904
add_foreign_key "user_websites", "users"

0 commit comments

Comments
 (0)