使用Flyway从Java代码创建新模式
我们在代码库中引入了Flyway。以前,我们将Postgres函数存储在公共模式中,并使用它来复制租户模式,以创建与租户模式具有相同结构的新模式。回购代码如下:使用Flyway从Java代码创建新模式,java,spring-boot,flyway,Java,Spring Boot,Flyway,我们在代码库中引入了Flyway。以前,我们将Postgres函数存储在公共模式中,并使用它来复制租户模式,以创建与租户模式具有相同结构的新模式。回购代码如下: @Repository public interface TenantRepository extends JpaRepository<Tenant, UUID> { @Query(nativeQuery = true, value = "SELECT clone_schema(:defaultS
@Repository
public interface TenantRepository extends JpaRepository<Tenant, UUID> {
@Query(nativeQuery = true, value = "SELECT clone_schema(:defaultSchema,:newSchema,:isCopyData);")
String callCloneSchema(@Param("defaultSchema") String defaultSchema, @Param("newSchema") String newSchema,@Param("isCopyData") boolean isCopyData);
}
@存储库
公共接口租户存储扩展了JpaRepository{
@查询(nativeQuery=true,value=“选择克隆模式(:defaultSchema,:newSchema,:isCopyData);”)
字符串callCloneSchema(@Param(“defaultSchema”)字符串defaultSchema,@Param(“newSchema”)字符串newSchema,@Param(“isCopyData”)布尔isCopyData);
}
我想删除这些函数,并想使用Flyway创建一个新的模式。Flyway是否提供了这种可能性?以下是一些用于手动触发具有单个模式的Flyway迁移的步骤: 1。禁用“自动迁移” 默认情况下,Spring boot希望自动运行Flyway SQL脚本。因此,您必须禁用此“自动迁移”(请参阅)。在这里,这是在“Spring Boot主类”中完成的: 2。手动迁移 要使用不同的模式自动执行迁移,这将是一个解决方案:
- 注入(默认)
实例Flyway
- 复制配置,但覆盖要使用的架构
- 触发迁移
src/main/resources/db/migration/V#u#u#uxyz.SQL
下)
@控制器
公共类控制器{
@自动连线
跑道;
@GetMapping(“foo”)
公共响应属性foo(@RequestParam(“schema”)字符串schema){
configure()
//应用/使用默认(弹簧)飞道配置
.configuration(flyway.getConfiguration())
//使用传递的架构
.schema(schema)
.defaultSchema(模式)
//获取一个Flyway实例
.load()
//运行迁移
.migrate();
返回ResponseEntity.noContent().build();
}
}
这个答案有用吗@如果没有,我想使用Flyway创建一个新的模式运行时
@SpringBootApplication
public class FooApplication {
public static void main(String[] args) {
SpringApplication.run(FooApplication.class, args);
}
@Bean
public FlywayMigrationStrategy flywayMigrationStrategy() {
return flyway -> {
// do nothing
};
}
}
@Controller
public class FooController {
@Autowired
Flyway flyway;
@GetMapping("foo")
public ResponseEntity<?> foo(@RequestParam("schema") String schema) {
Flyway.configure()
// apply/use the default (Spring) flyway configiration
.configuration(flyway.getConfiguration())
// use the passed schema
.schemas(schema)
.defaultSchema(schema)
// get a Flyway instance
.load()
// run the migration
.migrate();
return ResponseEntity.noContent().build();
}
}