2017-11-16 00:44:00 +01:00
|
|
|
from django.db import migrations, models
|
2020-04-27 07:19:08 +02:00
|
|
|
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
|
2016-10-27 03:05:21 +02:00
|
|
|
from django.db.migrations.state import StateApps
|
2017-11-16 00:44:00 +01:00
|
|
|
from django.db.utils import IntegrityError
|
2016-10-27 03:05:21 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-10-26 11:36:30 +02:00
|
|
|
def set_string_id_using_domain(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
|
2016-10-27 03:05:21 +02:00
|
|
|
Realm = apps.get_model('zerver', 'Realm')
|
|
|
|
for realm in Realm.objects.all():
|
|
|
|
if not realm.string_id:
|
|
|
|
prefix = realm.domain.split('.')[0]
|
|
|
|
try:
|
|
|
|
realm.string_id = prefix
|
|
|
|
realm.save(update_fields=["string_id"])
|
|
|
|
continue
|
|
|
|
except IntegrityError:
|
|
|
|
pass
|
|
|
|
for i in range(1, 100):
|
|
|
|
try:
|
|
|
|
realm.string_id = prefix + str(i)
|
|
|
|
realm.save(update_fields=["string_id"])
|
|
|
|
continue
|
|
|
|
except IntegrityError:
|
|
|
|
pass
|
2020-06-10 06:41:04 +02:00
|
|
|
raise RuntimeError(f"Unable to find a good string_id for realm {realm}")
|
2016-10-27 03:05:21 +02:00
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('zerver', '0036_rename_subdomain_to_string_id'),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
2020-04-29 08:43:25 +02:00
|
|
|
migrations.RunPython(set_string_id_using_domain, elidable=True),
|
2016-10-27 03:05:21 +02:00
|
|
|
|
|
|
|
migrations.AlterField(
|
|
|
|
model_name='realm',
|
|
|
|
name='string_id',
|
|
|
|
field=models.CharField(unique=True, max_length=40),
|
|
|
|
),
|
|
|
|
]
|