-
Notifications
You must be signed in to change notification settings - Fork 158
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
Rails 7 Wrong schema_search_path in ActiveStorage::DirectUploadsController #196
Comments
I'm also seeing 404s on files and even more worrisome is that existing URLs are linking to attachments in other workspaces. |
I'm also seeing this problem with Rails 7. For my application, E.g.
I'll try and muck around on a branch and find a fix |
I've opened a PR that might be the fix: #198 |
Hi all, It's not optimized and not very pretty as code, but here is the solution we developed to solve the problem
# frozen_string_literal: true
# Creates a new blob on the server side in anticipation of a direct-to-service upload from the client side.
# When the client-side upload is completed, the signed_blob_id can be submitted as part of the form to reference
# the blob that was created up front.
class ActiveStorage::DirectUploadsController < ActiveStorage::BaseController
def create
subdomain = Apartment::Tenant.current
blob = nil
Apartment::Tenant.switch(subdomain) do
blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_args)
end
render json: direct_upload_json(blob)
end
private
def blob_args
params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {}).to_h.symbolize_keys
end
def direct_upload_json(blob)
blob.as_json(root: false, methods: :signed_id).merge(direct_upload: {
url: blob.service_url_for_direct_upload,
headers: blob.service_headers_for_direct_upload
})
end
end |
I'm uncertain why this would fix anything:
Wouldn't Apartment already be switched to the current tenant? |
It seems like somewhere deep in active storage the schema is reset, without Apartment knowing about this. We came up with this fix in an initializer: module ActiveStorageApartmentFix
extend ActiveSupport::Concern
def key
self[:key] ||= "#{Apartment::Tenant.current}/#{self.class.generate_unique_secure_token(length: 28)}"
end
class_methods do
def create_before_direct_upload!(**args)
Apartment::Tenant.switch(Apartment::Tenant.current) do
super
end
end
end
end
Rails.application.config.to_prepare do
ActiveStorage::Blob.prepend(ActiveStorageApartmentFix)
ActiveRecord::Base.class_eval do
def self.find_signed!(signed_id, purpose: nil)
Apartment::Tenant.switch(Apartment::Tenant.current) do
super
end
end
end
end |
Thanks @ArthurWD for the quick fix. Small addition for those who need to avoid initializing Apartment while precompiling assets: module ActiveStorageApartmentFix
extend ActiveSupport::Concern
def key
self[:key] ||= "#{Apartment::Tenant.current}/#{self.class.generate_unique_secure_token(length: 28)}"
end
class_methods do
def create_before_direct_upload!(**args)
Apartment::Tenant.switch(Apartment::Tenant.current) do
super
end
end
end
end
Rails.application.config.to_prepare do
next if ENV['APARTMENT_DISABLE_INIT']
ActiveStorage::Blob.prepend(ActiveStorageApartmentFix)
ActiveRecord::Base.class_eval do
def self.find_signed!(signed_id, purpose: nil)
Apartment::Tenant.switch(Apartment::Tenant.current) do
super
end
end
end
end |
Steps to reproduce
After upgrading our app to Rails 7.0.2 from 6.1.4, the direct upload no longer works together with the apartment. Blob records are always created in the default schema (or whatever is configured as schema_search_path in database.yml). And if you try to attach such a record to a model, an error is issued because the record cannot be found in the current tenant's schema.
Expected behavior
Blobs should be created the schema of the current tenant.
Actual behavior
The schema_search_path in the create method of the ActiveStorage::DirectUploadsController is set to the default and blobs are inserted in the default schema.
Database: postgres 11
Apartment version: 2.11.0 (I also tried the fix in Fix Rails 7 connection handling #194)
Apartment config (in
config/initializers/apartment.rb
or so):use_schemas
:true
use_sql
:true
Rails (or ActiveRecord) version: 7.0.2
Ruby version: 2.7.5
The text was updated successfully, but these errors were encountered: