업로드 정리 및 S3에서 업로드 삭제

경고 메시지와 함께 “업로드 정리(clean up uploads)” 옵션을 활성화하는 것은 무섭게 느껴질 수 있습니다. 기존 포럼을 Discourse로 전환할 경우 이 설정은 비활성화됩니다. 모든 가져오기(import) 스크립트가 게시물 내 모든 업로드를 올바르게 등록하지는 않으므로, 이 옵션을 활성화하면 많은 첨부 파일이 손실될 수 있습니다.

다음 쿼리를 사용하면 업로드가 게시물에 의해 올바르게 참조되는지 확인할 수 있습니다:

select p.post_id, u.id as upload_id
from (select id post_id, (regexp_matches(cooked, 'data-download-href=[^\s]+/default/([a-z0-9]+)', 'g'))[1] upload_sha from posts where raw like '%upload://%' order by created_at) as p 
join uploads u on u.sha1 = p.upload_sha
where not exists(select * from upload_references r where r.upload_id = u.id)

모든 것이 올바르게 설정되어 있다면 이 쿼리는 아무 행도 반환하지 않아야 합니다. 이 쿼리를 Data Explorer 플러그인에서 사용하면 참조되지 않은 첨부 파일이 있는 게시물로 깔끔하게 링크됩니다.

위 쿼리가 결과를 반환하는 경우, 다음 쿼리를 사용하여 누락된 업로드 참조를 수정할 수 있습니다:

insert into upload_references(upload_id, target_type, target_id, created_at, updated_at)
select u.id, 'Post', p.post_id, u.created_at, u.updated_at 
from (select id post_id, (regexp_matches(cooked, 'data-download-href=[^\s]+/default/([a-z0-9]+)', 'g'))[1] upload_sha from posts where raw like '%upload://%' order by created_at) as p 
join uploads u on u.sha1 = p.upload_sha
on conflict do nothing;

수정 사항을 적용하려면 직접 데이터베이스에 접근할 수 있어야 합니다.