Skip to content

Commit

Permalink
fix: flyway not running for manual data source
Browse files Browse the repository at this point in the history
I changed to use `event.getBeanDefinition().getBeanName()` instead of

```java
event.getBeanDefinition() instanceof NameResolver) {
            ((NameResolver) event.getBeanDefinition())
                    .resolveName(
```
  • Loading branch information
sdelamo committed Dec 18, 2024
1 parent 5145602 commit 5125043
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ public DataSourceMigrationRunner(ApplicationContext applicationContext,
@Override
public DataSource onCreated(BeanCreatedEvent<DataSource> event) {
DataSource dataSource = event.getBean();
if (event.getBeanDefinition() instanceof NameResolver) {
((NameResolver) event.getBeanDefinition())
.resolveName()
.flatMap(name -> applicationContext.findBean(FlywayConfigurationProperties.class, Qualifiers.byName(name)))
.ifPresent(flywayConfig -> {
DataSource unwrappedDataSource = dataSourceResolver.resolve(dataSource);
run(flywayConfig, unwrappedDataSource);
});
}
event.getBeanDefinition().getBeanName()
.ifPresent(name -> runMigration(name, dataSource));
return dataSource;
}

private void runMigration(String name, DataSource dataSource) {
applicationContext.findBean(FlywayConfigurationProperties.class, Qualifiers.byName(name))
.ifPresent(flywayConfig -> {
DataSource unwrappedDataSource = dataSourceResolver.resolve(dataSource);
run(flywayConfig, unwrappedDataSource);
});
}
}
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ micronaut-test = "4.4.0"
micronaut-serde = "2.12.0"
micronaut-sql = "6.0.2"
micronaut-test-resources="2.6.2"

micronaut-data = "4.9.6"
micronaut-gradle-plugin = "4.4.4"

managed-flyway = "10.21.0"
Expand All @@ -20,7 +20,7 @@ spock = "2.3-groovy-4.0"
[libraries]
# Core
micronaut-core = { module = 'io.micronaut:micronaut-core-bom', version.ref = 'micronaut' }

micronaut-data = { module = "io.micronaut.data:micronaut-data-bom", version.ref = "micronaut-data" }
managed-flyway = { module = "org.flywaydb:flyway-core", version.ref = "managed-flyway" }
managed-flyway-mysql = { module = "org.flywaydb:flyway-mysql", version.ref = "managed-flyway" }
managed-flyway-sqlserver = { module = "org.flywaydb:flyway-sqlserver", version.ref = "managed-flyway" }
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rootProject.name = 'flyway-parent'
include 'flyway'
include 'flyway-bom'
include 'test-suite-java'
include 'test-suite-manual-datasource'
include 'test-suite-serde'

enableFeaturePreview('TYPESAFE_PROJECT_ACCESSORS')
Expand All @@ -22,6 +23,7 @@ micronautBuild {
useStandardizedProjectNames = true

importMicronautCatalog()
importMicronautCatalog("micronaut-data")
importMicronautCatalog("micronaut-serde")
importMicronautCatalog("micronaut-sql")
importMicronautCatalog("micronaut-test-resources")
Expand Down
27 changes: 27 additions & 0 deletions test-suite-manual-datasource/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id("io.micronaut.build.internal.flyway-base")
`java-library`
}

description = "Test suite for Flyway + Manual DataSource Configuration"

