FIX: Don't swallow the original error when moving posts

Dropping the temp table in an `ensure` block hides the actual exception. Creating the table with `ON COMMIT DROP` makes the temp table disappear automatically at the end of the transaction. We only need the explicit `DROP` in tests, because tests already run inside a transaction, so the temp table won't be dropped after each test which leads to spec failures.
This commit is contained in:
Gerhard Schlager 2019-10-09 15:54:54 +02:00
parent 5357ab3324
commit 10e509e47f
1 changed files with 3 additions and 7 deletions

View File

@ -77,11 +77,11 @@ class PostMover
destination_topic.reload
destination_topic
ensure
drop_temp_table
end
def create_temp_table
DB.exec("DROP TABLE IF EXISTS moved_posts") if Rails.env.test?
DB.exec <<~SQL
CREATE TEMPORARY TABLE moved_posts (
old_topic_id INTEGER,
@ -91,17 +91,13 @@ class PostMover
new_topic_title VARCHAR,
new_post_id INTEGER,
new_post_number INTEGER
);
) ON COMMIT DROP;
CREATE INDEX moved_posts_old_post_number ON moved_posts(old_post_number);
CREATE INDEX moved_posts_old_post_id ON moved_posts(old_post_id);
SQL
end
def drop_temp_table
DB.exec("DROP TABLE IF EXISTS moved_posts")
end
def move_each_post
max_post_number = destination_topic.max_post_number + 1