Is there a version control system for database structure changes?

后端 未结 22 1399
梦毁少年i
梦毁少年i 2020-11-30 17:53

I often run into the following problem.

I work on some changes to a project that require new tables or columns in the database. I make the database modifications and

22条回答
  •  难免孤独
    2020-11-30 18:14

    I'm a bit old-school, in that I use source files for creating the database. There are actually 2 files - project-database.sql and project-updates.sql - the first for the schema and persistant data, and the second for modifications. Of course, both are under source control.

    When the database changes, I first update the main schema in project-database.sql, then copy the relevant info to the project-updates.sql, for instance ALTER TABLE statements. I can then apply the updates to the development database, test, iterate until done well. Then, check in files, test again, and apply to production.

    Also, I usually have a table in the db - Config - such as:

    SQL

    CREATE TABLE Config
    (
        cfg_tag VARCHAR(50),
        cfg_value VARCHAR(100)
    );
    
    INSERT INTO Config(cfg_tag, cfg_value) VALUES
    ( 'db_version', '$Revision: $'),
    ( 'db_revision', '$Revision: $');
    

    Then, I add the following to the update section:

    UPDATE Config SET cfg_value='$Revision: $' WHERE cfg_tag='db_revision';
    

    The db_version only gets changed when the database is recreated, and the db_revision gives me an indication how far the db is off the baseline.

    I could keep the updates in their own separate files, but I chose to mash them all together and use cut&paste to extract relevant sections. A bit more housekeeping is in order, i.e., remove ':' from $Revision 1.1 $ to freeze them.

提交回复
热议问题