简体   繁体   English

Spring Boot&Liquibase示例

[英]Spring Boot & Liquibase by Example

Spring Boot and MySQL here. Spring Boot和MySQL在这里。 Trying to get my Spring Boot app to use Liquibase for my DB migrations and see in the docs that Spring Boot has built-in support for Liquibase . 试图让我的Spring Boot应用程序使用Liquibase进行数据库迁移,并在文档中看到Spring Boot 内置了对Liquibase的支持

However after reading those docs, I'm left with several related concerns: 但是在阅读完这些文档之后,我还有几个相关的问题:

  • What is the fundamental purpose of the db/changelog/db.changelog-master.yaml file? db/changelog/db.changelog-master.yaml文件的基本用途是什么? Is it to store Liquibase configurations (that dictate how Liquibase behaves), or is that where I'm supposed to put the actual, sequential SQL changes (the " migrations ") themselves? 是存储Liquibase配置(指示Liquibase的行为),还是我应该将实际的顺序SQL更改(“ 迁移 ”)本身放在哪里?
    • Ideally I'd like to have a src/main/resources/migrations directory and store my migration changes as individual SQL files, like so: 理想情况下,我希望有一个src/main/resources/migrations目录,并将我的迁移更改存储为单独的SQL文件,如下所示:
    • src/main/resources/migrations/001-schema.sql
    • src/main/resources/migrations/002-init.sql
    • src/main/resources/migrations/003-changing-account-types.sql
    • ...etc. ...等等。 Is it possible to configure Liquibase to do this via Spring Boot? 是否可以通过Spring Boot配置Liquibase来实现这一目的?
  • When will Spring Boot run these Liquibase migrations? Spring Boot什么时候运行这些Liquibase迁移? At app startup? 在app启动时? What if the Spring Boot app actually runs on a cluster of nodes (say 5 nodes behind a load-balanced URL)? 如果Spring Boot应用程序实际上在节点集群上运行(比如负载平衡URL后面的5个节点),该怎么办? Will Spring Boot run Liquibase run 5 times, once on each node? 将Spring Boot运行Liquibase运行5次,每个节点运行一次吗? Or does it somehow sense that one node is the " master migrator ", etc.? 或者它是否意识到一个节点是“ 主迁移者 ”等?

The db/changelog/db.changelog-master.yaml file is the one executed on application startup when using default configuration. db/changelog/db.changelog-master.yaml文件是在使用默认配置时在应用程序启动时执行的文件。 In that file you can have the sequential SQL changes as well as inclusions to other files. 在该文件中,您可以进行顺序SQL更改以及对其他文件的包含。 For example the file could contain inclusions like this (xml syntax) 例如,该文件可能包含这样的包含(xml语法)

<include file="migrations/001-schema.sql"/> 
<include file="migrations/002-init.sql"/> 
<include file="migrations/003-changing-account-types.sql"/> 

and you would have the configuration you wanted. 你会得到你想要的配置。

About your second question - yes, they are applied at startup. 关于你的第二个问题 - 是的,它们是在启动时应用的。 If it runs on a cluster of nodes, they will each check the status and apply the changes to database if they aren't already applied(the databasechangelog and databasechangelock tables are used for that and they make sure the changes are only applied once) 如果它在节点集群上运行,它们将分别检查状态并将更改应用于数据库(如果尚未应用)(数据库交换日志和数据库交换表用于此目的,并确保更改仅应用一次)

example for yaml syntax yaml语法的示例

databaseChangeLog:
- include:
    file: migrations/001-schema.sql
- include:
    file: migrations/002-init.sql
- include:
    file: migrations/003-changing-account-types.sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM