Skip to content

Commit

Permalink
feat: use uuid7 instead of uuid4 from now on
Browse files Browse the repository at this point in the history
UUID7 has the nice property of being semi-sequential, so it will be much
nicer on DB indexes, and following that, improve query performance as well

Note we don't fully replace UUID4 everywhere: In tests, we don't care too
much, so we don't build a UUID7 faker for those IDs, and in the Analytics
module, we use UUID4 as a simple "random string" utility for generated
identifiers (and using only the left few chars of it would break when using
UUID7).
  • Loading branch information
winged committed Oct 16, 2024
1 parent 8038e76 commit 59e7db8
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions caluma/caluma_analytics/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def path_from_root(self):

def _alias(self):
if not self.alias: # pragma: todo cover
# Note: Not using UUID7 here, as we need the "full" randomness
self.alias = slugify(self.identifier + "_" + str(uuid4())[:5]).replace(
"-", "_"
)
Expand Down
2 changes: 2 additions & 0 deletions caluma/caluma_core/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


class DjangoModelFactory(django.DjangoModelFactory):
# TODO these are still UUID4, but it's tests only, so we don't really
# care that much
created_by_user = Faker("uuid4")
created_by_group = Faker("uuid4")
modified_by_user = LazyAttribute(lambda inst: inst.created_by_user)
Expand Down
7 changes: 4 additions & 3 deletions caluma/caluma_core/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import uuid

import uuid_extensions
from django.db import models
from simple_history.models import HistoricalRecords

Expand Down Expand Up @@ -79,7 +78,9 @@ class UUIDModel(BaseModel, HistoricalModel):
Defined as Caluma default
"""

id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
id = models.UUIDField(
primary_key=True, default=uuid_extensions.uuid7, editable=False
)

def __repr__(self):
return f"{self.__class__.__name__}(id={self.id})"
Expand Down
6 changes: 3 additions & 3 deletions caluma/caluma_data_source/tests/data_sources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from uuid import uuid4
from uuid_extensions import uuid7str

from caluma.caluma_data_source.data_sources import BaseDataSource
from caluma.caluma_data_source.utils import data_source_cache
Expand Down Expand Up @@ -28,11 +28,11 @@ def get_data_test_string(self, info):

@data_source_cache(timeout=60)
def get_data_uuid(self, info):
return str(uuid4())
return uuid7str()

@data_source_cache(timeout=1)
def get_data_expire(self, info):
return str(uuid4())
return uuid7str()


class MyOtherDataSource(MyDataSource):
Expand Down
6 changes: 4 additions & 2 deletions caluma/caluma_form/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uuid
from functools import wraps

import uuid_extensions
from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.indexes import GinIndex
from django.db import models, transaction
Expand Down Expand Up @@ -476,7 +476,9 @@ class Answer(core_models.BaseModel):

# We need to replicate the UUIDModel in order to register it as historical model.
# Otherwise simple_history complains that the model is already registered,
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
id = models.UUIDField(
primary_key=True, default=uuid_extensions.uuid7, editable=False
)
question = models.ForeignKey(
"caluma_form.Question", on_delete=models.DO_NOTHING, related_name="answers"
)
Expand Down
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pyjexl = "^0.3.0"
python-memcached = "^1.62"
requests = "^2.31.0"
urllib3 = ">=1.26.8,<3.0.0"
uuid7 = "^0.1.0"

[tool.poetry.group.dev.dependencies]
# Dev dependencies are always pinned to an exact version (No caret or tilde
Expand Down

0 comments on commit 59e7db8

Please sign in to comment.