cmsmanage/paperwork/migrations/0002_add_certification_version_model.py

61 lines
2.4 KiB
Python
Raw Normal View History

# (Partially) Generated by Django 4.0.2 on 2022-02-04 18:01
from django.db import migrations, models
import django.db.models.deletion
def migrate_certification_version_forward(apps, schema_editor):
db_alias = schema_editor.connection.alias
Certification = apps.get_model("paperwork", "Certification")
CertificationVersion = apps.get_model("paperwork", "CertificationVersion")
for certification in Certification.objects.using(db_alias).all():
(version, _) = CertificationVersion.objects.using(db_alias).get_or_create(
definition=certification.certification, version=certification.version
)
certification.certification_version = version
certification.save()
class Migration(migrations.Migration):
dependencies = [
('membershipworks', '0001_initial'),
('paperwork', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='CertificationVersion',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('version', models.CharField(blank=True, db_column='Version', max_length=255, null=True)),
('definition', models.ForeignKey(db_column='Certification', on_delete=django.db.models.deletion.PROTECT, to='paperwork.certificationdefinition')),
],
),
migrations.AddConstraint(
model_name='certificationversion',
constraint=models.UniqueConstraint(fields=('definition', 'version'), name='unique_certification_version'),
),
migrations.AddField(
model_name='certification',
name='certification_version',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='paperwork.certificationversion', null=True),
),
migrations.RunPython(migrate_certification_version_forward),
migrations.AlterField(
model_name='certification',
name='certification_version',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='paperwork.certificationversion'),
),
migrations.RemoveField(
model_name='certification',
name='certification',
),
migrations.RemoveField(
model_name='certification',
name='version',
),
]