bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.45k stars 425 forks source link

Avoid the "best practice" of raising an exception if the public schema is not ready yet and PG_EXTRA_SEARCH_PATHS #662

Closed adrienrusso closed 3 years ago

adrienrusso commented 3 years ago

I noticed that the system check best_practice raises an exception when the command migrate_schemas is run with a fresh new database and PG_EXTRA_SEARCH_PATHS has been set.

It is possible to avoid this error by adding --skip-checks to the command, but It's not a good practice to skip all check because of a failure.

Instead, I suggest the check skip this part if the public schema is not ready.

Stack Trace with an empty database:

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./manage.py", line 33, in <module>
    execute_from_command_line(sys.argv)
  File "/.venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/.venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 366, in execute
    self.check()
  File "/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 392, in check
    all_issues = self._run_checks(
  File "/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "/.venv/lib/python3.8/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/.venv/src/django-tenant-schemas/tenant_schemas/apps.py", line 72, in best_practice
    existing_tenant_schemas = set(get_tenant_model().objects.all().values_list('schema_name', flat=True))
  File "/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 276, in __iter__
    self._fetch_all()
  File "/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 184, in __iter__
    for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
  File "/.venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1104, in results_iter
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
  File "/.venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1152, in execute_sql
    cursor.execute(sql, params)
  File "/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/.venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/monitoring/psycopg2_proxy/base.py", line 61, in execute
    result = self.__subject__.execute(*args, **kwargs)
django.db.utils.ProgrammingError: relation "multi_tenant_tenant" does not exist
LINE 1: SELECT "multi_tenant_tenant"."schema_name" FROM "multi_tenan...
adrienrusso commented 3 years ago

@bernardopires @goodtune Is this package still maintained?