Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions cli/generate_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ def main() -> None:
for collection, data in InternalHelper.MODELS.items():
if collection.startswith("_"):
continue
model = Model(collection, data["fields"])
model = Model(
collection,
data["fields"],
[
*data.get("unique_together", []),
*data.get("unique_together_strict", []),
],
)
dest.write(model.get_code())

if args.check:
Expand All @@ -137,11 +144,17 @@ class Model(Node):
MODEL_TEMPLATE = string.Template(dedent("""
class ${class_name}(${base_classes}):
collection = "${collection}"
verbose_name = "${verbose_name}"
verbose_name = "${verbose_name}"${unique_together_fields}
"""))

def __init__(self, collection: str, fields: dict[str, dict[str, Any]]) -> None:
def __init__(
self,
collection: str,
fields: dict[str, dict[str, Any]],
unique_together_fields: list[str] | None = None,
) -> None:
self.collection = collection
self.unique_together_fields = unique_together_fields
assert collection
self.attributes = {}
for field_name, field in fields.items():
Expand All @@ -165,6 +178,11 @@ def get_code(self) -> str:
"base_classes": ", ".join(cls.__name__ for cls in base_classes),
"collection": self.collection,
"verbose_name": verbose_name,
"unique_together_fields": (
f"\n unique_together_fields = {self.unique_together_fields}"
if self.unique_together_fields
else ""
),
}
)
+ "\n"
Expand Down
2 changes: 1 addition & 1 deletion data/example-data.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"_migration_index": 102,
"_migration_index": 103,
"gender":{
"1":{
"id": 1,
Expand Down
2 changes: 1 addition & 1 deletion data/initial-data.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"_migration_index": 102,
"_migration_index": 103,
"gender":{
"1":{
"id": 1,
Expand Down
12 changes: 12 additions & 0 deletions openslides_backend/action/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ def _build_write_request(
self.apply_event(event)
events_by_type[event["type"]].append(event)
information = self.get_full_history_information()
write_request.defer_constraints = self.get_defer_constraints()
if self.is_sub_call:
write_request.information = information
elif information:
Expand Down Expand Up @@ -775,6 +776,17 @@ def get_post_edit_function(self) -> EditFunction | None:
"""
return None

def get_defer_constraints(self) -> list[str] | None:
"""
Returns a list of contraints that should be deferred in the transaction.
Can be used for unique indexes on:
- simple fields
- multiple fields (unique_together and unique_together_strict)
- relation to relation
- generic-relation to relation
"""
return None


def merge_history_informations(
a: HistoryInformation | None, *other: HistoryInformation | None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from datetime import datetime
from zoneinfo import ZoneInfo

from meta.dev.src.generate_sql_schema import Helper
from meta.dev.src.helper_get_names import HelperGetNames

from ....models.models import Motion, MotionCategory
from ....permissions.permissions import Permissions
from ....services.database.commands import GetManyRequest
Expand Down Expand Up @@ -193,3 +196,12 @@ def get_number(
)
number = f"{prefix}{blank}{number_value_str}"
return number, number_value_map[motion_id]

def get_defer_constraints(self) -> list[str] | None:
assert "meeting_id, number" in self.model.unique_together_fields
return [
HelperGetNames.get_unique_constraint_name(
self.model.collection,
Helper.split_unique_together_fields("meeting_id, number"),
)
]
3 changes: 2 additions & 1 deletion openslides_backend/action/actions/user/base_merge_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def add_collection_field_groups(
self._all_collection_fields[collection] = [
i
for i in Class.__dict__.keys()
if i[:1] != "_" and i not in ["collection", "verbose_name", "id"]
if i[:1] != "_"
and i not in ["collection", "verbose_name", "id", "unique_together_fields"]
]
if back_field:
self._collection_back_fields[collection] = back_field
Expand Down
Loading