feat: Implement Dockerized multi-tenant Django application with initial setup for database, migrations, and superuser creation.
This commit is contained in:
45
docker/create_public_tenant.py
Normal file
45
docker/create_public_tenant.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import os
|
||||
import django
|
||||
from django.conf import settings
|
||||
|
||||
import sys
|
||||
|
||||
# Add /app to sys.path so we can import myproject
|
||||
sys.path.append('/app')
|
||||
|
||||
# Setup Django environment
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
|
||||
django.setup()
|
||||
|
||||
from tenants.models import Client, Domain
|
||||
|
||||
def ensure_public_tenant():
|
||||
domain_name = os.environ.get('DOMAIN_NAME', 'localhost')
|
||||
print(f"Checking public tenant for domain: {domain_name}")
|
||||
|
||||
# 1. Ensure Client exists
|
||||
client, created = Client.objects.get_or_create(
|
||||
schema_name='public',
|
||||
defaults={'name': 'Main Tenant'}
|
||||
)
|
||||
if created:
|
||||
print("Created public tenant client.")
|
||||
else:
|
||||
print("Public tenant client already exists.")
|
||||
|
||||
# 2. Ensure Domain exists
|
||||
# Check if this specific domain exists
|
||||
domain, created = Domain.objects.get_or_create(
|
||||
domain=domain_name,
|
||||
defaults={'tenant': client, 'is_primary': True}
|
||||
)
|
||||
|
||||
if created:
|
||||
print(f"Created domain {domain_name} for public tenant.")
|
||||
else:
|
||||
print(f"Domain {domain_name} already exists.")
|
||||
if domain.tenant != client:
|
||||
print(f"WARNING: Domain {domain_name} is assigned to another tenant!")
|
||||
|
||||
if __name__ == '__main__':
|
||||
ensure_public_tenant()
|
||||
Reference in New Issue
Block a user