try with connection_created signal

Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
Marc 'risson' Schmitt 2023-12-28 17:15:41 +01:00
parent 32b40a3bbb
commit 764a999db8
No known key found for this signature in database
GPG Key ID: 9C3FA22FABF1AA8D
1 changed files with 14 additions and 3 deletions

View File

@ -1,14 +1,25 @@
"""authentik tenants app""" """authentik tenants app"""
from django.core.signals import setting_changed
from django.db import DEFAULT_DB_ALIAS from django.db import DEFAULT_DB_ALIAS
from django.db.backends.signals import connection_created
from django.db.models.signals import post_migrate from django.db.models.signals import post_migrate
from django_tenants.utils import get_public_schema_name from django_tenants.utils import get_public_schema_name
from authentik.blueprints.apps import ManagedAppConfig from authentik.blueprints.apps import ManagedAppConfig
def ensure_default_tenant(*args, using=DEFAULT_DB_ALIAS, **kwargs): def ensure_default_tenant(*args, using=DEFAULT_DB_ALIAS, connection=None, **kwargs):
"""Make sure default tenant exists""" """Make sure default tenant exists"""
# Make sure we've migrated before touching the database
if connection is not None:
with connection.cursor() as cursor:
cursor.execute(
"SELECT * FROM information_schema.tables "
"WHERE table_schema = 'public' AND table_name = 'authentik_tenants_tenant'"
)
if not bool(cursor.rowcount):
return
using = connection.alias
from django_tenants.utils import schema_context from django_tenants.utils import schema_context
from authentik.tenants.models import Tenant from authentik.tenants.models import Tenant
@ -32,4 +43,4 @@ class AuthentikTenantsConfig(ManagedAppConfig):
"""Make sure default tenant exists, especially after a migration""" """Make sure default tenant exists, especially after a migration"""
ensure_default_tenant() ensure_default_tenant()
post_migrate.connect(ensure_default_tenant) post_migrate.connect(ensure_default_tenant)
setting_changed.connect(ensure_default_tenant) connection_created.connect(ensure_default_tenant)