jungm commented on code in PR #1525: URL: https://github.com/apache/tomee/pull/1525#discussion_r1779479704
########## examples/import-database-flyway/pom.xml: ########## @@ -0,0 +1,160 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with ~ + this work for additional information regarding copyright ownership. ~ The + ASF licenses this file to You 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. --> Review Comment: license header ########## examples/import-database-liquibase-maven/README.adoc: ########## @@ -0,0 +1,577 @@ += Import Database By Liquibase By Maven Plugin +:index-group: Import +:jbake-type: page +:jbake-status: not published/unrevised + +This is an example of how to use the Liquibase tool to import a database via the Maven Plugin. + +[discrete] +==== Import Database By Liquibase By Maven Plugin + +To use Liquibase, when building the application, we will use the Maven Plugin to import the database, we will use an in-memory database. + +The SQL instructions defined in the script will be used `V1_0__importSqlScriptTest.sql`: Review Comment: SQL scripts are different in this example - they should match with what we put in the README ########## examples/import-database-flyway-maven/pom.xml: ########## @@ -0,0 +1,198 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with ~ + this work for additional information regarding copyright ownership. ~ The + ASF licenses this file to You 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="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.superbiz</groupId> + <artifactId>import-database-flyway-maven</artifactId> + <version>10.0.0-M3-SNAPSHOT</version> + <packaging>war</packaging> + <name>TomEE :: Examples :: Import Database By Flyway By Maven Plugin</name> + <properties> + <version.jakartaee-api>10.0-M2</version.jakartaee-api> + <version.microprofile>5.0</version.microprofile> Review Comment: tomee 10 is microprofile 6.0. Also generally speaking, don't think this example requires microprofile APIs and I would like to keep these as simple as possible ########## examples/import-database-flyway/src/main/java/org/apache/openejb/assembler/classic/migrate/database/ImportByFlyway.java: ########## @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.openejb.assembler.classic.migrate.database; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import javax.sql.DataSource; + +import org.apache.openejb.OpenEJBRuntimeException; +import org.apache.openejb.assembler.classic.EntityManagerFactoryCallable; +import org.apache.openejb.util.LogCategory; +import org.apache.openejb.util.Logger; +import org.flywaydb.core.Flyway; + +/** + * This class is to be used instead of the ImportSql class, to import scripts + * into the database. + * + * @version $Rev$ $Date$ + */ +public class ImportByFlyway { + private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB, + EntityManagerFactoryCallable.class.getName()); + + public static final String IMPORT_FILE_PREFIX = "V_"; + public static final String IMPORT_FILE_EXTENSION = ".sql"; + + private final DataSource dataSource; + private final String resource; + + public ImportByFlyway(final ClassLoader cl, final String resource, final DataSource ds) { + this.dataSource = ds; + this.resource = resource; + + if (dataSource == null) { + throw new NullPointerException("datasource can't be null"); + } + + } + + public void doImport() { + + try { + + try { + + List<String> sqlFiles = listFilteredFiles(resource, Integer.MAX_VALUE); + + if (Objects.nonNull(sqlFiles)) { + if (sqlFiles.isEmpty()) { + LOGGER.error("The Resource directory for sql files, can not to be empty."); + throw new Exception("The Resource directory for sql files, can not to be empty."); + } + } + + } catch (final IOException e) { + throw new OpenEJBRuntimeException("The Resource directory for sql files, can not to be empty.", e); + } + + Flyway flyway = Flyway.configure().locations("filesystem:src/test/resources").dataSource(dataSource) + .cleanDisabled(false).load(); + + flyway.clean(); + flyway.migrate(); + + } catch (final Exception e) { + LOGGER.error("Can not create a statement, import scripts will be ignored", e); + return; + } + + } + + public void doValidate() { + String selectAllByMail = "SELECT id, description FROM table_test"; + + try (PreparedStatement statement = dataSource.getConnection().prepareStatement(selectAllByMail)) { + + ResultSet resultSet = statement.executeQuery(); + while (resultSet.next()) { + LOGGER.info("id:" + resultSet.getInt("id") + " description:" + resultSet.getString("description")); + + } + } catch (Exception ex) { + LOGGER.error("can't create a statement, import scripts will be ignored", ex); + } + + } Review Comment: this is very specific to the schema used in this example, maybe move this to the test to make it easier for users to just use (= copy) this class? ########## examples/import-database-flyway-maven/pom.xml: ########## @@ -0,0 +1,198 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with ~ + this work for additional information regarding copyright ownership. ~ The + ASF licenses this file to You 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="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.superbiz</groupId> + <artifactId>import-database-flyway-maven</artifactId> + <version>10.0.0-M3-SNAPSHOT</version> + <packaging>war</packaging> + <name>TomEE :: Examples :: Import Database By Flyway By Maven Plugin</name> + <properties> + <version.jakartaee-api>10.0-M2</version.jakartaee-api> + <version.microprofile>5.0</version.microprofile> + <version.arquillian>1.9.1.Final</version.arquillian> + <tomee.version>10.0.0-M3-SNAPSHOT</tomee.version> + </properties> + <dependencies> + <dependency> + <groupId>org.apache.tomee</groupId> + <artifactId>openejb-core</artifactId> + <version>10.0.0-M3-SNAPSHOT</version> + <exclusions> + <exclusion> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.tomee</groupId> + <artifactId>jakartaee-api</artifactId> + <version>${version.jakartaee-api}</version> + <scope>provided</scope> + </dependency> + <dependency> Review Comment: I think all dependencies below this line are not needed for this example ########## examples/import-database-liquibase/.gitignore: ########## @@ -0,0 +1 @@ +/bin/ Review Comment: accidentally committed? ########## examples/import-database-liquibase-maven/changelog.xml: ########## @@ -0,0 +1,9 @@ +<databaseChangeLog Review Comment: missing license header ########## examples/import-database-flyway-maven/pom.xml: ########## @@ -0,0 +1,198 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with ~ + this work for additional information regarding copyright ownership. ~ The + ASF licenses this file to You 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. --> Review Comment: license header format is incorrect ########## examples/import-database-flyway-maven/README.adoc: ########## @@ -0,0 +1,122 @@ += Import Database By Flyway By Maven Plugin +:index-group: Import +:jbake-type: page +:jbake-status: not published/unrevised + +This is an example of how to use the Flyway tool to import a database via the Maven Plugin. + +[discrete] +==== Import Database By Flyway By Maven Plugin + +To use Flyway, when building the application, we will use the Maven Plugin to import the database, we will use an in-memory database. + +The SQL instructions defined in the script will be used `V1_0__import-ImportSqlScriptTest.sql`: + +[source,sql] +---- +CREATE TABLE table_test (ID INT NOT NULL, DESCRIPTION VARCHAR(120) NOT NULL, PRIMARY KEY (ID)); +insert into table_test (id, description) values(1, 'Test 1'); +insert into table_test (id, description) values(2, 'Test 2'); +insert into table_test (id, description) values(3, 'Test 3'); +---- + +== Running the import by the Maven Plugin: + +The following steps must be followed: + +1 - Run the build of the entire project, in the project root directory, tomee: + +[source,bash] +---- +-Pquick -Dsurefire.useFile=false -DdisableXmlReport=true -DuniqueVersion=false -ff -Dassemble -DskipTests -DfailIfNoTests=false clean install Review Comment: afaik examples are standalone, so we should not be telling users to build tomee first ########## examples/import-database-liquibase-maven/pom.xml: ########## @@ -0,0 +1,191 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- ~ Licensed to the Apache Software Foundation (ASF) under one or more Review Comment: license header format looks off again ########## examples/import-database-liquibase/src/main/java/org/apache/openejb/assembler/classic/migrate/database/ImportByLiquibase.java: ########## @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.openejb.assembler.classic.migrate.database; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import javax.sql.DataSource; + +import org.apache.openejb.OpenEJBRuntimeException; +import org.apache.openejb.assembler.classic.EntityManagerFactoryCallable; +import org.apache.openejb.util.LogCategory; +import org.apache.openejb.util.Logger; + +import liquibase.Liquibase; +import liquibase.changelog.ChangeLogHistoryServiceFactory; +import liquibase.database.jvm.HsqlConnection; +import liquibase.exception.DatabaseException; +import liquibase.exception.LiquibaseException; +import liquibase.resource.DirectoryResourceAccessor; +import liquibase.resource.ResourceAccessor; +import liquibase.resource.SearchPathResourceAccessor; + +/** + * This class is to be used instead of the ImportSql class, to import scripts + * into the database. + * + * @version $Rev$ $Date$ + */ +public class ImportByLiquibase { + private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB, + EntityManagerFactoryCallable.class.getName()); + + public static final String IMPORT_FILE_PREFIX = "V_"; + public static final String IMPORT_FILE_EXTENSION = ".sql"; + + private final DataSource dataSource; + private final String resource; + + public ImportByLiquibase(final ClassLoader cl, final String resource, final DataSource ds) { + this.dataSource = ds; + this.resource = resource; + + if (dataSource == null) { + throw new NullPointerException("datasource can't be null"); + } + + } + + @SuppressWarnings("deprecation") + public void doImport() { + + try { + + List<String> sqlFiles = new ArrayList<String>(); + + try { + + sqlFiles = listFilteredFiles(resource, Integer.MAX_VALUE); + + if (Objects.nonNull(sqlFiles)) { + if (sqlFiles.isEmpty()) { + LOGGER.error("The Resource directory for sql files, can not to be empty."); + throw new Exception("The Resource directory for sql files, can not to be empty."); + } + } + + } catch (final IOException e) { + throw new OpenEJBRuntimeException("The Resource directory for sql files, can not to be empty.", e); + } + ChangeLogHistoryServiceFactory.getInstance().resetAll(); + for (String changelogPath : sqlFiles) { + + try (Liquibase liquibase = getLiquibase(changelogPath)) { + liquibase.update("test"); + + } catch (Exception e) { + LOGGER.error("Error running Liquibase changelog", e); + throw new RuntimeException("Error running Liquibase changelog", e); + } + } + + } catch (final Exception e) { + LOGGER.error("Can not create a statement, import scripts will be ignored", e); + return; + } + + } + + public void doValidate() { + String selectAllByMail = "SELECT id, description FROM public.table_test"; + + try (PreparedStatement statement = dataSource.getConnection().prepareStatement(selectAllByMail)) { + ResultSet resultSet = statement.executeQuery(); + while (resultSet.next()) { + LOGGER.info("id:" + resultSet.getInt("id") + " description:" + resultSet.getString("description")); + + } + } catch (Exception ex) { + LOGGER.error("can't create a statement, import scripts will be ignored", ex); + } + + } Review Comment: again, this looks more like test code to me ########## examples/import-database-liquibase/src/test/java/org/apache/openejb/assembler/classic/migrate/database/liquibase/changelog.xml: ########## @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> Review Comment: missing license header -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@tomee.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org