discourse/db/migrate/20200430010528_remove_avg_time_from_topics_posts.rb
David Taylor e76c583b91
DEV: Promote old post-deploy migrations to pre-deploy migrations (#13477)
Having a large number of post-deploy migrations running out-of-numerical-sequence with pre-deploy migrations can be problematic. For example, if we have the sequence

- db/migrate/2017... - add column
- db/post_migrate/2018... - drop the column
- db/migrate/2021... - add the same column again

It will work fine in numerical order. But if you run the pre-deploy migrations **followed by** the post-deploy migrations, you will not get the same result.

Our post-deploy system is designed to allow for seamless upgrades of Discourse. However, it is reasonable for us to only support this totally seamless experience for a limited period of time. This commit moves all post_deploy migrations which are more than 1 year old (i.e. more than 2 major Discourse versions ago) into the regular pre-deploy migrations directory. This limits the impact of any edge cases caused by out-of-numerical-sequence migrations.
2021-06-22 16:02:24 +01:00

42 lines
1.1 KiB
Ruby

# frozen_string_literal: true
class RemoveAvgTimeFromTopicsPosts < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def up
# this makes it re-runnable and also works if it was not created initially
execute <<~SQL
ALTER TABLE topics DROP COLUMN IF EXISTS avg_time
SQL
ActiveRecord::Base.transaction do
execute "DROP VIEW badge_posts"
execute <<~SQL
ALTER TABLE posts DROP COLUMN IF EXISTS avg_time
SQL
# we must recreate this view every time we amend posts
# p.* is auto expanded and persisted into the view definition
# at create time
execute <<~SQL
CREATE VIEW badge_posts AS
SELECT p.*
FROM posts p
JOIN topics t ON t.id = p.topic_id
JOIN categories c ON c.id = t.category_id
WHERE c.allow_badges AND
p.deleted_at IS NULL AND
t.deleted_at IS NULL AND
NOT c.read_restricted AND
t.visible AND
p.post_type IN (1,2,3)
SQL
end
end
def down
# do nothing re-runnable
end
end