sio2project/oioioi

Migration 0002 in OI app breaks clearing database

Closed this issue · 3 comments

Migration 0002 in OI app causes db clearing to not work, by breaking the python manage.py flush command (and by extension: ./easytoolbox.py flush-db), which is needed for running cypress tests locally.

File: https://github.com/sio2project/oioioi/blob/c3c399b02312dc1420aa43b474b9c01d10e9463e/oioioi/oi/migrations/0002_auto_20160412_1720.py

This migration removes Region and OIOnsiteRegistration models from the OI app, but does not delete their tables from the database (migrations.SeparateDatabaseAndState). Said tables contain non-nullable foreign key fields to other tables, causing an error when trying to clear the db, as Django only clears tables related to existing objects.

Depending on whether deleting these tables is acceptable, I found two possible solutions:

  1. If we can't delete the tables, we can make the foreign key fields nullable:
database_operations = [
        migrations.AlterField(
            model_name='oionsiteregistration',
            name='region',
            field=models.IntegerField(verbose_name='region', null=True),
        ),
        migrations.AlterField(
            model_name='oionsiteregistration',
            name='participant',
            field=oioioi.participants.fields.OneToOneBothHandsCascadingParticipantField(related_name='oi_oionsiteregistration', to='participants.Participant', on_delete=models.CASCADE, null=True),
        ),
        migrations.AlterField(
            model_name='region',
            name='contest',
            field=models.ForeignKey(to='contests.Contest', on_delete=models.CASCADE, null=True),
        )
    ]

    state_operations = [
        migrations.DeleteModel('oionsiteregistration'),
        migrations.DeleteModel('region'),
    ]

    operations = [
        migrations.SeparateDatabaseAndState(
            database_operations=database_operations,
            state_operations=state_operations)
    ]
  1. If we can delete said tables:
operations = [
        migrations.DeleteModel(
            name='OIOnsiteRegistration',
        ),
        migrations.DeleteModel(
            name='Region',
        ),
    ]

Both solutions fix the issue and pass all tests (as of 19.03.2024 / commit 056caf6).

This is probably caused by f55acf6.

Created branches with implemented solutions for testing:

With deleting tables:
https://github.com/dawidratynski/oioioi/tree/fix-oi-migration-v1

Without deleting tables:
https://github.com/dawidratynski/oioioi/tree/fix-oi-migration-v2

I prefer solution without removing tables.