sio2project / oioioi

GNU General Public License v3.0
160 stars 70 forks source link

Migration 0002 in OI app breaks clearing database #352

Closed dawidratynski closed 2 months ago

dawidratynski commented 3 months ago

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)
    ]
  2. 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).

A-dead-pixel commented 3 months ago

This is probably caused by f55acf60fdc39f0bde9bc66762ea4a8ad1dfea92.

dawidratynski commented 3 months ago

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

twalen commented 2 months ago

I prefer solution without removing tables.