Skip to content

Commit aa74e06

Browse files
committed
Fix uuids always producing the same result:
* Pass uuid as a callable not a result * Fix re-create the uuid index to make them unique * Re-generate all uuids * Add tests to test uniqueness.
1 parent a68bfd1 commit aa74e06

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

backend/src/appointment/database/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class Appointment(Base):
179179
__tablename__ = 'appointments'
180180

181181
id = Column(Integer, primary_key=True, index=True)
182-
uuid = Column(UUIDType(native=False), default=uuid.uuid4(), index=True)
182+
uuid = Column(UUIDType(native=False), default=uuid.uuid4, index=True, unique=True)
183183
calendar_id = Column(Integer, ForeignKey('calendars.id'))
184184
duration = Column(Integer)
185185
title = Column(encrypted_type(String))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""fix appointment uuids
2+
3+
Revision ID: e6ed0429ed46
4+
Revises: 12c7e1b34dd6
5+
Create Date: 2024-06-13 15:25:07.086174
6+
7+
"""
8+
import uuid
9+
10+
from alembic import op
11+
from sqlalchemy.orm import Session
12+
13+
from appointment.database import models, repo
14+
15+
16+
# revision identifiers, used by Alembic.
17+
revision = 'e6ed0429ed46'
18+
down_revision = '12c7e1b34dd6'
19+
branch_labels = None
20+
depends_on = None
21+
22+
23+
def upgrade() -> None:
24+
session = Session(op.get_bind())
25+
appointments: list[models.Appointment] = session.query(models.Appointment).all()
26+
for appointment in appointments:
27+
appointment.uuid = uuid.uuid4()
28+
session.add(appointment)
29+
session.commit()
30+
31+
32+
def downgrade() -> None:
33+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""update appointments make uuid unique
2+
3+
Revision ID: f732d6e597fe
4+
Revises: e6ed0429ed46
5+
Create Date: 2024-06-13 15:25:22.440864
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'f732d6e597fe'
14+
down_revision = 'e6ed0429ed46'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade() -> None:
20+
op.drop_index('ix_appointments_uuid', 'appointments')
21+
op.create_index('ix_appointments_uuid', 'appointments', ['uuid'], unique=True)
22+
23+
24+
def downgrade() -> None:
25+
op.drop_index('ix_appointments_uuid', 'appointments')
26+
op.create_index('ix_appointments_uuid', 'appointments', ['uuid'], unique=False)

backend/test/unit/test_models.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from appointment.database import models, repo, schemas
2+
3+
4+
class TestAppointment:
5+
def test_appointment_uuids_are_unique(self, with_db, make_caldav_calendar):
6+
calendar = make_caldav_calendar()
7+
with with_db() as db:
8+
9+
def get_data():
10+
return schemas.AppointmentFull(
11+
title='test',
12+
details='my appointment!',
13+
calendar_id=calendar.id,
14+
)
15+
16+
assert len(db.query(models.Appointment).all()) == 0
17+
18+
appointments = [
19+
repo.appointment.create(db, get_data()),
20+
repo.appointment.create(db, get_data()),
21+
repo.appointment.create(db, get_data()),
22+
repo.appointment.create(db, get_data()),
23+
]
24+
25+
assert len(db.query(models.Appointment).all()) == len(appointments)
26+
27+
for appointment in appointments:
28+
assert len(db.query(models.Appointment).filter(models.Appointment.uuid == appointment.uuid).all()) == 1

0 commit comments

Comments
 (0)