Wednesday, March 28, 2018

data manipulation code in custom migrations

Hello,


I recently ran into a problem with a custom migration containing code manipulating the data. 


The simplified scenario is:

  • a given model has been defined with its set of fields. So far so good.
  • later on, a new field has been added, intended to contain a required read-only value which is automatically generated when creating new instances
  • to fulfill the required constraint for existing instances, a custom migration has been added (let's call it M1 for reference) to generate the missing values. No problem, all (migrations, application,...) is working fine.

Later on, a new field has been added to the model (let's call it F). The associated automatic migration (let's call it M2) and the application still run fine.


A problem now occurs when running unit tests. At the time the temporary test database creation process runs, in hangs on migration M1. The reason is that when it runs, it loads the current model class definition which includes the field F. But this field does not exist yet in the database (since created later by the migration M2). Shortly said, the problem comes from the model and the database not being in sync when running M1.


Since I imagine that this situation is not uncommon, there should be an appropriate method to be applied in this case. Could anybody give some hints about the right way to proceed please ? Thanks in advance.


Best regards


Eric

No comments:

Post a Comment