Skip to content

Commit

Permalink
Upgrading Flyway to 7.11.1
Browse files Browse the repository at this point in the history
Upgrading Gradle dependencies to remove vulnerable dependencies
Upgrading to gradle 8
Add contribution instructions

fixes #16
fixes #14
fixes #13
  • Loading branch information
assafkamil committed Mar 4, 2023
1 parent c162310 commit 2e0fc2b
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 61 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/github-actions-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ name: Java CI

on:
push:
tags-ignore:
- '**'
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
Expand All @@ -20,6 +21,12 @@ jobs:
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
- name: Build with Gradle
run: ./gradlew build
- name: Upload test results
uses: actions/upload-artifact@v2
if: success() || failure()
with:
name: test-results
path: build/reports/tests/test
- name: Package
run: ./gradlew shadowJar
- uses: actions/upload-artifact@v2
Expand Down
15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Setting up the code in local

### Pre requisites

- Open JDK 11 must be installed in your system
- Docker must be installed in your system

### Steps

- To build the code base and run tests, run command `./gradlew clean build`
- To create a runnable jar, run command `./gradew clean shadowJar`

### Troubleshooting

- If you are using [colima](https://github.com/abiosoft/colima) as your docker container runtime, follow the instructions in https://www.testcontainers.org/supported_docker_environment/#using-colima
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ Flyway config files can also reside in s3 or within your git repo
## Logging
flyway-lambda logs to CloudWatch.
For more information, check [Logging](https://github.com/Geekoosh/flyway-lambda/wiki/Logging).

## Contributing
Please read the [contributing guidelines](CONTRIBUTING.md) to set up your development machine and proceed.
65 changes: 39 additions & 26 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
group 'com.geekoosh'
version '0.0.4'
version '0.9'

apply plugin: 'maven-publish'
apply plugin: 'java'


sourceCompatibility = 1.11

repositories {
Expand All @@ -21,7 +23,7 @@ buildscript {
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
classpath "com.github.jengelman.gradle.plugins:shadow:6.1.0"
classpath "com.github.johnrengelman:shadow:8.1.0"
classpath 'ca.cutterslade.gradle:gradle-dependency-analyze:1.3.3'
}
}
Expand All @@ -40,33 +42,44 @@ dependencyManagement {
}
}

ext {
jgitVersion = '6.4.0.202211300538-r'
jettyVersion = '10.0.14'
log4jVersion = '2.20.0'
testContainersVersion = '1.17.6'
}

dependencies {
implementation group: 'org.flywaydb', name: 'flyway-core', version: '7.5.3'
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '5.5.0.201909110433-r'
implementation group: 'commons-io', name: 'commons-io', version: '2.5'
implementation group: 'org.json', name: 'json', version: '20190722'
implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.12'
implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.0'
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.0'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
implementation group: 'com.amazonaws', name: 'aws-lambda-java-core', version: '1.2.0'
implementation group: 'com.amazonaws', name: 'aws-lambda-java-log4j2', version: '1.5.0'
implementation group: 'org.flywaydb', name: 'flyway-core', version: '7.11.1'
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: "${jgitVersion}"
implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
implementation group: 'org.json', name: 'json', version: '20230227'
implementation group: 'org.postgresql', name: 'postgresql', version: '42.5.4'
implementation group: 'com.mysql', name: 'mysql-connector-j', version: '8.0.32'
implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4jVersion}"
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4jVersion}"
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation group: 'com.amazonaws', name: 'aws-lambda-java-core', version: '1.2.2'
implementation group: 'com.amazonaws', name: 'aws-lambda-java-log4j2', version: '1.5.1'
implementation group: 'com.amazonaws', name: 'aws-java-sdk-s3'
implementation group: 'com.amazonaws', name: 'aws-java-sdk-secretsmanager'
implementation platform('org.testcontainers:testcontainers-bom:1.16.2')
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.+'
testCompile group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0'
testCompile group: 'com.adobe.testing', name: 's3mock', version: '1.0.4'
testCompile group: 'org.eclipse.jgit', name: 'org.eclipse.jgit.http.server', version: '5.5.0.201909110433-r'
testCompile group: 'org.eclipse.jetty', name: 'jetty-servlet', version: '9.4.3.v20170317'
testCompile group: 'org.eclipse.jgit', name: 'org.eclipse.jgit.junit.http', version: '5.5.0.201909110433-r'
testCompile group: 'org.eclipse.jgit', name: 'org.eclipse.jgit.junit', version: '5.5.0.201909110433-r'
testCompile group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '5.5.0.201909110433-r'
testCompile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '8.5.23'
testCompile group: 'org.testcontainers', name: 'postgresql', version: "1.16.2"
testCompile group: 'org.testcontainers', name: 'mysql', version: "1.16.2"
implementation platform("org.testcontainers:testcontainers-bom:${testContainersVersion}")
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.+'
testImplementation group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0'
testImplementation group: 'cloud.localstack', name: 'localstack-utils', version: '0.2.22'
testImplementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit.http.server', version: "${jgitVersion}"
testImplementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit.junit.http', version: "${jgitVersion}"
testImplementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit.junit', version: "${jgitVersion}"
testImplementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: "${jgitVersion}"
testImplementation group: 'org.eclipse.jetty', name: 'jetty-servlet', version: "${jettyVersion}"
testImplementation group: 'org.eclipse.jetty', name: 'jetty-util', version: "${jettyVersion}"
testImplementation group: 'org.eclipse.jetty', name: 'jetty-server', version: "${jettyVersion}"
testImplementation group: 'org.eclipse.jetty', name: 'jetty-security', version: "${jettyVersion}"
testImplementation group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '8.5.23'
testImplementation group: 'org.testcontainers', name: 'postgresql', version: "${testContainersVersion}"
testImplementation group: 'org.testcontainers', name: 'mysql', version: "${testContainersVersion}"
testImplementation "org.testcontainers:localstack:${testContainersVersion}"
}

