标签:har creat nbsp pre put ati family 看到了 扫描
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0</version> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>4.2.0</version> </dependency>
<plugin> <groupId>org.flywaydb</groupId> <artifactId>flyway-maven-plugin</artifactId> </plugin>
查看了很多文档,然后在application.properties写了下面的配置
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@192.168.1.11:1521/orcl spring.datasource.username=comm spring.datasource.password=comm # Whether to enable flyway. spring.flyway.enabled=true spring.flyway.baseline-on-migrate=false # The locations of migrations scripts. spring.flyway.locations=classpath:db/migration spring.flyway.clean-on-validation-error=false
@EnableTransactionManagement public class MyApplication { private static Logger logger = LoggerFactory.getLogger(MyApplication.class); public static void main(String[] args) { try { SpringApplication springApplication = new SpringApplication(MyApplication.class); springApplication.run(args); logger.info("----------------------flyway started successfully-----------------------"); } catch (Exception e) { logger.error(e.getMessage()); } } }
启动之后报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘flywayInitializer‘ defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "COMM" without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.
这是因为没有初始化那个COMM的schema,然后我就把配置文件的
spring.flyway.baseline-on-migrate= false 改成了true
重新启动就成功了。
我一直找不到,comm自动补全里有这张表,差点就疯了,然后看到别人的博文里说:
由于schema_version是记录数据库版本信息的,因此flyway对其做了保护策略,如果要查看这张表,则必须给表名加双引号,如:select * from "schema_version"
我终于看到了希望 : )
能在表里看到一条初始化的数据。
1. 我先放入了V1.0.0__init.sql 和 V2.0.0__init.sql,第一个是新建表test的,第二个是往test里面插入数据的,再次启动报错(test表不存在)了,那是因为schema_version表中初始化的那一条是1,然后就从V2执行了。
然后删掉了version表中的第二条数据,将sql文件重新起了名字
V1.0.1__init.sql: create table test ( id varchar2(20) not null, name VARCHAR2(20) );
V1.0.2__init.sql: insert into comm.test(id, name) values (‘1‘,‘张三‘); insert into comm.test(id, name) values (‘2‘,‘李四‘);
执行成功,表也创建成功了,开心ing。
1. 新建flyway的执行类,我比参考文档多了36行的(把上面version表还有test表都删掉了,执行会报错,没有COMM的schema)
1 package org.ssh.hip.flyway.controller; 2 3 4 import java.util.HashMap; 5 import java.util.Map; 6 7 import javax.sql.DataSource; 8 9 import org.flywaydb.core.Flyway; 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 import org.springframework.beans.factory.annotation.Autowired; 13 import org.springframework.stereotype.Controller; 14 import org.springframework.web.bind.annotation.RequestMapping; 15 import org.springframework.web.bind.annotation.ResponseBody; 16 17 18 @Controller 19 @RequestMapping("/flyway") 20 public class FlywayConfig { 21 22 @Autowired 23 private DataSource dataSource; 24 25 private Logger logger = LoggerFactory.getLogger(this.getClass()); 26 27 28 @RequestMapping(value = "/migrate") 29 @ResponseBody 30 public Map<String, String> migrate(){ 31 Map<String, String> res = new HashMap<String, String>(); 32 33 Flyway flyway = new Flyway(); 34 35 try { 36 flyway.setBaselineOnMigrate(true);// 为了创建schema 37 flyway.setDataSource(dataSource); 38 // 设置flyway扫描sql升级脚本、java升级脚本的目录路径或包路径(表示是src/main/resources/flyway下面,前缀默认为src/main/resources,因为这个路径默认在classpath下面) 39 flyway.setLocations("db/migration"); 40 // 设置sql脚本文件的编码 41 flyway.setEncoding("UTF-8"); 42 flyway.setOutOfOrder(true); 43 44 flyway.migrate(); 45 46 // } catch (FlywayException e) { 47 } catch (Exception e) { 48 49 flyway.repair(); 50 51 logger.error("Flyway配置加载出错",e); 52 res.put("code", "-1"); 53 res.put("message", "Flyway配置加载出错" + e.getMessage()); 54 } 55 res.put("code", "0"); 56 res.put("message","success"); 57 return res; 58 } 59 }
2. 修改启动类(将flyway配置从springboot排除,避免springboot自动配置)
1 import org.slf4j.Logger; 2 import org.slf4j.LoggerFactory; 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; 6 import org.springframework.transaction.annotation.EnableTransactionManagement; 7 import springfox.documentation.swagger2.annotations.EnableSwagger2; 8 9 @EnableSwagger2 10 @SpringBootApplication(exclude = { FlywayAutoConfiguration.class }) 11 @EnableTransactionManagement 12 public class MyApplication { 13 14 private static Logger logger = LoggerFactory.getLogger(MyApplication.class); 15 16 public static void main(String[] args) { 17 try { 18 SpringApplication springApplication = new SpringApplication(MyApplication.class); 19 springApplication.run(args); 20 logger.info("----------------------flyway started successfully-----------------------"); 21 } catch (Exception e) { 22 logger.error(e.getMessage()); 23 } 24 } 25 26 }
然后欧了~~
参考文档: 1.flyway,maven,oracle集成记录 https://blog.csdn.net/citeng1329/article/details/100288990 2.运用flyway实现数据库版本自动更新控制实录 https://blog.csdn.net/u014255803/article/details/79107708/ 3.SpringBoot项目集成Flyway配置执行顺序问题解决方法 https://blog.csdn.net/u012134942/article/details/97002288 4.为什么springboot会在启动的时候自动运行Flyway DB? (这就是把上一个问题里面exclude的那个启动类代码贴出来了,直接ctrl鼠标左键点进去就可以看到) https://www.jianshu.com/p/b05a36f91ebc
标签:har creat nbsp pre put ati family 看到了 扫描
原文地址:https://www.cnblogs.com/utomboy/p/12504571.html