Skip to content

Commit

Permalink
model: add migration to replace meeting primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippKilian committed May 29, 2024
1 parent 382d939 commit 97dc124
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 2 deletions.
17 changes: 17 additions & 0 deletions b3lb/rest/migrations/0017_rename_meeting_meetingold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.23 on 2024-05-24 13:22

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('rest', '0016_resize_meeting_name_length'),
]

operations = [
migrations.RenameModel(
old_name='Meeting',
new_name='MeetingOld',
),
]
44 changes: 44 additions & 0 deletions b3lb/rest/migrations/0018_meeting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 3.2.23 on 2024-05-24 13:29

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import rest.models
import uuid


class Migration(migrations.Migration):

dependencies = [
('rest', '0017_rename_meeting_meetingold'),
]

operations = [
migrations.CreateModel(
name='MeetingNew',
fields=[
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)),
('id', models.CharField(max_length=100)),
('room_name', models.CharField(max_length=256)),
('age', models.DateTimeField(default=django.utils.timezone.now)),
('attendees', models.SmallIntegerField(default=0)),
('end_callback_url', models.URLField(default='')),
('listenerCount', models.SmallIntegerField(default=0)),
('nonce', models.CharField(default=rest.models.get_nonce, editable=False, max_length=64, unique=True)),
('voiceParticipantCount', models.SmallIntegerField(default=0)),
('moderatorCount', models.SmallIntegerField(default=0)),
('videoCount', models.SmallIntegerField(default=0)),
('bbb_origin', models.CharField(default='', max_length=255)),
('bbb_origin_server_name', models.CharField(default='', max_length=255)),
('node', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rest.node')),
('secret', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rest.secret')),
],
options={
'ordering': ['secret__tenant', 'age'],
},
),
migrations.AddConstraint(
model_name='meetingnew',
constraint=models.UniqueConstraint(fields=('id', 'secret'), name='unique_meeting'),
),
]
35 changes: 35 additions & 0 deletions b3lb/rest/migrations/0019_migrate_meeting_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 3.2.23 on 2024-05-24 13:29

from django.db import migrations

def migrate_meetings(apps, schema_editor):
meeting_class = apps.get_model("rest", "MeetingNew")
meeting_old_class = apps.get_model("rest", "MeetingOld")
for meeting_old in meeting_old_class.objects.all():
meeting = meeting_class()
meeting.id = meeting_old.id
meeting.secret = meeting_old.secret
meeting.node = meeting_old.node
meeting.room_name = meeting_old.room_name
meeting.age = meeting_old.age
meeting.attendees = meeting_old.attendees
meeting.end_callback_url = meeting_old.end_callback_url
meeting.listenerCount = meeting_old.listenerCount
meeting.nonce = meeting_old.nonce
meeting.voiceParticipantCount = meeting_old.voiceParticipantCount
meeting.moderatorCount = meeting_old.moderatorCount
meeting.videoCount = meeting_old.videoCount
meeting.bbb_origin = meeting_old.bbb_origin
meeting.bbb_origin_server_name = meeting_old.bbb_origin_server_name
meeting.save()


class Migration(migrations.Migration):

dependencies = [
('rest', '0018_meeting'),
]

operations = [
migrations.RunPython(migrate_meetings)
]
39 changes: 39 additions & 0 deletions b3lb/rest/migrations/0020_remove_meetingold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 3.2.23 on 2024-05-24 13:46

from django.db import migrations, models
import django.db.models.deletion

def migrate_record_set_meetings(apps, schema_editor):
record_set_class = apps.get_model('rest', 'RecordSet')
for record_set in record_set_class.objects.all():
if record_set.meeting:
record_set.meeting = None
record_set.save()


class Migration(migrations.Migration):

dependencies = [
('rest', '0019_migrate_meeting_data'),
]

operations = [
migrations.RunPython(migrate_record_set_meetings),
migrations.AlterField(
model_name='recordset',
name='meeting',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='rest.meetingnew'),
),
migrations.DeleteModel(
name='MeetingOld',
),
migrations.RenameModel(
old_name='MeetingNew',
new_name='Meeting',
),
migrations.AlterField(
model_name='recordset',
name='meeting',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='rest.meeting'),
),
]
23 changes: 23 additions & 0 deletions b3lb/rest/migrations/0021_refill_recordset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.23 on 2024-05-24 14:14

from django.db import migrations

def refill_record_set(apps, schema_editor):
record_set_class = apps.get_model('rest', 'RecordSet')
meeting_class = apps.get_model('rest', 'Meeting')
for record_set in record_set_class.objects.all():
if record_set.status == "UNKNOWN" and record_set.meta_meeting_id:
meetings = meeting_class.objects.filter(id=record_set.meta_meeting_id, secret=record_set.secret)
if meetings.count() == 1:
record_set.meeting = meetings[0]
record_set.save()

class Migration(migrations.Migration):

dependencies = [
('rest', '0020_remove_meetingold'),
]

operations = [
migrations.RunPython(refill_record_set)
]
8 changes: 6 additions & 2 deletions b3lb/rest/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@


from base64 import b32encode
from django.db import models
from django.core.exceptions import ValidationError
from django.core.files.storage import FileSystemStorage, default_storage
from django.core.validators import MaxValueValidator, MinValueValidator, RegexValidator
from django.conf import settings
from django.contrib import admin
from django.db import models
from django.db.models.constraints import UniqueConstraint
from django.utils import timezone
from django.utils.crypto import get_random_string
from django.utils.html import format_html
Expand Down Expand Up @@ -575,7 +576,8 @@ class SecretMetricsListAdmin(admin.ModelAdmin):

# meeting - tenant - node relation class
class Meeting(models.Model):
id = models.CharField(max_length=cst.MEETING_ID_LENGTH, primary_key=True)
uuid = models.UUIDField(primary_key=True, editable=False, unique=True, default=uid.uuid4)
id = models.CharField(max_length=cst.MEETING_ID_LENGTH)
secret = models.ForeignKey(Secret, on_delete=models.CASCADE)
node = models.ForeignKey(Node, on_delete=models.CASCADE)
room_name = models.CharField(max_length=cst.MEETING_NAME_LENGTH)
Expand All @@ -592,11 +594,13 @@ class Meeting(models.Model):

class Meta(object):
ordering = ['secret__tenant', 'age']
constraints = [UniqueConstraint(fields=['id', 'secret'], name='unique_meeting')]

def __str__(self):
return "{} {}".format(self.secret.tenant.slug, self.room_name)


# meeting - tenant - node relation class
class MeetingAdmin(admin.ModelAdmin):
model = Meeting
list_display = ['__str__', 'bbb_origin_server_name', 'node', 'attendees', 'listenerCount', 'voiceParticipantCount', 'videoCount', 'age', 'id']
Expand Down

0 comments on commit 97dc124

Please sign in to comment.