Spring Boot Flyway管理数据库版本 - josdem

栏目: Java · 发布时间: 5年前

内容简介:您知道在软件开发中使用版本控制的好处,例如让我们开始创建一个包含web和jpa依赖项的新Spring Boot项目:这是生成的build.gradle文件:

您知道在软件开发中使用版本控制的好处,例如 GitSubversion 。这次我将向您展示 Flyway 来管理数据库的版本控制,因此您可以使用 Gradle 和Spring Boot 轻松跟踪架构在所有环境中的演变。

让我们开始创建一个包含web和jpa依赖项的新Spring Boot项目:

spring init --dependencies=web,jpa --language=groovy --build=gradle spring-boot-flyway

这是生成的build.gradle文件:

buildscript {
  ext {
    springBootVersion = '1.5.12.RELEASE'
  }
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  }
}

apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
  mavenCentral()
}

dependencies {
  compile 'org.springframework.boot:spring-boot-starter-web'
  compile 'org.springframework.boot:spring-boot-starter-data-jpa'
  compile 'mysql:mysql-connector-java:5.1.34'
  compile 'org.codehaus.groovy:groovy'
  testCompile 'org.springframework.boot:spring-boot-starter-test'
}

然后让我们在gradle.properties添加Flyway插件来连接到 MySQL 数据库:

plugins {
  id "org.flywaydb.flyway" version "5.0.7"
}

flyway {
  url = 'jdbc:mysql://localhost:3306/flyway_demo'
  user = 'flywayUser'
  password = 'flywaySecret'
}

现在让我们创建第一个迁移src/main/resources/db/migration/V1__person_create.sql:

DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
);

其次,让我们添加第二个迁移src/main/resources/db/migration/V2__person_insert.sql:

INSERT INTO `person` VALUES (1, 'Jose Luis', 'De la Cruz Morales'), (2, 'Eric', 'Haddad')

让我们运行Flyway来使用gradle迁移我们的数据库:

gradle flywayMigrate -i

如果一切顺利,您应该看到以下输出:

Flyway Community Edition 5.0.7 by Boxfuse
Database: jdbc:mysql://localhost:3306/flyway_demo (MySQL 5.7)
Successfully validated 2 migrations (execution time 00:00.039s)
Current version of schema `flyway_demo`: << Empty Schema >>
Migrating schema `flyway_demo` to version 1 - person create
Migrating schema `flyway_demo` to version 2 - person insert
Successfully applied 2 migrations to schema `flyway_demo` (execution time 00:00.985s)

我们可以在gradle 按照以下方式执行依赖于bootRun任务的flywayMigrate任务:

bootRun.dependsOn rootProject.tasks['flywayMigrate']

Flyway使用VX__description.sql约定名称。我们application.properties用来指定嵌入式数据库驱动程序类,凭证和ddl(数据定义语言)策略。

spring.datasource.url=jdbc:mysql://localhost:3306/flyway_demo
spring.datasource.username=flywayUser
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none

JPA具有DDL生成功能,可以将这些功能设置为在数据库启动时运行。这是通过前两个属性控制的:

  • spring.jpa.generate-ddl (布尔值)打开和关闭功能,与供应商无关。
  • spring.jpa.hibernate.ddl-auto(枚举)是一种Hibernate功能,它以更细粒度的方式控制行为。这个特点是:create,create-drop,validate,和update。

在生产中使用Flyway版本控制时,强烈建议您使用none或仅指定此属性。

这是我们的Person实体:

@Entity
class Person {

  @Id
  @GeneratedValue(strategy=AUTO)
  Long id

  @Column(nullable = false)
  String firstname

  @Column(nullable = false)
  String lastname

}

源码下载


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Developing Large Web Applications

Developing Large Web Applications

Kyle Loudon / Yahoo Press / 2010-3-15 / USD 34.99

As web applications grow, so do the challenges. These applications need to live up to demanding performance requirements, and be reliable around the clock every day of the year. And they need to withs......一起来看看 《Developing Large Web Applications》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具