60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
# Generated by Django 5.2.17 on 2026-08-19
|
|
|
|
from django.db import migrations
|
|
|
|
|
|
def retarget_cycles_flock_fk(apps, schema_editor):
|
|
connection = schema_editor.connection
|
|
if connection.vendor != "sqlite":
|
|
return
|
|
with connection.cursor() as cursor:
|
|
cursor.execute("SELECT sql FROM sqlite_master WHERE name='cycles'")
|
|
row = cursor.fetchone()
|
|
if not row or "flock_old" not in (row[0] or ""):
|
|
return
|
|
cursor.execute("PRAGMA foreign_keys=OFF")
|
|
cursor.execute("ALTER TABLE cycles RENAME TO cycles_old")
|
|
cursor.execute(
|
|
"""
|
|
CREATE TABLE cycles (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
total_days integer NOT NULL,
|
|
start_date date NOT NULL,
|
|
end_date date NOT NULL,
|
|
doc_in_weight integer NOT NULL,
|
|
doc_in_count integer NOT NULL,
|
|
created_at datetime NOT NULL,
|
|
updated_at datetime NOT NULL,
|
|
flock_id bigint NOT NULL REFERENCES flock(id) DEFERRABLE INITIALLY DEFERRED,
|
|
kandang_id bigint NOT NULL REFERENCES kandang(id) DEFERRABLE INITIALLY DEFERRED,
|
|
status varchar(30) NOT NULL,
|
|
CONSTRAINT chk_cycles_date_range CHECK (end_date >= start_date)
|
|
)
|
|
"""
|
|
)
|
|
cursor.execute(
|
|
"""
|
|
INSERT INTO cycles (
|
|
id, total_days, start_date, end_date, doc_in_weight, doc_in_count,
|
|
created_at, updated_at, flock_id, kandang_id, status
|
|
)
|
|
SELECT
|
|
id, total_days, start_date, end_date, doc_in_weight, doc_in_count,
|
|
created_at, updated_at, flock_id, kandang_id, status
|
|
FROM cycles_old
|
|
"""
|
|
)
|
|
cursor.execute("DROP TABLE cycles_old")
|
|
cursor.execute("PRAGMA foreign_keys=ON")
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
("farms", "0003_flock_cycle_nullable"),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(retarget_cycles_flock_fk, migrations.RunPython.noop),
|
|
]
|