# Re-adding missing uploads to the database

**URL:** <https://meta.discourse.org/t/re-adding-missing-uploads-to-the-database/152978>\
**Category:** Support\
**Created:** [May 27, 2020, 3:42pm UTC](https://meta.discourse.org/t/re-adding-missing-uploads-to-the-database/152978 "2020-05-27T15:42:38Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [May 27, 2020, 3:42pm UTC](https://meta.discourse.org/t/re-adding-missing-uploads-to-the-database/152978/1 "2020-05-27T15:42:38Z")

</div>

I’ve got a site where it seems that a bunch of uploads have been removed from the uploads table in the database, but still exist in the filesystem. This leaves links to these broken. I’ve fixed a few by re-uploading files into a new topic to create the record in the DB (at the time I thought that the files were missing too, but @angus graciously pointed out why I wasn’t finding them–One day I’ll ask: why do we have both sha1 **and** base62 names for all of these assets?) and then re-baking the posts that include those uploads, like this:

```ruby
def rebake_posts_with_uploads(topic_id, post_number)
  p = Post.find_by(topic_id: topic_id, post_number: post_number)
  exit unless p
  re = /upload:\/\/(.+?)\)/
  shas= p.raw.scan(re)

  shas.each do |sha|
    posts = Post.where("raw like '%#{sha[0]}%'")
    next unless posts
    puts "Found #{posts.count - 1} #{sha[0]}"
    posts.each do |post|
      next if post.id == p.id
      puts "rebake #{post.id}--#{BASE_URL}/t/-/#{post.topic_id}/#{post.post_number}"
      post.rebake!
    end
  end
end

```

My new plan, since it seems that the files exist in the file system but not in the database, is to do something like this:

```plaintext
for (all files in /shared/uploads/default/original/1x) do |file|
  unless file is in uploads table
     create upload record 
     for each post that includes that upload record
       rebake

```

Does that seem right? I’m looking at [`uploads.rake`](https://github.com/discourse/discourse/blob/master/lib/tasks/uploads.rake) and don’t see anything that seems to do this already. This is sort of the opposite of

> <https://github.com/discourse/discourse/blob/main/lib/tasks/uploads.rake#L336-L351>

but instead of `FileUtils.rm(file_path)` I’d instead do an `Upload.create`, I think.

If this seems really stupid or there is a much better solution, I’d love to hear it before I go down this little rabbit hole.

Thanks.

I don’t know how this happened. I was hoping to pin the blame on a custom plugin, but I’m afraid that’s not the case. It may be related to another discussion, in which someone said:

> [@Adding a stable way of adding whitelisted "orphaned" uploads in a plugin](https://meta.discourse.org/t/adding-a-stable-way-of-adding-whitelisted-orphaned-uploads-in-a-plugin/128354/2):
>
> This is so clunky

---

_[View the full topic](https://meta.discourse.org/t/re-adding-missing-uploads-to-the-database/152978)._
