转载

Spring Boot Liquibase基础教程 - josdem

Liquibase是数据库的源代码控制。

更改日志Changelog:开发人员将基于文本的文件中的数据库更改日志存储在其本地开发计算机上,并将其应用于本地数据库。

更改集ChangeSet:由“author”和“id”属性唯一标识。每个变更集通常都包含一个更改,该变更描述了要应用于数据库的更改/重构。

这次我将向您展示如何将Liquibase添加到Spring Boot项目中。首先,您需要在build.gradle文件中添加Liquibase依赖项:

compile "org.liquibase:liquibase-core:3.5.3"

执行此命令以生成主更改日志:

liquibase --driver=com.mysql.jdbc.Driver --classpath=/${CONNECTOR_PATH}/mysql-connector-java-5.1.34.jar --changeLogFile=${PROJECT_PATH}/src/main/resources/db/changelog/db.changelog-master.yaml --url=jdbc:mysql://localhost:3306/db_name --username=username --password=password generateChangeLog

如果您使用的是Windowns计算机:

liquibase --driver=com.mysql.jdbc.Driver --classpath=C:/${CONNECTOR_PATH}/mysql-connector-java-5.1.34.jar --changeLogFile=C:/${PROJECT_PATH}/db/changelog/db.changelog-master.yaml --url=jdbc:mysql://localhost:3306/db_name --username=username--password=password generateChangeLog

上个命令将生成一个db.changelog-master.yaml文件。

databaseChangeLog:
- changeSet:
    id: 1487128064865-1
    author: josdem (generated)
    changes:
    - createTable:
        columns:
        - column:
            autoIncrement: true
            constraints:
              primaryKey: true
            name: id
            type: BIGINT
        - column:
            constraints:
              nullable: false
            name: date_created
            type: datetime(6)
        - column:
            constraints:
              nullable: false
            name: email
            type: VARCHAR(255)
        - column:
            constraints:
              nullable: false
            name: status
            type: INT
        - column:
            constraints:
              nullable: false
            name: token
            type: VARCHAR(255)
        tableName: registration_code
- changeSet:
    id: 1487128064865-2
    author: josdem (generated)
    changes:
    - createTable:
        columns:
        - column:
            autoIncrement: true
            constraints:
              primaryKey: true
            name: id
            type: BIGINT
        - column:
            constraints:
              nullable: false
            name: account_non_expired
            type: BIT(1)
        - column:
            constraints:
              nullable: false
            name: account_non_locked
            type: BIT(1)
        - column:
            constraints:
              nullable: false
            name: credentials_non_expired
            type: BIT(1)
        - column:
            constraints:
              nullable: false
            name: date_created
            type: datetime(6)
        - column:
            name: email
            type: VARCHAR(255)
        - column:
            constraints:
              nullable: false
            name: enabled
            type: BIT(1)
        - column:
            name: firstname
            type: VARCHAR(255)
        - column:
            name: lastname
            type: VARCHAR(255)
        - column:
            constraints:
              nullable: false
            name: password
            type: VARCHAR(255)
        - column:
            constraints:
              nullable: false
            name: role
            type: VARCHAR(255)
        - column:
            constraints:
              nullable: false
            name: username
            type: VARCHAR(255)
        tableName: user
- changeSet:
    id: 1487128064865-3
    author: josdem (generated)
    changes:
    - addUniqueConstraint:
        columnNames: username
        constraintName: UK_sb8bbouer5wak8vyiiy4pf2bx
        tableName: user

这是我目前使用Liquibase创建YAML文件的实体:

@Entity
class User implements Serializable {

  @Id
  @GeneratedValue(strategy=AUTO)
  Long id
  @Column(unique = true, nullable = false)
  String username
  @Column(nullable = false)
  String password
  @Column(nullable = true)
  String firstname
  @Column(nullable = true)
  String lastname
  @Column(nullable = true)
  String email
  @Column(nullable = false)
  @Enumerated(STRING)
  Role role

  @Column(nullable = false)
  Boolean enabled = false
  @Column(nullable = false)
  Boolean accountNonExpired = true
  @Column(nullable = false)
  Boolean credentialsNonExpired = true
  @Column(nullable = false)
  Boolean accountNonLocked = true
  @Column(nullable = false)
  Date dateCreated = new Date()

}

RegistrationCode.groovy

@Entity
class RegistrationCode {

  @Id
  @GeneratedValue(strategy=AUTO)
  Long id
  @Column(nullable = false)
  String email
  @Column(nullable = false)
  Date dateCreated = new Date()
  @Column(nullable = false)
  String token = UUID.randomUUID().toString().replaceAll('-','')
  @Column(nullable = false)
  RegistrationCodeStatus status = RegistrationCodeStatus.VALID

  Boolean isValid(){
    status == RegistrationCodeStatus.VALID ? true : false
  }
}

Role.groovy

enum Role {
  USER,ADMIN
}

就是这样,当您启动应用程序时,Liquibase将执行主更改日志并为您创建数据库架构。

源码

原文  https://www.jdon.com/51496
正文到此结束
Loading...