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
49 changes: 29 additions & 20 deletions openslides_backend/migrations/sql_diff_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ def remove_empty(dictionary: dict[str, Any], key: str) -> None:
del dictionary[key]


def check_changed_view_primary(collection_name: str, field_name: str) -> None:
"""Prints a warning if the field became a view or changed in primary side."""
curr_field_def = CURR_MODELS[collection_name]["fields"][field_name]
prev_field_def = PREV_MODELS[collection_name]["fields"][field_name]
is_view, is_primary, _ = get_view_field_state_write_fields(
collection_name, field_name, curr_field_def
)
with prev_models_context():
was_view, was_primary, _ = get_view_field_state_write_fields(
collection_name, field_name, prev_field_def
)

for label, previous, current in (
("view", was_view, is_view),
("primary", was_primary, is_primary),
):
if previous != current:
print(
f"WARNING: The {label} state of {collection_name}/{field_name} "
f"changed from {previous} to {current}; "
f"ensure everything gets properly dropped and recreated."
)


def alter_views_conditionally(
collection_name: str, has_write_fields: bool, is_view_field: bool
) -> None:
Expand Down Expand Up @@ -645,6 +669,7 @@ def handle_remove_table_fields(
for field_name in remove_list:
field_def = PREV_MODELS[collection_name]["fields"][field_name]
if field_def.get("calculated"):
dc_remove_list.remove(field_name)
continue

drop_column = True
Expand All @@ -669,26 +694,6 @@ def handle_remove_table_fields(
result += AlterSchemaHelper.get_drop_column_statement(
collection_name, field_name, PREV_CODE_BLOCKS["view_sql"]
)
type_ = field_def["type"]
if "relation" in type_ and not type_.startswith("generic"):
with prev_models_context():
foreign_table_field: TableFieldType = (
TableFieldType.get_definitions_from_foreign(
field_def.get("to"), field_def.get("reference")
)
)
fk, idx = HelperGetNames.get_fk_and_index_name(
HelperGetNames.get_table_name(collection_name),
field_name,
HelperGetNames.get_table_name(foreign_table_field.table),
foreign_table_field.ref_column,
)
result += AlterSchemaHelper.get_drop_table_constraint_statement(
collection_name, fk
)
result += AlterSchemaHelper.get_drop_index_statement(
collection_name, idx
)
if isinstance(field_def.get("enum"), list):
# enum exists and is field based
result += AlterSchemaHelper.get_drop_enum_type_statement_from_collection_and_column(
Expand Down Expand Up @@ -766,6 +771,10 @@ def handle_remove_field_attributes(
raise BadCodingException(
f"{collection_name}/{field_name}: '{attr}' is a required field attribute."
)
case "reference":
# Doesn't need handling since it should be done by to.
# Changes should only be effective for 1:1 relations if the writing side flips.
check_changed_view_primary(collection_name, field_name)
case _:
# Skipped as not likely to be removed in the foreseeable future:
# "to" and "reference": can only be removed if type changes to not relational field
Expand Down
4 changes: 2 additions & 2 deletions openslides_backend/migrations/yaml_diff_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from cli.util.util import get_view_field_state_write_fields
from meta.dev.src.helper_get_names import ROOT as CURR_MODELS_DIR
from meta.dev.src.helper_get_names import InternalHelper, build_models_yaml_content
from meta.dev.src.validate import RELATION_TYPES
from openslides_backend.migrations.migration_helper import MigrationHelper

"""
Expand Down Expand Up @@ -264,8 +265,7 @@ def create_remove_recursive(
elif curr_models:
if not len(path) == 3 or not (
isinstance(prev_value, dict)
and "type" in prev_value
and prev_value.get("to")
and prev_value.get("type") in RELATION_TYPES
and was_view_field(path[0], key, prev_value)
):
missing_entries.append(key)
Expand Down
Loading