Skip to content

Commit

Permalink
Flyway extension
Browse files Browse the repository at this point in the history
  • Loading branch information
cristhiank committed Mar 23, 2019
1 parent 68e0df2 commit 4da876a
Show file tree
Hide file tree
Showing 21 changed files with 1,011 additions and 1 deletion.
13 changes: 12 additions & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<jprocesses.version>1.6.5</jprocesses.version>
<jboss-logmanager.version>1.0.2</jboss-logmanager.version>
<jetty.version>9.4.15.v20190215</jetty.version>
<flyway.version>5.2.4</flyway.version>
<yasson.version>1.0.3</yasson.version>
</properties>

Expand Down Expand Up @@ -261,6 +262,12 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
Expand Down Expand Up @@ -1714,7 +1721,11 @@
<artifactId>jetty-io</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>${flyway.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
10 changes: 10 additions & 0 deletions build-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,16 @@
<artifactId>quarkus-kotlin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway-runtime</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Miscellaneous -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public final class FeatureBuildItem extends MultiBuildItem {
public static final String UNDERTOW_WEBSOCKETS = "undertow-websockets";
public static final String VERTX = "vertx";
public static final String VERTX_WEB = "vertx-web";
public static final String FLYWAY = "flyway";

private final String info;

Expand Down
58 changes: 58 additions & 0 deletions extensions/flyway/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018 Red Hat, Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-flyway-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-flyway</artifactId>
<name>Quarkus - Flyway - Deployment</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway-runtime</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2018 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.quarkus.flyway;

import static java.nio.file.Files.walk;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;

import org.jboss.logging.Logger;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.substrate.SubstrateResourceBuildItem;

class FlywayBuildStep {

private static final String FLYWAY_DATABASES_PATH_ROOT = "org/flywaydb/core/internal/database";
private static final String FLYWAY_METADATA_TABLE_FILENAME = "createMetaDataTable.sql";
private static final String[] FLYWAY_DATABASES_WITH_SQL_FILE = {
"cockroachdb",
"derby",
"h2",
"hsqldb",
"mysql",
"oracle",
"postgresql",
"redshift",
"saphana",
"sqlite",
"sybasease"
};

private static final Logger LOGGER = Logger.getLogger(FlywayBuildStep.class);

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FeatureBuildItem.FLYWAY);
}

@Record(ExecutionTime.STATIC_INIT)
@BuildStep(providesCapabilities = "io.quarkus.flyway")
void registerSubstrateResources(
BuildProducer<SubstrateResourceBuildItem> resource,
ApplicationArchivesBuildItem appArchives) throws IOException, URISyntaxException {
Path root = appArchives.getRootArchive().getArchiveRoot();
List<String> resources = generateDatabasesSQLFiles();
resources.addAll(discoverApplicationMigrations(root));
resource.produce(new SubstrateResourceBuildItem(resources.toArray(new String[0])));
}

private List<String> discoverApplicationMigrations(Path root) throws IOException, URISyntaxException {
List<String> resources = new ArrayList<>();
try {
// TODO: this should be configurable, using flyway default
String location = "db/migration";
Enumeration<URL> migrations = Thread.currentThread().getContextClassLoader().getResources(location);
while (migrations.hasMoreElements()) {
URL path = migrations.nextElement();
LOGGER.info("Adding application migrations in path: " + path);
List<String> applicationMigrations = walk(Paths.get(path.toURI()))
.filter(Files::isRegularFile)
.map(it -> Paths.get(location, it.getFileName().toString()).toString())
.peek(it -> LOGGER.info("Discovered: " + it))
.collect(Collectors.toList());
resources.addAll(applicationMigrations);
}
return resources;
} catch (IOException | URISyntaxException e) {
LOGGER.fatal("Error discovering application migrations: " + e.getMessage(), e);
throw e;
}
}

private List<String> generateDatabasesSQLFiles() {
List<String> result = new ArrayList<>();
for (String database : FLYWAY_DATABASES_WITH_SQL_FILE) {
String filePath = FLYWAY_DATABASES_PATH_ROOT + "/" + database + "/" + FLYWAY_METADATA_TABLE_FILENAME;
result.add(filePath);
LOGGER.info("Adding flyway internal migration: " + filePath);
}
return result;
}
}
37 changes: 37 additions & 0 deletions extensions/flyway/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018 Red Hat, Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<artifactId>quarkus-build-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
<relativePath>../../build-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-flyway-parent</artifactId>
<name>Quarkus - Flyway</name>
<packaging>pom</packaging>
<modules>
<module>runtime</module>
<module>deployment</module>
</modules>
</project>
63 changes: 63 additions & 0 deletions extensions/flyway/runtime/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018 Red Hat, Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-flyway-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-flyway-runtime</artifactId>
<name>Quarkus - Flyway - Runtime</name>

<dependencies>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.substratevm</groupId>
<artifactId>svm</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<!-- Mark this as a runtime dependency, so to make sure it's included on the final classpath during native-image -->
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 4da876a

Please sign in to comment.