Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registering Liquibase BigIntType and Additional Classes for Reflection #42964

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.liquibase.deployment;

import static java.util.function.Predicate.not;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Paths;
Expand All @@ -16,7 +14,6 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import jakarta.enterprise.context.ApplicationScoped;
Expand All @@ -36,7 +33,6 @@
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand Down Expand Up @@ -77,6 +73,7 @@
import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.datatype.DataTypeInfo;
import liquibase.exception.LiquibaseException;
import liquibase.parser.ChangeLogParser;
import liquibase.parser.ChangeLogParserFactory;
Expand All @@ -97,6 +94,7 @@ class LiquibaseProcessor {
"www.liquibase.org/xml/ns/dbchangelog/*.xsd"));

private static final DotName DATABASE_CHANGE_PROPERTY = DotName.createSimple(DatabaseChangeProperty.class.getName());
private static final DotName DATA_TYPE_INFO_ANNOTATION = DotName.createSimple(DataTypeInfo.class.getName());

@BuildStep
FeatureBuildItem feature() {
Expand All @@ -114,7 +112,6 @@ void nativeImageConfiguration(
List<JdbcDataSourceBuildItem> jdbcDataSourceBuildItems,
CombinedIndexBuildItem combinedIndex,
CurateOutcomeBuildItem curateOutcome,
Capabilities capabilities,
BuildProducer<ReflectiveClassBuildItem> reflective,
BuildProducer<NativeImageResourceBuildItem> resource,
BuildProducer<ServiceProviderBuildItem> services,
Expand All @@ -126,8 +123,19 @@ void nativeImageConfiguration(
liquibase.sqlgenerator.core.LockDatabaseChangeLogGenerator.class.getName()));

reflective.produce(ReflectiveClassBuildItem
.builder(liquibase.change.AbstractSQLChange.class, liquibase.database.jvm.JdbcConnection.class).methods()
.builder(liquibase.datatype.core.UnknownType.class.getName())
.reason(getClass().getName())
.constructors().methods()
.build());

reflective.produce(ReflectiveClassBuildItem.builder(
liquibase.AbstractExtensibleObject.class.getName(),
liquibase.change.core.DeleteDataChange.class.getName(),
liquibase.change.core.EmptyChange.class.getName(),
liquibase.database.jvm.JdbcConnection.class.getName(),
liquibase.plugin.AbstractPlugin.class.getName())
.reason(getClass().getName())
.methods()
.build());

reflective.produce(ReflectiveClassBuildItem
Expand All @@ -146,6 +154,7 @@ void nativeImageConfiguration(
.constructors().build());

reflective.produce(ReflectiveClassBuildItem.builder(
liquibase.changelog.RanChangeSet.class.getName(),
liquibase.configuration.LiquibaseConfiguration.class.getName(),
liquibase.parser.ChangeLogParserConfiguration.class.getName(),
liquibase.GlobalConfiguration.class.getName(),
Expand Down Expand Up @@ -185,6 +194,18 @@ void nativeImageConfiguration(
.reason(getClass().getName())
.constructors().methods().fields().build());

// register all liquibase.datatype.core.* data types
Set<String> classesAnnotatedWithDataTypeInfo = combinedIndex.getIndex().getAnnotations(DATA_TYPE_INFO_ANNOTATION)
.stream()
.map(AnnotationInstance::target)
.filter(at -> at.kind() == AnnotationTarget.Kind.CLASS)
.map(at -> at.asClass().name().toString())
.collect(Collectors.toSet());
reflective.produce(ReflectiveClassBuildItem.builder(classesAnnotatedWithDataTypeInfo.toArray(String[]::new))
.reason(getClass().getName())
.constructors().methods()
.build());

Collection<String> dataSourceNames = jdbcDataSourceBuildItems.stream()
.map(JdbcDataSourceBuildItem::getName)
.collect(Collectors.toSet());
Expand All @@ -200,20 +221,6 @@ void nativeImageConfiguration(
.constructors().methods().fields().build());
});

// CommandStep implementations are needed
consumeService(liquibase.command.CommandStep.class.getName(), (serviceClassName, implementations) -> {
var filteredImpls = implementations.stream()
.filter(commandStepPredicate(capabilities))
.toArray(String[]::new);
services.produce(new ServiceProviderBuildItem(serviceClassName, filteredImpls));
reflective.produce(ReflectiveClassBuildItem.builder(filteredImpls)
.reason(getClass().getName())
.constructors().build());
for (String implementation : filteredImpls) {
runtimeInitialized.produce(new RuntimeInitializedClassBuildItem(implementation));
}
});

var dependencies = curateOutcome.getApplicationModel().getRuntimeDependencies();
resource.produce(NativeImageResourceBuildItem.ofDependencyResources(
dependencies, LIQUIBASE_ARTIFACT, LIQUIBASE_RESOURCE_FILTER));
Expand All @@ -223,14 +230,6 @@ void nativeImageConfiguration(
resourceBundle.produce(new NativeImageResourceBundleBuildItem("liquibase/i18n/liquibase-core"));
}

private static Predicate<String> commandStepPredicate(Capabilities capabilities) {
if (capabilities.isPresent("io.quarkus.jdbc.h2")) {
return (s) -> true;
} else {
return not("liquibase.command.core.StartH2CommandStep"::equals);
}
}

private void consumeService(String serviceClassName, BiConsumer<String, Collection<String>> consumer) {
try {
String service = ServiceProviderBuildItem.SPI_ROOT + serviceClassName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<include relativeToChangelogFile="true" file="create-tables.xml" />

<include relativeToChangelogFile="true" file="test/test.xml" />

<include relativeToChangelogFile="true" file="test/test-data-types.xml" />
<include relativeToChangelogFile="true" file="create-views.xml" />

<includeAll relativeToChangelogFile="true" path="includeAll" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<changeSet author="dev (generated)" id="create-data-types-tables-1">
<createTable tableName="TYPE_TEST">
<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="decimal_col" type="decimal" autoIncrement="true"/>
<column name="int_col" type="int" autoIncrement="true"/>
<column name="med_int_col" type="mediumint" autoIncrement="true"/>
<column name="number_col" type="number" autoIncrement="true"/>
<column name="small_int_col" type="smallint" autoIncrement="true"/>
<column name="tiny_int_col" type="tinyint" autoIncrement="true"/>
</createTable>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static void doTestLiquibaseQuarkusFunctionality() {
.get("/liquibase/update")
.then()
.body(is(
"create-tables-1,test-1,create-view-inline,create-view-file-abs,create-view-file-rel,"
"create-tables-1,test-1,create-data-types-tables-1,create-view-inline,create-view-file-abs,create-view-file-rel,"
+ ("includeAll-1,includeAll-2,")
+ "json-create-tables-1,json-test-1,"
+ "sql-create-tables-1,sql-test-1,"
Expand Down