spring - 在 Spring Boot 中使用 liquibase mongodb 扩展
问题描述
尝试在 Spring Boot 中使用 liquibase-mongodb 扩展,但运行迁移对我的数据库没有影响。
在 pom 文件中添加了 liquibase-core、liquibase-mongodb 扩展和 ongo-java-driver 作为依赖项。
这是我的变更日志文件:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.0.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet id="first" author="andrei">
<ext:createCollection collectionName="myCollection">
<ext:options>
{
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "address"],
properties: {
name: {
bsonType: "string",
description: "The Name"
},
address: {
bsonType: "string",
description: "The Address"
}
}
}
},
validationAction: "warn",
validationLevel: "strict"
}
</ext:options>
</ext:createCollection>
</changeSet>
</databaseChangeLog>
我尝试注入 bean SpringLiquibase,但它需要我提供一个 DataSource,它是一个接口,而 Liquidbase 的 mongo 扩展不提供此接口的实现。
@Bean
public SpringLiquibase liquibase() {
MongoLiquibaseDatabase db = new MongoLiquibaseDatabase();
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master.yaml");
//How to do this?
// liquibase.setDataSource();
liquibase.setShouldRun(true);
return liquibase;
}
有没有人有一个使用Spring的liquidbase mongodb扩展的工作示例?
解决方案
最简单的方法可能是使用org.springframework.jdbc.datasource.SimpleDriverDataSource
and liquibase.ext.mongodb.database.MongoClientDriver
。
MongoClientDriver 是 liquibase-mongodb 扩展的一部分,并根据java.sql.Driver
Liquibase 的需要调整 MongoClient 到接口。
这应该允许您创建 SimpleDriverDataSource 并将其传递给 liquibase,例如:
SimpleDriverDataSource dataSource = new SimpleDriverDataSource(new MongoClientDriver(), YOUR_URL, YOUR_USERNAME, YOUR_PASSWORD);
liquibase.setDataSource(dataSource);
推荐阅读
- apache-spark - 基于 Databricks Spark SQL 子查询的查询抛出 TreeNodeException
- jasper-reports - 有没有办法垂直分割碧玉报告甘特图?
- android - 当来自后台时,片段行为将是什么。考虑片段在堆栈中
- python - 是否可以使用python仅更新json文件中的单个记录(当有多个记录时)?
- spring-boot - RestTemplate 的 postForObject 的模拟返回 null
- c# - WIX 设置 - API 调用失败
- sql - 如何在sql server中找到左连接不匹配的行并在其之上执行一些操作?
- javascript - 如何使用谷歌云维护的参考词汇表实现谷歌云翻译 API 来翻译前端数据?
- javascript - 更新工具提示数据而不在 Canvasjs 中重新加载整个图表
- mysql - 查询保存 JSON 的 MYSQL 长文本