cofoundry icon indicating copy to clipboard operation
cofoundry copied to clipboard

How to update existing data when data models are modified

Open dumboster opened this issue 6 years ago • 2 comments

Hello, I want to update data in DB when I modify the definition of data models : for example, if I rename a property in a data model of a content section, I don't want to loose data in this property. I saw that I could use Auto-Update, but I would like to know if it is the recommended solution. Thanks

dumboster avatar Oct 18 '18 09:10 dumboster

When you update a field in you c# model file the data will continue to exist in the database record until you update the entity, at which point the json data will be re-serialized and overwritten.

The best way to batch update your model data in the database will be to run a sql query using the built-in json functions in sqlserver, which are available in 2016+ or SqlAzure.

To run that script you can either do that manually or use the Cofoundry auto-update as you have suggested. There's an example of using the auto-updater in the SPA Site sample

I've pondered about whether we could make this easier by adding tools to help migrate model data, but any application-side helpers would suffer from poor performance with bulk oprations if large numbers of data models neede to be updated.

I'll leave this issue open and give it some thought further down the line:

  • Add documentation on migrating data model data and encourage developers to use the SQL tools
  • Investigate whether we can add a migration tool that allows you to intercept json deserialization for the data model and add custom mapping - this would avoid having to bulk update the data but might make your codebase more complicated
  • Perhaps we can add auto-update helpers that can do simple narrow-scoped tasks like renaming fields that use the db-level json functions. Migration tasks like changing data types or manipulating data values would be out of scope and require raw SQL scripts.

HeyJoel avatar Oct 18 '18 10:10 HeyJoel

Thank you. Your response matches my thoughts.

dumboster avatar Oct 18 '18 12:10 dumboster