# 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:** 4

<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 28, 2020, 11:16pm UTC](https://meta.discourse.org/t/re-adding-missing-uploads-to-the-database/152978/4 "2020-05-28T23:16:03Z")

</div>

I’m trying something like this:

```ruby
def add_missing_files_to_uploads
  public_directory = Rails.root.join("public").to_s
  db = RailsMultisite::ConnectionManagement.current_db
  uploads_directory = File.join(public_directory, 'uploads', db).to_s
  # uploads and optimized images
  missing = 0
  matched = 0
  Dir.glob("#{uploads_directory}/**/*.pdf").each do |file_path|
    sha1 = Upload.generate_digest(file_path)
    url = file_path.split(public_directory, 2)[1]
    if (Upload.where(sha1: sha1).empty? &&
        Upload.where(url: url).empty?)
      puts "MISSING #{file_path}" if DEBUG
      missing += 1
    else
      matched += 1
    end
  end
  puts "MISS: #{missing}. Match #{matched}"
end

```

Does that look sort of close? My first test seems to have failed, but I might have screwed something up.

---

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