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

TenantFileSystemStorage doesn't work anymore #249

Closed
stygmate opened this issue Mar 21, 2019 · 14 comments
Closed

TenantFileSystemStorage doesn't work anymore #249

stygmate opened this issue Mar 21, 2019 · 14 comments

Comments

@stygmate
Copy link

it's pretty easy to view the problem
just launch:

./manage.py tenant_command shell --schema=schema1

and in the shell:

from django.core.files.storage import default_storage
print(default_storage.base_location)

the output will be:

/media/public
@stygmate
Copy link
Author

@tomturner can you check that ?

@atodorov
Copy link
Member

In my testing, starting with clean MEDIA_ROOT directory whichever tenant receives the first file upload ends up determining the directory path structure.

So if I first upload to the public schema I get uploads/tenants/public/... and everything ends up there.

If I first upload to a schema called aero I get uploads/tenants/aero/... and then uploads on public end up here too. Will look into the code for and try to make a PR.

@stygmate
Copy link
Author

stygmate commented Mar 29, 2019

@atodorov i'm not behind the code but if i remember they removed path and url methods overides of FileSystemStorage in TenantFileSystemStorage and keeped overided code only in init
...

atodorov added a commit to MrSenko/django-tenants that referenced this issue Mar 29, 2019
according to documentation TenantFileSystemStorage() will create
a separate directory for each schema_name under MEDIA_ROOT.

This is not currently the case! My speculation is that this is
broken because the subdir prefix is configured only once in the
constructor of TenantFileSystemStorage() and then cached. Thus we
end-up using the schema_name of the first tenant which happens to
save files.

The fact that TenantFileSystemStorageTestCase.test_format_string
broke without changing anything else supports the above speculation.
The reason is that the new
test_files_are_saved_under_subdirectories_per_tenant() test method
is executed *before* test_format_string()!
atodorov added a commit to MrSenko/django-tenants that referenced this issue Mar 29, 2019
according to documentation TenantFileSystemStorage() will create
a separate directory for each schema_name under MEDIA_ROOT.

This is not currently the case! See:
https://travis-ci.org/tomturner/djago-tenants/jobs/513242830

My speculation is that this is broken because the subdir prefix
is configured only once in the constructor of
TenantFileSystemStorage() and then cached. Thus we end-up using
the schema_name of the first tenant which happens to save files.

The fact that TenantFileSystemStorageTestCase.test_format_string
broke without changing anything else supports the above speculation.
The reason is that the new
test_files_are_saved_under_subdirectories_per_tenant() test method
is executed *before* test_format_string()!
atodorov added a commit to MrSenko/django-tenants that referenced this issue Mar 29, 2019
…tenants#249

because the both depend on the current schema_name they can't
be cached, nor can be initialized only once in the constructor.
An important reason is that the `default_storage` class in Django
is initialized only once!

Also includes a small performance improvement which prevents the
raising and catching of exceptions when
MULTITENANT_RELATIVE_MEDIA_ROOT is not specified at all!

Note: looks like this changed in django-tenants#198 but looking at the PR doesn't
reveal why that is. There is a discussion about static file finders
and template loaders but nothing for TenantFileSystemStorage().
@jcass77
Copy link
Contributor

jcass77 commented Apr 1, 2019

Should these changes perhaps also apply to TenantStaticFilesStorage as it inherits from FileSystemStorage too?

@atodorov
Copy link
Member

atodorov commented Apr 1, 2019

@jcass77 maybe but I don't use that storage nor have the capacity to figure out how not to break existing behavior for people who are using the other storage classes.

@stygmate
Copy link
Author

stygmate commented Apr 1, 2019

@atodorov maybe write a alternative BetterTenantFileSystemStorage class and pull it :) ?

@alexandernst
Copy link
Contributor

I can confirm that this also applies to TenantStaticFilesStorage. Whatever tenant receives the first request get's to be the default and only for all requests, no matter to which tenant the requests are being directed to.

@jcass77
Copy link
Contributor

jcass77 commented Apr 2, 2019

Linking in related issue #248.

@alexandernst
Copy link
Contributor

Here is the MR for this issue (the static files part): #255

@alexandernst
Copy link
Contributor

@atodorov Can you fix your unit tests in #252 so @tomturner can review it, and also review #255 and merge them if they look fine?

@atodorov
Copy link
Member

atodorov commented Apr 2, 2019

@atodorov Can you fix your unit tests in #252

No, because I don't know how!

As I've said in #252 (comment) using the standard executor everything works and I've also verified that manually but I have no idea why the multiprocessing executor fails and I am asking for help to debug or hints how to figure out what the problem is.

You are doing work in the same space so maybe you can figure it out. Without more hints I'm kind of stuck on that multiprocessing test and not sure why Travis fails.

@alexandernst
Copy link
Contributor

@atodorov ok, let me see if I can get around this. Do you mind if I hijack your MR?

@atodorov
Copy link
Member

atodorov commented Apr 2, 2019

go ahead, we can always cherry-pick, rebase or whatever.

alexandernst pushed a commit to Develatio/django-tenants that referenced this issue Apr 3, 2019
according to documentation TenantFileSystemStorage() will create
a separate directory for each schema_name under MEDIA_ROOT.

This is not currently the case! See:
https://travis-ci.org/tomturner/djago-tenants/jobs/513242830

My speculation is that this is broken because the subdir prefix
is configured only once in the constructor of
TenantFileSystemStorage() and then cached. Thus we end-up using
the schema_name of the first tenant which happens to save files.

The fact that TenantFileSystemStorageTestCase.test_format_string
broke without changing anything else supports the above speculation.
The reason is that the new
test_files_are_saved_under_subdirectories_per_tenant() test method
is executed *before* test_format_string()!
alexandernst pushed a commit to Develatio/django-tenants that referenced this issue Apr 3, 2019
…tenants#249

because the both depend on the current schema_name they can't
be cached, nor can be initialized only once in the constructor.
An important reason is that the `default_storage` class in Django
is initialized only once!

Also includes a small performance improvement which prevents the
raising and catching of exceptions when
MULTITENANT_RELATIVE_MEDIA_ROOT is not specified at all!

Note: looks like this changed in django-tenants#198 but looking at the PR doesn't
reveal why that is. There is a discussion about static file finders
and template loaders but nothing for TenantFileSystemStorage().
@tomturner
Copy link
Member

@alexandernst and @atodorov thank you for all your hard work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants