Can Flyway or Liquibase generate an update script instead of updating the database directly?

前端 未结 5 1054
难免孤独
难免孤独 2020-12-31 04:14

First, a little background. I have a set of Java applications, some based on JPA, some not. To create my databases I am currently using Hibernates schema export to generate

相关标签:
5条回答
  • 2020-12-31 04:29

    What you want is a schema diff tool. I remember hearing that TOAD has a rather powerful one. Hibernate will also try to generate schema update scripts based on the entities it detects and the database metadata.

    However what you need is... to not do that and instead use Flyway to make all your database changes. That is you should turn of Hibernates automatic schema updates and write the schema updates yourself going forward. Every time you want to make a change to the database you have to write a schema update.

    Some people capture the SQL output of how hibernates schema update as a way of getting automated schema evolution updates. The problem is that hibernate is typically wrong especially if add a @NotNull column.

    Also in terms of your admin I believe Flyway can output based on its schema_version table and the SQL/Java migration scripts the SQL output it will run thus your DBA could run it outside of Flyway (If it doesn't this would be an easy feature to add).

    0 讨论(0)
  • 2020-12-31 04:32

    Liquibase handles it quite fine. It looks at your database at the current state, finds unapplied changesets and generates an SQL script with update command in sql output mode.

    Using a proper database migration tool instead of Hibernate generator is the way to go in any case, sooner or later you'll end up with a situation that Hibernate does not support. For us it was dropping an unique index and replacing it with another. You can also enable hibernate.hbm2ddl.auto=validate to feel safe about the compatibility between database structure and entity beans.

    0 讨论(0)
  • 2020-12-31 04:44

    Yes, generating a SQL script is a built-in feature for Liquibase. Another area where Liquibase has an advantage over flyway is it's ability to migrate database code changes (packages, procedures, and functions). I use flyway but I'd love to have those Liquibase features available in flyway.

    0 讨论(0)
  • 2020-12-31 04:49

    The documentation for Flyway command line tool says that dryRunOutput is a Flyway Pro feature that outputs SQL statements to a specified file.

    0 讨论(0)
  • 2020-12-31 04:52

    We had a similar problem when I worked at a consultancy (Intelliware) so the devs there put together some code and pushed it up to GitHub.

    We tried unsuccessfully to get it included into the Flyway core repo.

    https://github.com/Intelliware/flyway-script-generator

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