Using Flyway to load data conditionally per environment

后端 未结 4 1693
感情败类
感情败类 2020-12-24 13:36

Does flyway support conditional execution of a script, per environment?

For example, if I have test data, can I create a test data script folder thats only loaded i

4条回答
  •  青春惊慌失措
    2020-12-24 14:30

    Maven profiles did not give me the flexibility I wanted. I came up with a strategy that uses ant to merge files. This allows me to have common scripts and then included additional data depending on the deploy type eg. prod, dev.

    This is my project structure:

    ├── build.xml
    ├── database.properties
    ├── database.properties.template
    ├── lib
    │   └── ant-contrib-1.0b3.jar
    ├── pom.xml
    └── sql
        ├── common
        │   ├── V1.0__.sql
        │   ├── V1.2.1__.sql
        │   └── V1.3__.sql
        ├── dev
        │   ├── V1.0__.sql
        │   └── V1.3__.sql
        └── prod
            └── V1.0__.sql
    

    database.properties.template file in the root folder, before running this has to manually be copied to database.properties and the username and password can be entered. database.properties should be ignored by VCS so that passwords don't end up in the repo.

    deployType scripts are merged into the src/main/resources/db/migrate directory, here is the ant script that does that, note that scripts that are to be merged have the same name:

    
    
        
        
    
        
    
        
          
            
          
        
    
        
            
            
    
            
            
                
                    
                
            
    
            
            
                
                    
                
            
        
    
        
            
            
            
    
            
                
                
            
        
    
    

    This ant file is executed by maven, here is the pom config:

    
    
    
        4.0.0
    
        data
        data
        1.1-SNAPSHOT
        pom
        data
    
        
            filesystem:${basedir}/src/main/resources/
        
        
            
                
                    org.apache.maven.plugins
                    maven-antrun-plugin
                    1.7
                    
                        
                            mergeScripts
                            validate
                            false
                            
                                
                                    
                                
                            
                            
                                run
                            
                        
                    
                
                
                    org.flywaydb
                    flyway-maven-plugin
                    3.2.1
                    
                        
                            common
                        
                        database.properties
                        flyway
    filesystem:${basedir}/src/main/resources/db/migration
    mysql mysql-connector-java 5.1.36

    Should you require different data for different client distributions you could add dist directories into the sql directory and they could contain deployType sub directories.

    Add the dist property to the database.properties.template file and then modify the append target in the build.xml to look like this:

    
        
        
        
    
        
            
            
                
                
        
    
    

提交回复
热议问题