dependencies {
annotationProcessor(mn.micronaut.inject.java)
annotationProcessor(mnData.micronaut.data.processor)
implementation(mnData.micronaut.data.jdbc)
implementation(mn.micronaut.http.server.netty)
implementation(projects.micronautFlyway)
implementation(mnSql.hikaricp)
annotationProcessor(mnSerde.micronaut.serde.processor)
implementation(mnSerde.micronaut.serde.jackson)
runtimeOnly(mnLogging.logback.classic)
runtimeOnly(mnSql.h2)

testAnnotationProcessor(mn.micronaut.inject.java)
testImplementation(mnTest.micronaut.test.junit5)
testImplementation(mn.micronaut.http.client)
testRuntimeOnly(mnTest.junit.jupiter.engine)
}
tasks.withType<Test> {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package example.micronaut;

import io.micronaut.data.annotation.GeneratedValue;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;
import io.micronaut.serde.annotation.Serdeable;

@Serdeable
@MappedEntity
public record Book(
@GeneratedValue
@Id Long id,
String title
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package example.micronaut;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

import java.util.ArrayList;
import java.util.List;

@Controller("/books")
class BookController {
private final MicronautBookRepository micronautBookRepository;
private final GrailsBookRepository grailsBookRepository;

BookController(MicronautBookRepository micronautBookRepository,
GrailsBookRepository grailsBookRepository) {
this.micronautBookRepository = micronautBookRepository;
this.grailsBookRepository = grailsBookRepository;
}

@Get
List<Book> all() {
List<Book> all = new ArrayList<>();
all.addAll(micronaut());
all.addAll(grails());
return all;
}

@Get("/micronaut")
List<Book> micronaut() {
return micronautBookRepository.findAll();
}

@Get("/grails")
List<Book> grails() {
return grailsBookRepository.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package example.micronaut;

import io.micronaut.data.repository.CrudRepository;

public interface BookRepository extends CrudRepository<Book, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package example.micronaut;

import com.zaxxer.hikari.HikariDataSource;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Primary;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

import javax.sql.DataSource;


@Factory
public class DatasourceFactory {

@Singleton
@Primary
@Named("default")
DataSource dataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:h2:mem:micronautDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE");
ds.setUsername("sa");
ds.setPassword("");
ds.setDriverClassName("org.h2.Driver");
return ds;
}

@Singleton
@Named("grails")
DataSource dataSourceGrails() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:h2:mem:grailsDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE");
ds.setUsername("sa");
ds.setPassword("");
ds.setDriverClassName("org.h2.Driver");
return ds;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package example.micronaut;

import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;

@JdbcRepository(dialect = Dialect.H2, dataSource = "grails")
public interface GrailsBookRepository extends BookRepository {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package example.micronaut;

import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;

@JdbcRepository(dialect = Dialect.H2)
public interface MicronautBookRepository extends BookRepository{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Micronaut Books DataSource
datasources.default.dialect=H2
datasources.default.schema-generate=NONE
flyway.datasources.default.enabled=true

# Grails Books DataSource
datasources.grails.dialect=H2
datasources.grails.schema-generate=NONE
flyway.datasources.grails.enabled=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
create table book(
id bigint auto_increment primary key,
title varchar(255) not null
);
14 changes: 14 additions & 0 deletions test-suite-manual-datasource/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
<!--
<logger name="io.micronaut.flyway" level="TRACE"/>
<logger name="org.flywaydb.core" level="TRACE"/>
-->
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package example.micronaut;

import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

@MicronautTest(transactional = false)
class BookControllerTest {

@Inject
MicronautBookRepository micronautBookRepository;

@Inject
GrailsBookRepository grailsBookRepository;

@Inject
@Client("/")
HttpClient httpClient;

@Test
void books() {
//given:
saveGrailsBooks();
saveMicronautBooks();

BlockingHttpClient client = httpClient.toBlocking();
Argument<List<Book>> arg = Argument.listOf(Book.class);

//when:
List<Book> books = client.retrieve(HttpRequest.GET("/books"), arg);
//then:
assertEquals(9, books.size());

//when:
books = client.retrieve(HttpRequest.GET("/books/micronaut"), arg);
//then:
assertEquals(2, books.size());

//when:
books = client.retrieve(HttpRequest.GET("/books/grails"), arg);

//then:
assertEquals(7, books.size());
//cleanup:
grailsBookRepository.deleteAll();
micronautBookRepository.deleteAll();
}

void saveGrailsBooks() {
grailsBookRepository.save(new Book(null, "Grails 3 - Step by Step"));
grailsBookRepository.save(new Book(null, "Grails Goodness Notebook"));
grailsBookRepository.save(new Book(null, "Falando de Grails"));
grailsBookRepository.save(new Book(null, "Grails in Action"));
grailsBookRepository.save(new Book(null, "The Definitive Guide to Grails 2"));
grailsBookRepository.save(new Book(null, "Programming Grails"));
grailsBookRepository.save(new Book(null, "Grails 2: Quick Start Guide"));
}

void saveMicronautBooks() {
micronautBookRepository.save(new Book(null, "Introducing Micronaut: Build, Test and Deploy Java Microservices on Oracle Cloud"));
micronautBookRepository.save(new Book(null, "Building Microservices wiht Micronaut: A quick-start guide to building high-performance reactive microservices for Java developers"));
}
}

0 comments on commit 5125043

Please sign in to comment.