跳至主要內容

Liquibase 数据库版本管理

大约 2 分钟

Liquibase 数据库版本管理

Liquibase vs Flyway

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>liquibase-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core -->
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>4.17.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/liquibase
spring.datasource.username=root
spring.datasource.password=*c_c>CLos7cf
spring.liquibase.change-log=classpath:/db/changelog/changelog.json

src/main/resources/db/changelog/changelog.json

{
  "databaseChangeLog": [
    {
      "includeAll": {
        "path": "db/changelog/sql/"
      }
    }
  ]
}

src/main/resources/db/changelog/sql/00000_ddl.sql

create table tb_device(
    device_id bigint not null auto_increment,
    device_type int default null,
    primary key (device_id)
);

src/main/resources/db/changelog/sql/00010_dml.sql

create table tb_device_0 like tb_device;

运行结果

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.5)

2022-12-04 22:00:42.826  INFO 15828 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2022-12-04 22:00:43.439  INFO 15828 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-12-04 22:00:44.431  INFO 15828 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-12-04 22:00:44.584  INFO 15828 --- [           main] liquibase.lockservice                    : Successfully acquired change log lock
2022-12-04 22:00:44.639  INFO 15828 --- [           main] liquibase.changelog                      : Reading resource: db/changelog/sql/00000_ddl.sql
2022-12-04 22:00:44.646  INFO 15828 --- [           main] liquibase.changelog                      : Reading resource: db/changelog/sql/00010_dml.sql
2022-12-04 22:00:44.722  INFO 15828 --- [           main] liquibase.changelog                      : Reading from liquibase.DATABASECHANGELOG
Running Changeset: db/changelog/sql/00010_dml.sql::raw::includeAll
2022-12-04 22:00:44.774  INFO 15828 --- [           main] liquibase.changelog                      : Custom SQL executed
2022-12-04 22:00:44.775  INFO 15828 --- [           main] liquibase.changelog                      : ChangeSet db/changelog/sql/00010_dml.sql::raw::includeAll ran successfully in 18ms
2022-12-04 22:00:44.793  INFO 15828 --- [           main] liquibase.lockservice                    : Successfully released change log lock
2022-12-04 22:00:44.863  INFO 15828 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.318 seconds (JVM running for 2.642)
2022-12-04 22:00:44.871  INFO 15828 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-12-04 22:00:44.875  INFO 15828 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

参考链接

(全文完)