7

This problem confuses me the most when thinking about using feature toggles in applications. Most of the features require some changes in database. How then feature flag can be implemented to be able revert database changes smoothly along with turning on/off feature toggle ? Especially when some database migration tools are used like flyway or liquibase.

Let's use some example. For instance I have simple table Playlist with Id, Name columns. I want to add feature of adding description to Playlist in application. DB table needs to be changed. New column has been added. Column value is not mandatory. Simple so far. After some time feature is going to be turned off from production. Solution is quite easy but messy - we can leave column in db and don't use it in a code anymore. Migration tools works incrementally so i don't see any option to get back to previous db state when i turn off feature toggle (or am I wrong ?). Harder example is when field needs to be mandatory. Then i can't just leave that field in db. It will be backward incompatible. So what's then ? How to process this situation ? I think it's quite common situation.

Additionally even with slight changes on db, my application model for that class will change too. (assuming usage of some ORM). This is not as easy as replace implementation with strategy pattern. Unless you provide a lot of abstraction to your ORM model usage. So adding such a feature flag seems to be very complicated. Can anyone help me understand it how it's possible to use feature toogles then ? Or maybe someone has full example to show ? Preferred with Java

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Artur Skrzydło
  • 995
  • 11
  • 32

1 Answers1

2

Feature toggles are a way of changing behaviour without changing code - which means they are also not associated with database schema changes. Turning off a toggle should do something reversible like hiding a field from the UI, not something permanent like dropping a database column. With that in mind, your example would look like:

  1. Ship v1 of Playlist
  2. In v2, add UI for descriptions but disabled by feature flag. Migrate database to add new description column. Ship.
  3. Enable feature flag. Users can now use description
  4. Disable feature flag. Users cannot use description, but it's still in application and db
  5. In v3, remove dormant UI elements and logic. Migrate database to drop unwanted description column. Ship.
Julia Hayward
  • 1,458
  • 11
  • 14
  • Thanks for your answer. It make sense, but still I don't know what to do in situation when I add field which is mandatory. Then it's backward incompatible and I can't use it in step 2. Also in my db mapping class I need to add it, then upper layers methods needs to have an option to create Playlist with description. In MVC controller is also affected because I need to have different endpoint for this (maybe some versioning would be nice here). As i see it's a lot of changes which are tough to switch off or on. – Artur Skrzydło Dec 10 '19 at 19:58
  • For when the description is mandatory maybe you could add a default in your db (empty string etc) – Stefan Feb 09 '21 at 13:33