从Java代码使用Flyway创建新架构 [英] Create new schema using Flyway from Java code

查看:121
本文介绍了从Java代码使用Flyway创建新架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经在代码库中引入了Flyway.以前,我们在公共模式中存储了Postgres函数,并使用该函数复制租户模式以创建具有与租户模式相同结构的新模式.回购代码如下:

We have introduced Flyway inside the codebase. Previously, we have the Postgres function stored in the public schema and we use that to replicate the tenant schema to create a new schema that has the same structure as the tenant schema. The repo code is as follows:

@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);
}

我想删除这些功能,并希望使用Flyway创建新的架构.Flyway是否提供这种可能性?

I would like to delete these functions and want to create a new schema using the Flyway. Does Flyway offer such a possibility?

推荐答案

以下是用于通过个别模式手动触发Flyway迁移的一些步骤:

Here are some steps used to manually trigger Flyway migration with individual schemas:

1.禁用自动迁移"

默认情况下,Spring Boot希望自动运行Flyway SQL脚本.因此,您必须禁用此自动迁移"功能(请参阅此博客中的 4.3.).在这里,这是在"Spring Boot主类"中完成的:

By default, Spring boot would like to automatically run the Flyway SQL scripts. So you have to disable this "auto migration" (see 4.3. in this Blog). Here, this is done in the "Spring Boot main class":

@SpringBootApplication                                        
public class FooApplication {                                 
                                                              
    public static void main(String[] args) {                  
        SpringApplication.run(FooApplication.class, args);    
    }                                                         
                                                              
    @Bean                                                     
    public FlywayMigrationStrategy flywayMigrationStrategy() {
        return flyway -> {                                    
            // do nothing                                     
        };                                                    
    }                                                         
                                                              
}                                                             

2.手动迁移

要使用不同的模式自动进行迁移,这将是一种解决方案:

To automatically do migration using different schemas, this would be one solution:

  • 注入(默认) Flyway 实例
  • 复制配置,但覆盖要使用的架构
  • 触发迁移

示例代码:通过GET请求触发迁移(SQL文件位于 src/main/resources/db/migration/V#_#_#___ xyz.sql 下)

@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();                        
    }                                                                     
                                                                          
}                                                                         

这篇关于从Java代码使用Flyway创建新架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