How to create a database with flyway?

前端 未结 5 919
执念已碎
执念已碎 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:43

    I dont know if this is even possible to do in flyway.

    Flyway is intended to connect to an already existing database (whether it is empty or not). It also would be a good practice to keep your database creation separate from your database migrations.

    0 讨论(0)
  • 2020-12-25 12:48

    Flyway always operates within the database used in the jdbc connection string.

    Once connected, all scripts run within a transaction. As CREATE DATABASE is not supported within transactions, you will not be able to accomplish what you want.

    What you can do however, is create a schema instead. Flyway will even do this for you, if you point it at a non-existing one.

    0 讨论(0)
  • 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.

        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>${flyway.version}</version>
            <executions>
                <execution>
                    <id>create-db</id>
                    <goals>
                        <goal>migrate</goal>
                    </goals>
                    <configuration>
                        <driver>org.postgresql.Driver</driver>
                        <url>jdbc:postgresql://database-server/</url>
                        <user>postgres</user>
                        <password>password</password>
                        <placeholders>
                            <DATABASE.NAME>MyDatabase</DATABASE.NAME>
                        </placeholders>
                        <locations>
                            <location>com/foo/bar/database/create</location>
                        </locations>
                    </configuration>
                </execution>
                <execution>
                    <id>migrate-db</id>
                    <goals>
                        <goal>migrate</goal>
                    </goals>
                    <configuration>
                        <driver>org.postgresql.Driver</driver>
                        <url>jdbc:postgresql://database-server/MyDatabase</url>
                        <user>postgres</user>
                        <password>password</password>
                        <locations>
                            <location>com/foo/bar/database/migrate</location>
                        </locations>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>${postgresql.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    

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

    CREATE DATABASE ${DATABASE.NAME}

    0 讨论(0)
  • 2020-12-25 12:55

    Flyway can't create database for you. It can create schema if you didn't create one by

    flyway.schemas: schema1,schema2
    
    0 讨论(0)
  • 2020-12-25 12:56

    If u have schema database creation command in V1 of your scripts, flyway can generate it but not database:

    flyway -baselineOnMigrate=true -url=jdbc:mysql://localhost/ -schemas=test_db -user=root -password=root_pass -locations=filesystem:/path/to/scrips/ migrate
    

    and similar to this in the script file:

    DROP SCHEMA IF EXISTS `test_db` ;
    CREATE SCHEMA `test_db` COLLATE utf8_general_ci ;
    
    0 讨论(0)
提交回复
热议问题