stash
This commit is contained in:
parent
491ee05f15
commit
7c2db40eab
6 changed files with 120 additions and 0 deletions
|
|
@ -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)),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
|
@ -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),
|
||||
),
|
||||
]
|
||||
16
backend/toolshed/migrations/0020_storagelocation_id.py
Normal file
16
backend/toolshed/migrations/0020_storagelocation_id.py
Normal 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),
|
||||
),
|
||||
]
|
||||
|
|
@ -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),
|
||||
]
|
||||
|
|
@ -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'),
|
||||
),
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue