Skip to content

Commit 9a3e5c0

Browse files
committed
Include data migration and column removal in migration
1 parent 8882785 commit 9a3e5c0

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

db/migrate/20251203164131_create_new_thread_followers.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
class CreateNewThreadFollowers < ActiveRecord::Migration[7.2]
22
def change
3+
create_table_new_thread_followers
4+
move_rows_with_non_nil_post_id
5+
remove_post_id_column_from_thread_followers
6+
end
7+
8+
def create_table_new_thread_followers
39
create_table :new_thread_followers do |t|
410
t.bigint :user_id
511
t.bigint :post_id
@@ -9,4 +15,16 @@ def change
915
add_index :new_thread_followers, :user_id
1016
add_index :new_thread_followers, :post_id
1117
end
18+
19+
def move_rows_with_non_nil_post_id
20+
NewThreadFollower.insert_all(
21+
ThreadFollower.select(:user_id, :post_id, :created_at, :updated_at).where.not(post_id:nil)
22+
)
23+
end
24+
25+
def remove_post_id_column_from_thread_followers
26+
change_table :thread_followers do |t|
27+
t.remove :post_id
28+
end
29+
end
1230
end

0 commit comments

Comments
 (0)