task buildZip(type: Zip) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
3 changes: 2 additions & 1 deletion src/main/java/com/geekoosh/flyway/FlywayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.amazonaws.services.s3.model.S3Object;
import com.geekoosh.flyway.request.DBRequest;
import com.geekoosh.flyway.request.FlywayMethod;
import com.geekoosh.flyway.request.FlywayRequest;
import com.geekoosh.lambda.MigrationFilesService;
import com.geekoosh.lambda.s3.S3Service;
Expand Down Expand Up @@ -241,7 +242,7 @@ public void validate() throws IOException {
flyway.validate();
}
public MigrationInfoService call() throws IOException {
switch (flywayRequest.getFlywayMethod()) {
switch (FlywayMethod.valueOf(flywayRequest.getFlywayMethod())) {
case MIGRATE:
logger.info("Running migration");
migrate();
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/geekoosh/flyway/request/FlywayRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.geekoosh.flyway.request;

import java.util.List;
import java.util.Locale;
import java.util.Map;

public class FlywayRequest {
Expand Down Expand Up @@ -35,7 +36,7 @@ public class FlywayRequest {
private String baselineDescription;
private String installedBy;
private String configFile;
private FlywayMethod flywayMethod;
private String flywayMethod = FlywayMethod.MIGRATE.toString();

public Integer getConnectRetries() {
return connectRetries;
Expand Down Expand Up @@ -316,12 +317,16 @@ public FlywayRequest setConfigFile(String configFile) {
return this;
}

public FlywayMethod getFlywayMethod() {
public String getFlywayMethod() {
return flywayMethod;
}

public FlywayRequest setFlywayMethod(FlywayMethod flywayMethod) {
this.flywayMethod = flywayMethod;
this.flywayMethod = flywayMethod.toString();
return this;
}
public FlywayRequest setFlywayMethod(String flywayMethod) {
this.flywayMethod = flywayMethod.toUpperCase(Locale.ROOT);
return this;
}

Expand All @@ -333,7 +338,7 @@ public static FlywayRequest build(FlywayRequest base) {
base.getConfigFile(), EnvironmentVars.FLYWAY_CONFIG_FILE
));
base.setFlywayMethod(ValueManager.value(
base.getFlywayMethod(), EnvironmentVars.FLYWAY_METHOD,
FlywayMethod.valueOf(base.getFlywayMethod()), EnvironmentVars.FLYWAY_METHOD,
v -> v == null ? FlywayMethod.MIGRATE : FlywayMethod.valueOf(v)
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void testMigrateMySQL() throws Exception {

con.close();

request.setFlywayRequest(new FlywayRequest().setFlywayMethod(FlywayMethod.CLEAN));
request.setFlywayRequest(new FlywayRequest().setFlywayMethod("clean"));
response = flywayHandler.handleRequest(request, null);
Assert.assertNull(response.getInfo().getCurrent());
}
Expand Down Expand Up @@ -247,7 +247,7 @@ public void testBaselineMySQL() throws Exception {
);
FlywayHandler flywayHandler = new FlywayHandler();
Request request = new Request().setFlywayRequest(
new FlywayRequest().setFlywayMethod(FlywayMethod.BASELINE).setBaselineVersion("1")
new FlywayRequest().setFlywayMethod("BASELINE").setBaselineVersion("1")
);
Response response = flywayHandler.handleRequest(request, null);
Assert.assertEquals("1", response.getInfo().getCurrent().getVersion().toString());
Expand Down
34 changes: 20 additions & 14 deletions src/test/java/com/geekoosh/flyway/FlywayHandlerPostgresTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@

package com.geekoosh.flyway;

import com.adobe.testing.s3mock.S3MockRule;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.secretsmanager.AWSSecretsManager;
import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest;
import com.amazonaws.services.secretsmanager.model.GetSecretValueResult;
Expand All @@ -60,25 +58,33 @@
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.utility.DockerImageName;

import java.io.File;
import java.sql.*;

@RunWith(MockitoJUnitRunner.class)
public class FlywayHandlerPostgresTests {

@ClassRule
public static S3MockRule S3_MOCK_RULE = new S3MockRule();
private final AmazonS3 s3 = S3_MOCK_RULE.createS3Client();
private final S3MockHelper s3MockHelper = new S3MockHelper(s3);
public static LocalStackContainer localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3"))
.withServices(LocalStackContainer.Service.S3);
private S3MockHelper s3MockHelper;
private static final String BUCKET1 = "bucket-1";
private static final String BUCKET2 = "bucket-2";
private static final String BUCKET3 = "bucket-3";

@Rule
public final EnvironmentVariables environmentVariables
= new EnvironmentVariables();

@Before
public void setUp() throws Exception {
S3Service.setAmazonS3(s3);
public void setUp() {
s3MockHelper = new S3MockHelper(localstack);
S3Service.setAmazonS3(s3MockHelper.getS3());
s3MockHelper.getS3().createBucket(BUCKET1);
s3MockHelper.getS3().createBucket(BUCKET2);
s3MockHelper.getS3().createBucket(BUCKET3);
}

private void testMigrate(String connectionString, String bucket) throws SQLException {
Expand Down Expand Up @@ -134,10 +140,10 @@ public void testMigratePostgresWithS3Folder() throws SQLException {
postgres.start();
s3MockHelper.upload(
new File(getClass().getClassLoader().getResource("migrations/postgres/V1__init.sql").getFile()),
"Bucket-1",
BUCKET1,
"migrations/V1__init.sql"
);
testMigrate(postgres.getJdbcUrl(), "Bucket-1", "migrations");
testMigrate(postgres.getJdbcUrl(), BUCKET1, "migrations");
}
}
@Test
Expand All @@ -146,10 +152,10 @@ public void testMigratePostgres() throws SQLException {
postgres.start();
s3MockHelper.upload(
new File(getClass().getClassLoader().getResource("migrations/postgres/V1__init.sql").getFile()),
"Bucket-2",
BUCKET2,
"V1__init.sql"
);
testMigrate(postgres.getJdbcUrl(), "Bucket-2");
testMigrate(postgres.getJdbcUrl(), BUCKET2);
}
}
@Test
Expand All @@ -166,10 +172,10 @@ public void testPasswordSecret() throws SQLException {
.thenReturn(getSecretValueResult);
s3MockHelper.upload(
new File(getClass().getClassLoader().getResource("migrations/postgres/V1__init.sql").getFile()),
"Bucket-3",
BUCKET3,
"migrations/V1__init.sql"
);
testMigrate(postgres.getJdbcUrl(), "Bucket-3", "migrations", "password_secret", true);
testMigrate(postgres.getJdbcUrl(), BUCKET3, "migrations", "password_secret", true);
}
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/test/java/com/geekoosh/flyway/FlywayServiceTests.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.geekoosh.flyway;

import com.adobe.testing.s3mock.S3MockRule;
import com.amazonaws.services.s3.AmazonS3;
import com.geekoosh.flyway.request.DBRequest;
import com.geekoosh.flyway.request.FlywayRequest;
import com.geekoosh.lambda.MigrationFilesService;
Expand All @@ -19,6 +17,8 @@
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.utility.DockerImageName;

import static org.junit.Assert.*;

Expand All @@ -35,17 +35,20 @@ public class FlywayServiceTests {
= new EnvironmentVariables();

@ClassRule
public static S3MockRule S3_MOCK_RULE = new S3MockRule();

private final AmazonS3 s3 = S3_MOCK_RULE.createS3Client();
public static LocalStackContainer localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3"))
.withServices(LocalStackContainer.Service.S3);
private final String BUCKET1 = "bucket-1";
private final S3MockHelper s3MockHelper = new S3MockHelper(localstack);

@Before
public void setUp() throws IOException {
s3MockHelper.getS3().createBucket(BUCKET1);
Properties confProps = new Properties();
confProps.setProperty("flyway.sqlMigrationPrefix", "F");
S3MockHelper s3MockHelper = new S3MockHelper(s3);
s3MockHelper.uploadConfig(confProps, "Bucket-1", "flyway/config.props");
s3.putObject("Bucket-1", "flyway/bad-config.props", "vary bad string");

s3MockHelper.uploadConfig(confProps, BUCKET1, "flyway/config.props");

s3MockHelper.getS3().putObject(BUCKET1, "flyway/bad-config.props", "vary bad string");
}

private MySQLContainer mySQLContainer() {
Expand All @@ -58,7 +61,7 @@ public void testConfigure() throws IOException {
Mockito.when(migrationFilesService.getFolders()).thenReturn(Arrays.asList("folder1", "folder2"));
try (MySQLContainer mysql = mySQLContainer()) {
mysql.start();
S3Service.setAmazonS3(s3);
S3Service.setAmazonS3(s3MockHelper.getS3());
String connectionString = mysql.getJdbcUrl();
DBRequest dbRequest = new DBRequest().setUsername("user").setPassword("password").setConnectionString(connectionString);

Expand All @@ -73,7 +76,7 @@ public void testConfigure() throws IOException {
Stream.of(conf.getLocations()).map(Location::getPath).collect(Collectors.toList()));
assertEquals("V", conf.getSqlMigrationPrefix());

environmentVariables.set("FLYWAY_CONFIG_FILE", "s3://Bucket-1/flyway/config.props");
environmentVariables.set("FLYWAY_CONFIG_FILE", String.format("s3://%s/flyway/config.props", BUCKET1));
flywayService = new FlywayService(
FlywayRequest.build(null),
dbRequest,
Expand Down Expand Up @@ -112,7 +115,7 @@ public void testConfigurationNotFound() throws IOException {
try (MySQLContainer mysql = mySQLContainer()) {
mysql.start();

environmentVariables.set("FLYWAY_CONFIG_FILE", "s3://Bucket-1/flyway/config-not-there.props");
environmentVariables.set("FLYWAY_CONFIG_FILE", String.format("s3://%s/flyway/config-not-there.props", BUCKET1));
String connectionString = mysql.getJdbcUrl();
DBRequest dbRequest = new DBRequest().setUsername("user").setPassword("password").setConnectionString(connectionString);

Expand Down
Loading

0 comments on commit 2e0fc2b

Please sign in to comment.