简体   繁体   English

使用gradle和flyway插件创建MySQL模式

[英]Create MySQL schema with gradle and flyway plugin

We are using gradle in our project to build a multi-repository project. 我们在项目中使用gradle来构建一个多存储库项目。 Since it is automatically built and tested on a Jenkins build server, we need to set up and tear down the database. 由于它是在Jenkins构建服务器上自动构建和测试的,因此我们需要设置和拆除数据库。

My understanding of Continuous Integration best practices is that we should have a single push of a button to also set up and tear down the testing database. 我对持续集成最佳实践的理解是,我们只需按一下按钮即可设置和拆除测试数据库。 Even if this does not exist yet. 即使这还不存在。 So I want a gradle task that simply creates a database in MySQL if it does not exist yet. 所以我想要一个gradle任务,如果它还不存在,只需在MySQL中创建一个数据库。

Browsing the this fine site I thought I had found the answer when I was pointed to using the flyway plugin for Gradle. 浏览这个很好的网站,当我被指向使用Gradle的flyway插件时,我以为我找到了答案。 Apparently flyway does support automatic schema creation since version 2.1 ( http://java.dzone.com/announcements/flyway-21-released-automatic ), so it should be possible using the plugin, should it not? 从版本2.1( http://java.dzone.com/announcements/flyway-21-released-automatic )开始,flyway确实支持自动模式创建,所以应该可以使用该插件,如果不是这样的话?

However, if I run gradle flywayInit or gradle flywayClean , using a url like url = 'jdbc:mysql://127.0.0.1:3306/test' every time I get the message unknown database: test . 但是,如果我运行gradle flywayInitgradle flywayClean ,每次我收到消息unknown database: test都使用url = 'jdbc:mysql://127.0.0.1:3306/test'这样的url = 'jdbc:mysql://127.0.0.1:3306/test' unknown database: test So I tried the following: 所以我尝试了以下方法:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'org.flywaydb:flyway-gradle-plugin:3.0'
    }
}

// Use Flyway plugin to create database
apply plugin: 'flyway'
flyway {
    user = 'root'
    password = 'root'
    url = 'jdbc:mysql://127.0.0.1:3306'
    schemas = ['test']
}

I would have expected another connection error. 我本来期望另一个连接错误。 Instead the build was succesful, but no database schema was created. 相反,构建是成功的,但没有创建数据库架构。 How can I create a schema using gradle and Flyway? 如何使用gradle和Flyway创建架构? If we can do it without Flyway that is fine, too. 如果我们可以在没有Flyway的情况下做到这一点也很好。

UPDATE: It turns out this works just fine. 更新:事实证明这很好用。 I was looking at an outdated version of my list of database schemas. 我正在查看我的数据库模式列表的过时版本。 Apparently Sequel Pro is too stupid to update the list of schemas when you refresh. 显然,Sequel Pro在刷新时更新模式列表太愚蠢了。 Or I am too stupid to find the right button in Sequel pro. 或者我太傻了,无法在Sequel pro中找到合适的按钮。

UPDATE 2: To make sure this always happens on every build, I added the following line to build.gradle: 更新2:为了确保每次构建都会发生这种情况,我将以下行添加到build.gradle:

// Add dependencies to tasks
build.dependsOn flywayInit

I followed Get started with Gradle and Flyway and everything ran ok. 我跟着开始使用Gradle和Flyway ,一切都运行正常。

I am using gradle 2.2 and the build.gradle file is: 我使用的是gradle 2.2,build.gradle文件是:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'mysql:mysql-connector-java:5.1.34'
        classpath 'org.flywaydb:flyway-gradle-plugin:3.1'
    }
}

apply plugin: 'flyway'
apply plugin: 'java'

flyway {
    url = 'jdbc:mysql://localhost:3306'
    user = 'root'
    password = 'root'
    schemas = ['demo1']
}

After that, run gradle flywayMigrate -i . 之后,运行gradle flywayMigrate -i If the database does not exist, will be created by flyway. 如果数据库不存在,将由flyway创建。

You can see a list of Flyway tasks here: link 您可以在此处查看Flyway任务列表: 链接

There is a subtle, but important difference between what you have there and what Flyway can do: since version 2.1 Flyway can create schemas , but not databases . 你在那里和Flyway可以做的事情之间有一个微妙但重要的区别:因为版本2.1 Flyway可以创建模式 ,但不能创建数据库

The name in the MySQL url is a database you must create yourself using the MySQL command create database . MySQL url中的名称是您必须使用MySQL命令create database自行create database Within that database, Flyway can create the schemas you specify with flyway.schemas . 在该数据库中,Flyway可以使用flyway.schemas创建您指定的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM