rewrite migration to stay compatible with sqlite

This commit is contained in:
Laura Klünder 2024-02-09 21:35:50 +01:00
parent a8261d0b3d
commit 24e14c32ae
2 changed files with 51 additions and 37 deletions

View file

@ -0,0 +1,29 @@
import numpy as np
from django.core.management.base import BaseCommand
from c3nav.mapdata.models.geometry.space import WifiMeasurement, RangingBeacon
class Command(BaseCommand):
help = 'list FTM-capable APs that have been spotted'
def handle(self, *args, **options):
found_beacons = {}
# lets collect them
for wifi_measurement in WifiMeasurement.objects.select_related("space"):
for measurement_list in wifi_measurement.data:
for measurement in measurement_list:
if measurement.get("supports80211mc", False) or measurement.get("distance", None):
found_beacons.setdefault(measurement["bssid"], []).append((wifi_measurement, measurement))
# put in the ones we know
known = {r.bssid: r for r in RangingBeacon.objects.all()}
# lets go through them
for bssid, measurements in found_beacons.items():
print(bssid, measurements[0][1]["ssid"], known.get(bssid))
points = {wifi_measurement.geometry for wifi_measurement, measurement in measurements}
for wifi_measurement, measurement in measurements:
break
#print(f" Space={wifi_measurement.space.title!r} RSSI={measurement['rssi']} "
# f"distance={measurement.get('distance')} distance_sd={measurement.get('distance_sd')}")

View file

@ -2,47 +2,32 @@
from django.db import migrations from django.db import migrations
sql_up = ''' def forwards_func(apps, schema_editor):
-- create an obstacle group for every distinct obstacle color ObstacleGroup = apps.get_model('mapdata', 'ObstacleGroup')
insert into mapdata_obstaclegroup (color, titles) Obstacle = apps.get_model('mapdata', 'Obstacle')
select color as color, LineObstacle = apps.get_model('mapdata', 'LineObstacle')
json_build_object('en', concat('Color ', color)) as titles
from
(select distinct color
from mapdata_obstacle
where color is not null
union
select distinct color
from mapdata_lineobstacle
where color is not null) as obstacle_colors;
-- set the groups for colored obstacles to the previously created group with that color grouped = {}
update mapdata_obstacle as o
set group_id = g.id
from mapdata_obstaclegroup g
where g.color = o.color;
update mapdata_lineobstacle as o for obstacle in Obstacle.objects.filter(color__isnull=False):
set group_id = g.id grouped.setdefault(obstacle.color, []).append(obstacle)
from mapdata_obstaclegroup g
where g.color = o.color;
'''
sql_down = ''' for lineobstacle in LineObstacle.objects.filter(color__isnull=False):
-- set obstacle color from associated group color and remove group grouped.setdefault(lineobstacle.color, []).append(lineobstacle)
update mapdata_obstacle as o
set color = g.color, group_id = null
from mapdata_obstaclegroup g
where g.id = o.group_id;
update mapdata_lineobstacle as o for color, obstacles in grouped.items():
set color = g.color, group_id = null group = ObstacleGroup.objects.create(
from mapdata_obstaclegroup g color=color,
where g.id = o.group_id; titles={"en": f"Color {color}"}
)
for obstacle in obstacles:
obstacle.group = group
obstacle.save()
-- delete groups
delete from mapdata_obstaclegroup where true; def backwards_func(apps, schema_editor):
''' ObstacleGroup = apps.get_model('mapdata', 'ObstacleGroup')
ObstacleGroup.objects.all().delete()
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -52,5 +37,5 @@ class Migration(migrations.Migration):
] ]
operations = [ operations = [
migrations.RunSQL(sql_up, sql_down) migrations.RunPython(forwards_func, backwards_func)
] ]