How to create a database with flyway?

前端 未结 5 930
执念已碎
执念已碎 2020-12-25 11:55

Question: Is it possible to create a new DB in a migration script and then connect to it? How?

My Scenario: I\'m trying to use flyw

5条回答
  •  清酒与你
    2020-12-25 12:52

    Here is a workaround that worked for me (assuming the use of the Maven plugin):

    Configure the plugin with two executions. The first execution creates the database. The second execution migrates an existing database.

        
            org.flywaydb
            flyway-maven-plugin
            ${flyway.version}
            
                
                    create-db
                    
                        migrate
                    
                    
                        org.postgresql.Driver
                        jdbc:postgresql://database-server/
                        postgres
                        password
                        
                            MyDatabase
                        
                        
                            com/foo/bar/database/create
                        
                    
                
                
                    migrate-db
                    
                        migrate
                    
                    
                        org.postgresql.Driver
                        jdbc:postgresql://database-server/MyDatabase
                        postgres
                        password
                        
                            com/foo/bar/database/migrate
                        
                    
                
            
            
                
                    org.postgresql
                    postgresql
                    ${postgresql.version}
                
            
        
    

    Then add V1__Created_database.sql to the com/foo/bar/database/create directory. This file contains:

    CREATE DATABASE ${DATABASE.NAME}

提交回复
热议问题