This commit is contained in:
2026-05-02 05:29:54 +03:30
parent 8d73b1703c
commit bd02c8342a
@@ -4,12 +4,128 @@ import django.db.models.deletion
from django.db import migrations, models
def _table_exists(connection, table_name):
with connection.cursor() as cursor:
return table_name in connection.introspection.table_names(cursor)
def _column_names(connection, table_name):
with connection.cursor() as cursor:
description = connection.introspection.get_table_description(cursor, table_name)
return {column.name for column in description}
def _constraint_names(connection, table_name):
with connection.cursor() as cursor:
constraints = connection.introspection.get_constraints(cursor, table_name)
return set(constraints.keys())
def sync_farmer_calendar_schema(apps, schema_editor):
connection = schema_editor.connection
zone_table = "farmer_calendar_zones"
event_table = "farmer_calendar_events"
if not _table_exists(connection, zone_table):
schema_editor.execute(
"""
CREATE TABLE farmer_calendar_zones (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
uuid CHAR(32) NOT NULL UNIQUE,
label VARCHAR(255) NOT NULL,
value VARCHAR(255) NOT NULL,
is_active BOOL NOT NULL DEFAULT TRUE,
created_at DATETIME(6) NOT NULL,
updated_at DATETIME(6) NOT NULL,
farm_id BIGINT NOT NULL,
CONSTRAINT farmer_calendar_zones_farm_id_fk
FOREIGN KEY (farm_id) REFERENCES farm_hub_farmhub (id)
)
"""
)
zone_constraints = _constraint_names(connection, zone_table)
if "uniq_farmer_calendar_zone_per_farm" not in zone_constraints:
schema_editor.execute(
"""
ALTER TABLE farmer_calendar_zones
ADD CONSTRAINT uniq_farmer_calendar_zone_per_farm UNIQUE (farm_id, value)
"""
)
event_columns = _column_names(connection, event_table)
if "priority" not in event_columns:
schema_editor.execute(
"""
ALTER TABLE farmer_calendar_events
ADD COLUMN priority VARCHAR(16) NULL
"""
)
if "scheduled_date" not in event_columns:
schema_editor.execute(
"""
ALTER TABLE farmer_calendar_events
ADD COLUMN scheduled_date DATE NULL
"""
)
if "status" not in event_columns:
schema_editor.execute(
"""
ALTER TABLE farmer_calendar_events
ADD COLUMN status VARCHAR(16) NOT NULL DEFAULT 'open'
"""
)
if "time" not in event_columns:
schema_editor.execute(
"""
ALTER TABLE farmer_calendar_events
ADD COLUMN time TIME NULL
"""
)
if "zone_id" not in event_columns:
schema_editor.execute(
"""
ALTER TABLE farmer_calendar_events
ADD COLUMN zone_id BIGINT NULL
"""
)
schema_editor.execute(
"""
ALTER TABLE farmer_calendar_events
ADD CONSTRAINT farmer_calendar_events_zone_id_fk
FOREIGN KEY (zone_id) REFERENCES farmer_calendar_zones (id)
"""
)
schema_editor.execute(
"""
ALTER TABLE farmer_calendar_events
MODIFY COLUMN start DATETIME(6) NULL
"""
)
schema_editor.execute(
"""
ALTER TABLE farmer_calendar_events
MODIFY COLUMN end DATETIME(6) NULL
"""
)
def noop_reverse(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
("farmer_calendar", "0001_initial"),
]
operations = [
migrations.SeparateDatabaseAndState(
database_operations=[
migrations.RunPython(sync_farmer_calendar_schema, noop_reverse),
],
state_operations=[
migrations.CreateModel(
name="FarmerCalendarZone",
fields=[
@@ -22,7 +138,11 @@ class Migration(migrations.Migration):
("updated_at", models.DateTimeField(auto_now=True)),
(
"farm",
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="calendar_zones", to="farm_hub.farmhub"),
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="calendar_zones",
to="farm_hub.farmhub",
),
),
],
options={
@@ -30,11 +150,6 @@ class Migration(migrations.Migration):
"ordering": ["label"],
},
),
migrations.AddField(
model_name="farmercalendarevent",
name="deadline",
field=models.BigIntegerField(blank=True, null=True),
),
migrations.AddField(
model_name="farmercalendarevent",
name="priority",
@@ -89,4 +204,6 @@ class Migration(migrations.Migration):
model_name="farmercalendarzone",
constraint=models.UniqueConstraint(fields=("farm", "value"), name="uniq_farmer_calendar_zone_per_farm"),
),
],
),
]