Combine Hibernate's automatic schema creation and database versioning

后端 未结 1 898
情歌与酒
情歌与酒 2021-02-08 18:29

I have an application where Hibernate creates all my table schemata whenever the application is run on a machine for the first time. This works nicely.

Now I was howeve

相关标签:
1条回答
  • 2021-02-08 18:45

    We had the same problem with our Java/Hibernate project, and did not want any code duplication effort. Hibernate "update" feature is not reliable at all, LiquidBase is better but not 100% fool proof either. In the end we developed a simple script to manage the following process:

    1. The "current" database schema is always generated by Hibernate, against a DEV database directly.
    2. A "previous" database schema is generated by a series of LiquiBase change sets.
    3. Every time a migration is needed, a LiquiBase "diff" function is invoked between the "previous" and "current" databases (two actual databases, yes, more reliably that way), generating a new LiquiBase change set.
    4. This change set needs to be manually reviewed. All change sets are kept in source code control.
    5. The PRODUCTION database has its schema generated by applying all the LiquiBase change sets.

    A key command in our script reads like the following:

    ${LIQB_COMMAND} ${PREV_DB_OPTIONS} --changeLogFile=${LIQB_CHGLOG_FILE_NEW}  \
        diffChangeLog \
                    --referenceUsername=${DEV_DB_USER} \
                    --referencePassword=${DEV_DB_PWD} \
                    --referenceDriver=com.mysql.jdbc.Driver \
                    --referenceUrl=${DEV_DB_URL}
    

    This way, our migration process is quite reliable and you don't write the schema code twice. There is a manual review of the generated change set in XML, but most of the time there is no problem, and it sure is much easier than manually writing the schema change operations.

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