Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow active_storage_blobs.content_type to be NULL #43055

Closed

Conversation

betesh
Copy link
Contributor

@betesh betesh commented Aug 19, 2021

Summary

As of #38124 (Jan, 2020), active_storage_blobs.content_type has a default value so it will never be NULL. Let's enforce that at the database level.

As of rails#38124 (Jan, 2020),
it has a default value so it will never be NULL.
@ghiculescu
Copy link
Member

I'm not sure if this is a good idea for existing apps, who knows what sort of data they have in there. I think it is a good idea for new apps though.

It would also need a changelog entry, maybe in the changelog you can suggest that existing Active Storage users write a migration for their own tables if they want to.

@brenogazzola
Copy link
Contributor

brenogazzola commented Aug 20, 2021

@betesh Here's a suggestion on how to solve some of the null values:

missing_files = []
ActiveStorage::Blob.where(content_type: nil).find_each do |blob|
  blob.identified = false
  blob.identify
rescue
  missing_files << blob.id
end

puts "The following blobs are missing their files: #{missing_files}"

In my case, out of 2M blobs I had two with NULL and their files were missing. In both cases blob.attachments.present? returned false, so it seems these were orphaned blobs that are probably safe to delete.

@betesh
Copy link
Contributor Author

betesh commented Aug 24, 2021

One option is putting @brenogazzola 's suggestion in the update-migration with enough comments that if manual intervention is required, it will be easy enough for the user to figure out what to do. For example:

def change
  missing_files = []
  ActiveStorage::Blob.where(content_type: nil).find_each do |blob|
    blob.identified = false
    blob.identify
  rescue
    if blob.attachments.present?
      missing_files << blob.id
    else
      files_that_cannot_be_identified << blob.id
    end
  end

  raise "content_type could not be identified for the following files: #{files_that_cannot_be_identified}.  Please set the content type manually and then re-run this migration." if files_that_cannot_be_identified.present?
  raise "Some blobs are missing their files: #{missing_files}.  These can probably safely be deleted.  After deleting or setting the content type manually, please re-run this migration" if missing_files.present?
  change_column_null(:active_storage_blobs, :content_type, false)
end

Pros: Enables existing apps to apply the migration with minimal confusion.
Cons: It can still be confusing. We're encouraging people to delete records when they don't necessarily have a full understanding of what they're doing.

Overall, I think the risk of users deleting records without knowing what they are doing is too high and the best thing is to remove the update migration and it makes more sense to go with @ghiculescu's suggestion. Agreed?

@rails-bot
Copy link

rails-bot bot commented Nov 22, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Nov 22, 2021
@rails-bot rails-bot bot closed this Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants