What's the best way to deprecate a column in a database schema?

后端 未结 2 1014
闹比i
闹比i 2021-02-05 21:30

After reading through many of the questions here about DB schema migration and versions, I\'ve come up with a scheme to safely update DB schema during our update process. The b

相关标签:
2条回答
  • 2021-02-05 22:32

    "Deprecate" usually means (to me at least) that something is marked for removal at some future date, should not used by new functionality and will be removed/changed in existing code.

    I don't know of a good way to "mark" a deprecated column, other than to rename it, which is likely to break things! Even if such a facility existed, how much use would it really be?

    So do you really want to deprecate or remove? From the content of your question, I'm guessing the latter.

    I have the nasty feeling that you may be in one of those "if I wanted to get to there I wouldn't start from here" situations. However, here are some ideas that spring to mind:

    1. Read Recipes for Continuous Database Integration which seems to address much of your problem area

    2. Drop the column explicitly. In MySQL 5.0 (and even earlier?) the facility exists as part of DDL: see the ALTER TABLE syntax.

    3. Look at how ActiveRecord::Migration works in Ruby. A migration can include the "remove_column" directive, which will deal with the problem in a platform-appropriate way. It definitely works with MySQL, from personal experience.

    4. Run a script against your export to remove the column from the INSERT statements, both column and values lists. Probably quite viable if your DB is fairly small, which I'm guessing it must be if you export and re-import it as described.

    0 讨论(0)
  • 2021-02-05 22:36

    Maybe you should refactor to use views over your tables, where the views never include the deprocated columns.

    0 讨论(0)
提交回复
热议问题