Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't import plugin models in main migrations #25

Merged
merged 1 commit into from
Sep 11, 2017

Conversation

jmbowman
Copy link

@jmbowman jmbowman commented Sep 7, 2017

While working on the switch from nose to pytest in edx-platform, I discovered that we import models from some of the wiki plugins in the main wiki app's migrations. We don't run migrations before starting tests, but we do have a test which verifies that we don't have model changes which aren't reflected in our migrations. And we don't actually use those plugins, so those models aren't found when preparing the database for a test run. So depending on exactly when this test is run, it can break subsequent tests which delete users because after importing those model files, Django now expects there to be tables for models in them which have foreign keys to user records.

Fixing this by doing the following:

  • Edit the problematic migration to no longer import those model files, replacing the "upload_to" attributes that depended on functions from them with static strings instead.
  • Add a new migration which deletes the tables for the plugin models, so makemigrations won't care about the fact that those field attributes changed.
  • Add migrations to the plugins which are very close to what upstream is currently using, on the off chance we actually do start using these plugins someday.
  • Borrowed some Django app configuration code from upstream to assign appropriate app labels to the plugins, as used in relationships in the migration files.

Copy link

@doctoryes doctoryes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of questions.

model_name='imagerevision',
name='revisionpluginrevision_ptr',
),
migrations.DeleteModel(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - do Django's automatically-generated migrations remove each field like this before deleting models?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these were just the foreign keys, removed separately to avoid referential integrity problems when dropping the tables. As I noted in my other reply, this is a fairly tangled pair of models.

field=models.OneToOneField(related_name='current_set', null=True, to='wiki_attachments.AttachmentRevision', blank=True, help_text='The revision of this attachment currently in use (on all articles using the attachment)', verbose_name='current revision'),
preserve_default=True,
),
]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the separateAddFields after the CreateModels? Is this just several migrations from upstream squashed together?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what was in our original 001_initial migration for the wiki app, copied over verbatim. This was a particularly tricky case because AttachmentRevision has a foreign key to itself, plus it and Attachment have foreign keys to each other. I think Django just erred on the side of caution and added each of the foreign keys individually after creating the tables.

Copy link

@bmedx bmedx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@jmbowman jmbowman force-pushed the jmbowman/migrations_cleanup branch from a8151e2 to 65514af Compare September 11, 2017 16:08
@jmbowman jmbowman merged commit 2b4da25 into edx_release Sep 11, 2017
@jmbowman jmbowman deleted the jmbowman/migrations_cleanup branch September 11, 2017 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants