Skip to content

Commit

Permalink
Merge pull request #12677 from gsmet/liquibase-load-data
Browse files Browse the repository at this point in the history
Liquibase - Support loading data in native
  • Loading branch information
gsmet authored Oct 13, 2020
2 parents b5690d5 + dfefd1a commit d9721ea
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import static io.quarkus.deployment.annotations.ExecutionTime.STATIC_INIT;

import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -27,12 +33,18 @@
import io.quarkus.deployment.builditem.CapabilityBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.deployment.builditem.nativeimage.*;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBundleBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.deployment.pkg.steps.NativeBuild;
import io.quarkus.deployment.util.ServiceUtil;
import io.quarkus.liquibase.runtime.LiquibaseBuildTimeConfig;
import io.quarkus.liquibase.runtime.LiquibaseContainerProducer;
import io.quarkus.liquibase.runtime.LiquibaseRecorder;
import liquibase.change.Change;
import liquibase.change.core.LoadDataChange;
import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
Expand Down Expand Up @@ -82,6 +94,7 @@ void nativeImageConfiguration(
liquibase.logging.LogFactory.class.getName(),
liquibase.change.ColumnConfig.class.getName(),
liquibase.change.AddColumnConfig.class.getName(),
liquibase.change.core.LoadDataColumnConfig.class.getName(),
liquibase.sql.visitor.PrependSqlVisitor.class.getName(),
liquibase.sql.visitor.ReplaceSqlVisitor.class.getName(),
liquibase.sql.visitor.AppendSqlVisitor.class.getName(),
Expand Down Expand Up @@ -237,7 +250,7 @@ private List<String> getChangeLogs(Collection<String> dataSourceNames, Liquibase

// default datasource
if (DataSourceUtil.hasDefault(dataSourceNames)) {
resources.addAll(findAllChangeLogs(liquibaseBuildConfig.defaultDataSource.changeLog, changeLogParserFactory,
resources.addAll(findAllChangeLogFiles(liquibaseBuildConfig.defaultDataSource.changeLog, changeLogParserFactory,
classLoaderResourceAccessor, changeLogParameters));
}

Expand All @@ -250,7 +263,7 @@ private List<String> getChangeLogs(Collection<String> dataSourceNames, Liquibase

for (String namedDataSourceChangeLog : namedDataSourceChangeLogs) {
resources.addAll(
findAllChangeLogs(namedDataSourceChangeLog, changeLogParserFactory, classLoaderResourceAccessor,
findAllChangeLogFiles(namedDataSourceChangeLog, changeLogParserFactory, classLoaderResourceAccessor,
changeLogParameters));
}

Expand All @@ -262,7 +275,7 @@ private List<String> getChangeLogs(Collection<String> dataSourceNames, Liquibase
/**
* Finds all resource files for the given change log file
*/
private Set<String> findAllChangeLogs(String file, ChangeLogParserFactory changeLogParserFactory,
private Set<String> findAllChangeLogFiles(String file, ChangeLogParserFactory changeLogParserFactory,
ClassLoaderResourceAccessor classLoaderResourceAccessor,
ChangeLogParameters changeLogParameters) {
try {
Expand All @@ -275,6 +288,11 @@ private Set<String> findAllChangeLogs(String file, ChangeLogParserFactory change
for (ChangeSet changeSet : changelog.getChangeSets()) {
result.add(changeSet.getFilePath());

changeSet.getChanges().stream()
.filter(c -> c instanceof LoadDataChange)
.map(c -> ((LoadDataChange) c).getFile())
.forEach(result::add);

// get all parents of the changeSet
DatabaseChangeLog parent = changeSet.getChangeLog();
while (parent != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<include relativeToChangelogFile="true" file="json/changeLog.json" />
<include relativeToChangelogFile="true" file="sql/changeLog.sql" />
<include relativeToChangelogFile="true" file="yaml/changeLog.yaml" />
<include relativeToChangelogFile="true" file="complex-schema/changeLog.xml" />
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.9.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

<changeSet id="00000000000000" author="jhipster">
<createSequence sequenceName="sequence_generator" startValue="1050" incrementBy="50"/>
</changeSet>

<!--
JHipster core tables.
The initial schema has the '00000000000001' id, so that it is over-written if we re-generate it.
-->
<changeSet id="00000000000001" author="jhipster">
<createTable tableName="jhi_user">
<column name="id" type="bigint" >
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="login" type="varchar(50)">
<constraints unique="true" nullable="false" uniqueConstraintName="ux_user_login"/>
</column>
<column name="password_hash" type="varchar(60)"/>
<column name="first_name" type="varchar(50)"/>
<column name="last_name" type="varchar(50)"/>
<column name="email" type="varchar(191)">
<constraints unique="true" nullable="true" uniqueConstraintName="ux_user_email"/>
</column>
<column name="image_url" type="varchar(256)"/>
<column name="activated" type="boolean" valueBoolean="false">
<constraints nullable="false" />
</column>
<column name="lang_key" type="varchar(10)"/>
<column name="activation_key" type="varchar(20)"/>
<column name="reset_key" type="varchar(20)"/>
<column name="created_by" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="created_date" type="timestamp"/>
<column name="reset_date" type="timestamp">
<constraints nullable="true"/>
</column>
<column name="last_modified_by" type="varchar(50)"/>
<column name="last_modified_date" type="timestamp"/>
</createTable>

<createTable tableName="jhi_authority">
<column name="name" type="varchar(50)">
<constraints primaryKey="true" nullable="false"/>
</column>
</createTable>

<createTable tableName="jhi_user_authority">
<column name="user_id" type="bigint">
<constraints nullable="false"/>
</column>
<column name="authority_name" type="varchar(50)">
<constraints nullable="false"/>
</column>
</createTable>

<addPrimaryKey columnNames="user_id, authority_name" tableName="jhi_user_authority"/>

<addForeignKeyConstraint baseColumnNames="authority_name"
baseTableName="jhi_user_authority"
constraintName="fk_authority_name"
referencedColumnNames="name"
referencedTableName="jhi_authority"/>

<addForeignKeyConstraint baseColumnNames="user_id"
baseTableName="jhi_user_authority"
constraintName="fk_user_id"
referencedColumnNames="id"
referencedTableName="jhi_user"/>

<addNotNullConstraint columnName="password_hash"
columnDataType="varchar(60)"
tableName="jhi_user"/>
<loadData
file="db/complex-schema/data/user.csv"
separator=";"
tableName="jhi_user">
<column name="id" type="numeric"/>
<column name="activated" type="boolean"/>
<column name="created_date" type="timestamp"/>
</loadData>
<dropDefaultValue tableName="jhi_user" columnName="created_date" columnDataType="datetime"/>
<loadData
file="db/complex-schema/data/authority.csv"
separator=";"
tableName="jhi_authority">
<column name="name" type="string"/>
</loadData>

<loadData
file="db/complex-schema/data/user_authority.csv"
separator=";"
tableName="jhi_user_authority">
<column name="user_id" type="numeric"/>
</loadData>
<createTable tableName="jhi_persistent_audit_event">
<column name="event_id" type="bigint" >
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="principal" type="varchar(50)">
<constraints nullable="false" />
</column>
<column name="event_date" type="timestamp"/>
<column name="event_type" type="varchar(255)"/>
</createTable>

<createTable tableName="jhi_persistent_audit_evt_data">
<column name="event_id" type="bigint">
<constraints nullable="false"/>
</column>
<column name="name" type="varchar(150)">
<constraints nullable="false"/>
</column>
<column name="value" type="varchar(255)"/>
</createTable>
<addPrimaryKey columnNames="event_id, name" tableName="jhi_persistent_audit_evt_data"/>

<createIndex indexName="idx_persistent_audit_event"
tableName="jhi_persistent_audit_event"
unique="false">
<column name="principal" type="varchar(50)"/>
<column name="event_date" type="timestamp"/>
</createIndex>

<createIndex indexName="idx_persistent_audit_evt_data"
tableName="jhi_persistent_audit_evt_data"
unique="false">
<column name="event_id" type="bigint"/>
</createIndex>

<addForeignKeyConstraint baseColumnNames="event_id"
baseTableName="jhi_persistent_audit_evt_data"
constraintName="fk_evt_pers_audit_evt_data"
referencedColumnNames="event_id"
referencedTableName="jhi_persistent_audit_event"/>
</changeSet>

<changeSet author="jhipster" id="00000000000002" context="test">
<createTable tableName="jhi_date_time_wrapper">
<column name="id" type="BIGINT">
<constraints primaryKey="true" primaryKeyName="jhi_date_time_wrapperPK"/>
</column>
<column name="instant" type="timestamp"/>
<column name="local_date_time" type="timestamp"/>
<column name="offset_date_time" type="timestamp"/>
<column name="zoned_date_time" type="timestamp"/>
<column name="local_time" type="time"/>
<column name="offset_time" type="time"/>
<column name="local_date" type="date"/>
</createTable>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name
ROLE_ADMIN
ROLE_USER
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id;login;password_hash;first_name;last_name;email;image_url;activated;lang_key;created_by;last_modified_by
1;system;$2a$10$mE.qmcV0mFU5NcKh73TZx.z4ueI/.bDWbj0T1BYyqP481kGGarKLG;System;System;system@localhost;;true;ru;system;system
2;anonymoususer;$2a$10$j8S5d7Sr7.8VTOYNviDPOeWX8KcYILUVJBsYV83Y5NtECayypx9lO;Anonymous;User;anonymous@localhost;;true;ru;system;system
3;admin;$2a$10$gSAhZrxMllrbgj/kkK9UceBPpChGWJA7SYIb1Mqo.n5aNLq1/oRrC;Administrator;Administrator;admin@localhost;;true;ru;system;system
4;user;$2a$10$VEjxo0jq2YG9Rbk2HmX9S.k1uZBGYUHdUcid3g/vfiEl7lwWgOH/K;User;User;user@localhost;;true;ru;system;system
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
user_id;authority_name
1;ROLE_ADMIN
1;ROLE_USER
3;ROLE_ADMIN
3;ROLE_USER
4;ROLE_USER
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void testLiquibaseQuarkusFunctionality() {
.get("/liquibase/update")
.then()
.body(is(
"create-tables-1,test-1,json-create-tables-1,json-test-1,sql-create-tables-1,sql-test-1,yaml-create-tables-1,yaml-test-1"));
"create-tables-1,test-1,json-create-tables-1,json-test-1,sql-create-tables-1,sql-test-1,yaml-create-tables-1,yaml-test-1,00000000000000,00000000000001,00000000000002"));
}

}

0 comments on commit d9721ea

Please sign in to comment.