This commit is contained in:
j3d1 2026-08-26 17:53:29 +02:00
parent 491ee05f15
commit 7c2db40eab
6 changed files with 120 additions and 0 deletions

View file

@ -0,0 +1,23 @@
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('toolshed', '0017_inventoryitem_id_not_null_and_unique'),
]
operations = [
migrations.CreateModel(
name='OwnerStorageLocationSequence',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('last_id', models.PositiveIntegerField(default=0)),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+',
to=settings.AUTH_USER_MODEL, unique=True)),
],
),
]

View file

@ -0,0 +1,21 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('toolshed', '0018_ownerstoragelocationsequence'),
]
operations = [
migrations.RenameField(
model_name='storagelocation',
old_name='id',
new_name='internal_id',
),
migrations.AlterField(
model_name='storagelocation',
name='internal_id',
field=models.AutoField(primary_key=True, serialize=False),
),
]

View file

@ -0,0 +1,16 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('toolshed', '0019_rename_id_storagelocation_internal_id'),
]
operations = [
migrations.AddField(
model_name='storagelocation',
name='id',
field=models.PositiveIntegerField(editable=False, null=True),
),
]

View file

@ -0,0 +1,40 @@
from django.db import migrations
def collapse_ids(apps, schema_editor):
"""Collapse each owner's StorageLocation ids from the sparse global range they had before this
migration down to a continuous 1..N range, in original creation order (internal_id order). Then
seed OwnerStorageLocationSequence so future allocation continues right after."""
StorageLocation = apps.get_model('toolshed', 'StorageLocation')
OwnerStorageLocationSequence = apps.get_model('toolshed', 'OwnerStorageLocationSequence')
scope = None
next_id = 0
counts = {}
for location in StorageLocation.objects.order_by('owner_id', 'internal_id'):
key = location.owner_id
if key != scope:
scope = key
next_id = 0
next_id += 1
location.id = next_id
location.save(update_fields=['id'])
counts[key] = next_id
for owner_id, count in counts.items():
OwnerStorageLocationSequence.objects.update_or_create(owner_id=owner_id, defaults={'last_id': count})
def noop_reverse(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
('toolshed', '0020_storagelocation_id'),
]
operations = [
migrations.RunPython(collapse_ids, noop_reverse),
]

View file

@ -0,0 +1,20 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('toolshed', '0021_backfill_storagelocation_id'),
]
operations = [
migrations.AlterField(
model_name='storagelocation',
name='id',
field=models.PositiveIntegerField(editable=False),
),
migrations.AddConstraint(
model_name='storagelocation',
constraint=models.UniqueConstraint(fields=('owner', 'id'), name='storagelocation_unique_owner_scoped_id'),
),
]

Binary file not shown.