Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

obsolete enum member who is stored in Database

I have an enum in an entity of the EF6 and it is stored as a byte in the database when saving the enum.

Now I want to mark a value of the Enum as Obsolete, but there will still be data using this value. It just is not used in the code.

Technically, I would like to delete this value in the code, but what would you do with the old data?

Migrate?

like image 258
Christian Alt-Wibbing Avatar asked Nov 23 '25 00:11

Christian Alt-Wibbing


1 Answers

The good practice is to follow these steps:

  • data migration: replace the obsolete value into your database with the correct one.
  • cleaning the code: remove the enum value because it is no longer neeeded.

If data migration can't be done because old value is still kept into the database, so I will follow these steps:

  • each property setter or method that set a field from your enum must add some defensive code. When setting with the obsolete enum value you throw an exception InvalidOperationException with a explicit message.
  • in the enum definition, I will put a XML comments with a <remark> that will help developers to know about what's going on.
  • optional step: going further by adding custom code analyzer to track the use of that value.

Notice I don't decorate the enum value with [Obsolete] attribute because this attribute means two things:

  • the code is obsolete at version X.
  • the code will be removed in a version after X: version Y.

Those two things let developers that use version X to clean their actual code base (remove all use of the obsolete enum value) and get ready for version Y (no changes will be needed because all the job is done when using version X). You can't use [Obsolete] attribute because you said that old data will still exist into the database so you may need to display somewhere and you're not following the two things I said about [Obsolete] attribute.

like image 160
CodeNotFound Avatar answered Nov 24 '25 15:11

CodeNotFound



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!