From 911dc6202ffcb7c78b694cd9a62aacec05353f8b Mon Sep 17 00:00:00 2001 From: rhkp Date: Tue, 22 Oct 2024 19:07:06 +0000 Subject: [PATCH] Add kogito-db-migrator-tool and kogito-db-migrator-tool-image packages --- .gitignore | 5 +- .../kogito-db-migrator-tool-image/README.md | 116 ++++++++++++++++++ .../env/index.js | 54 ++++++++ .../kogito-db-migrator-tool-image/install.js | 64 ++++++++++ .../package.json | 44 +++++++ ...tor-kie-kogito-db-migrator-tool-image.yaml | 50 ++++++++ .../module.yaml | 26 ++++ packages/kogito-db-migrator-tool/.gitignore | 4 + packages/kogito-db-migrator-tool/README.md | 31 +++++ packages/kogito-db-migrator-tool/env/index.js | 31 +++++ packages/kogito-db-migrator-tool/install.js | 27 ++++ packages/kogito-db-migrator-tool/package.json | 37 ++++++ packages/kogito-db-migrator-tool/pom.xml | 107 ++++++++++++++++ .../migrator/DBConnectionChecker.java | 66 ++++++++++ .../postgresql/migrator/DBMigrator.java | 94 ++++++++++++++ .../postgresql/migrator/MigrationService.java | 66 ++++++++++ .../src/main/resources/application.properties | 40 ++++++ .../migrator/DBConnectionCheckerTest.java | 72 +++++++++++ .../postgresql/migrator/DBMigratorTest.java | 102 +++++++++++++++ .../migrator/MigrationServiceTest.java | 80 ++++++++++++ packages/maven-base/pom.xml | 20 +++ .../scripts/build-kogito-apps-components.sh | 3 + pnpm-lock.yaml | 44 +++++++ repo/graph.dot | 5 + repo/graph.json | 19 +++ 25 files changed, 1205 insertions(+), 2 deletions(-) create mode 100644 packages/kogito-db-migrator-tool-image/README.md create mode 100644 packages/kogito-db-migrator-tool-image/env/index.js create mode 100644 packages/kogito-db-migrator-tool-image/install.js create mode 100644 packages/kogito-db-migrator-tool-image/package.json create mode 100644 packages/kogito-db-migrator-tool-image/resources/incubator-kie-kogito-db-migrator-tool-image.yaml create mode 100644 packages/kogito-db-migrator-tool-image/resources/modules/kogito-postgres-db-migration-deps/module.yaml create mode 100644 packages/kogito-db-migrator-tool/.gitignore create mode 100644 packages/kogito-db-migrator-tool/README.md create mode 100644 packages/kogito-db-migrator-tool/env/index.js create mode 100644 packages/kogito-db-migrator-tool/install.js create mode 100644 packages/kogito-db-migrator-tool/package.json create mode 100644 packages/kogito-db-migrator-tool/pom.xml create mode 100644 packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/DBConnectionChecker.java create mode 100644 packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/DBMigrator.java create mode 100644 packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/MigrationService.java create mode 100644 packages/kogito-db-migrator-tool/src/main/resources/application.properties create mode 100644 packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/DBConnectionCheckerTest.java create mode 100644 packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/DBMigratorTest.java create mode 100644 packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/MigrationServiceTest.java diff --git a/.gitignore b/.gitignore index 9b7817e7c6f..e68db79f20f 100644 --- a/.gitignore +++ b/.gitignore @@ -347,6 +347,7 @@ turbo.json packages/*/.idea examples/*/.idea - +# Jar files +**/*.jar # Apache RAT check excludes file -!.rat-excludes \ No newline at end of file +!.rat-excludes diff --git a/packages/kogito-db-migrator-tool-image/README.md b/packages/kogito-db-migrator-tool-image/README.md new file mode 100644 index 00000000000..4af61293cea --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/README.md @@ -0,0 +1,116 @@ + + +# Kogito Postgres DB Migrator Tool Image + +This package contains the `Containerfile/Dockerfile` and scripts to build a container image for Kogito Postgres DB Migrator Tool. Details about the Kogito Postgres DB Migrator Tool can be found [here](../kogito-db-migrator-tool/README.md) + +## Additional requirements + +- docker + +## Build + +_NOTE_: Before performing this step, be sure that the Kogito Postgres DB Migrator Tool jar has been built and available for inclusion in the image. + +- Enable the image to be built: + + ```bash + export KIE_TOOLS_BUILD__buildContainerImages=true + ``` + +Run the following in the root folder of the repository to build the package: + +```bash +pnpm -F @kie-tools/kogito-db-migrator-tool-image... build:prod +``` + +- Then check if the image is correctly stored: + + ```bash + docker images + ``` + +## Run + +- Start up a clean container with: + + ```bash + docker run docker.io/apache/incubator-kie-kogito-service-db-migration-postgresql:latest + ``` + +## Customization + +1. Run a container with custom environment variables: + +| NAME | DESCRIPTION | DEFAULT | +| --------------------------------------- | -------------------------------------------------------------------------------- | ----------------------------------------- | +| MIGRATE_DB_DATAINDEX | Set to true if you want to migrate data index database, set to false otherwise | false | +| QUARKUS_DATASOURCE_DATAINDEX_JDBC_URL | Data index database url e.g. jdbc:postgresql://host.docker.internal:5432/di | jdbc:postgresql://localhost:5432/postgres | +| QUARKUS_DATASOURCE_DATAINDEX_USERNAME | Data index database username | postgres | +| QUARKUS_DATASOURCE_DATAINDEX_PASSWORD | Data index database password | postgres | +| QUARKUS_FLYWAY_DATAINDEX_SCHEMAS | Data index database schema | data-index-service | +| MIGRATE_DB_JOBSSERVICE | Set to true if you want to migrate jobs service database, set to false otherwise | false | +| QUARKUS_DATASOURCE_JOBSSERVICE_JDBC_URL | Jobs service database url e.g. jdbc:postgresql://host.docker.internal:5432/js | jdbc:postgresql://localhost:5432/postgres | +| QUARKUS_DATASOURCE_JOBSSERVICE_USERNAME | Jobs service database username | postgres | +| QUARKUS_DATASOURCE_JOBSSERVICE_PASSWORD | Jobs service database password | postgres | +| QUARKUS_FLYWAY_JOBSSERVICE_SCHEMAS | Jobs service database schema | jobs-service | + +### Example + +An example to use diverse environment variables + +```bash + docker run \ + --env MIGRATE_DB_DATAINDEX=true \ + --env QUARKUS_DATASOURCE_DATAINDEX_JDBC_URL= \ + --env QUARKUS_DATASOURCE_DATAINDEX_USERNAME= \ + --env QUARKUS_DATASOURCE_DATAINDEX_PASSWORD= \ + --env QUARKUS_FLYWAY_DATAINDEX_SCHEMAS=data-index-service \ + --env MIGRATE_DB_JOBSSERVICE=true \ + --env QUARKUS_DATASOURCE_JOBSSERVICE_JDBC_URL= \ + --env QUARKUS_DATASOURCE_JOBSSERVICE_USERNAME= \ + --env QUARKUS_DATASOURCE_JOBSSERVICE_PASSWORD= \ + --env QUARKUS_FLYWAY_JOBSSERVICE_SCHEMAS=jobs-service \ + docker.io/apache/incubator-kie-kogito-db-migrator-tool:main +``` + +--- + +Apache KIE (incubating) is an effort undergoing incubation at The Apache Software +Foundation (ASF), sponsored by the name of Apache Incubator. Incubation is +required of all newly accepted projects until a further review indicates that +the infrastructure, communications, and decision making process have stabilized +in a manner consistent with other successful ASF projects. While incubation +status is not necessarily a reflection of the completeness or stability of the +code, it does indicate that the project has yet to be fully endorsed by the ASF. + +Some of the incubating project’s releases may not be fully compliant with ASF +policy. For example, releases may have incomplete or un-reviewed licensing +conditions. What follows is a list of known issues the project is currently +aware of (note that this list, by definition, is likely to be incomplete): + +- Hibernate, an LGPL project, is being used. Hibernate is in the process of + relicensing to ASL v2 +- Some files, particularly test files, and those not supporting comments, may + be missing the ASF Licensing Header + +If you are planning to incorporate this work into your product/project, please +be aware that you will need to conduct a thorough licensing review to determine +the overall implications of including this work. For the current status of this +project through the Apache Incubator visit: +https://incubator.apache.org/projects/kie.html diff --git a/packages/kogito-db-migrator-tool-image/env/index.js b/packages/kogito-db-migrator-tool-image/env/index.js new file mode 100644 index 00000000000..00dc1399d44 --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/env/index.js @@ -0,0 +1,54 @@ +/* + * 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. + */ + +const { varsWithName, composeEnv, getOrDefault } = require("@kie-tools-scripts/build-env"); + +const rootEnv = require("@kie-tools/root-env/env"); +const sonataflowImageCommonEnv = require("@kie-tools/sonataflow-image-common/env"); + +module.exports = composeEnv([rootEnv, sonataflowImageCommonEnv], { + vars: varsWithName({ + KOGITO_DB_MIGRATOR_TOOL_IMAGE__registry: { + default: "docker.io", + description: "The image registry.", + }, + KOGITO_DB_MIGRATOR_TOOL_IMAGE__account: { + default: "apache", + description: "The image registry account.", + }, + KOGITO_DB_MIGRATOR_TOOL_IMAGE__name: { + default: "incubator-kie-kogito-db-migrator-tool", + description: "The image name.", + }, + KOGITO_DB_MIGRATOR_TOOL_IMAGE__buildTag: { + default: rootEnv.env.root.streamName, + description: "The image tag.", + }, + }), + get env() { + return { + kogitoDbMigratorToolImage: { + registry: getOrDefault(this.vars.KOGITO_DB_MIGRATOR_TOOL_IMAGE__registry), + account: getOrDefault(this.vars.KOGITO_DB_MIGRATOR_TOOL_IMAGE__account), + name: getOrDefault(this.vars.KOGITO_DB_MIGRATOR_TOOL_IMAGE__name), + buildTag: getOrDefault(this.vars.KOGITO_DB_MIGRATOR_TOOL_IMAGE__buildTag), + }, + }; + }, +}); diff --git a/packages/kogito-db-migrator-tool-image/install.js b/packages/kogito-db-migrator-tool-image/install.js new file mode 100644 index 00000000000..cec8142a81d --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/install.js @@ -0,0 +1,64 @@ +/* + * 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. + */ + +const { execSync } = require("child_process"); +const fs = require("fs"); + +const { env } = require("./env"); +const path = require("path"); +const pythonVenvDir = path.dirname(require.resolve("@kie-tools/python-venv/package.json")); +const sonataflowImageCommonDir = path.dirname(require.resolve("@kie-tools/sonataflow-image-common/package.json")); +const replaceInFile = require("replace-in-file"); + +const activateCmd = + process.platform === "win32" + ? `${pythonVenvDir}\\venv\\Scripts\\Activate.bat` + : `. ${pythonVenvDir}/venv/bin/activate`; + +execSync( + `${activateCmd} && \ + python3 ${sonataflowImageCommonDir}/resources/scripts/versions_manager.py --bump-to ${env.kogitoDbMigratorToolImage.buildTag} --source-folder ./resources`, + { stdio: "inherit" } +); + +// Find and read the -image.yaml file +const resourcesPath = path.resolve(__dirname, "./resources"); +const files = fs.readdirSync(resourcesPath); +const imageYamlFiles = files.filter((fileName) => fileName.endsWith("image.yaml")); +if (imageYamlFiles.length !== 1) { + throw new Error("There should only be one image.yaml file on ./resources!"); +} +const originalYamlPath = path.join(resourcesPath, imageYamlFiles[0]); +let imageYaml = fs.readFileSync(originalYamlPath, "utf8"); + +const imageUrl = `${env.kogitoDbMigratorToolImage.registry}/${env.kogitoDbMigratorToolImage.account}/${env.kogitoDbMigratorToolImage.name}`; + +// Replace the whole string between quotes ("") with the image name +imageYaml = imageYaml.replace(/(?<=")(.*kie-kogito-db-migrator-tool.*)(?=")/gm, imageUrl); + +// Write file and then rename it to match the image name +fs.writeFileSync(originalYamlPath, imageYaml); +fs.renameSync(originalYamlPath, path.join(resourcesPath, `${env.kogitoDbMigratorToolImage.name}-image.yaml`)); + +// Replace image URL in .feature files +replaceInFile.sync({ + files: ["**/*.feature"], + from: /@docker.io\/apache\/.*/g, + to: `@${imageUrl}`, +}); diff --git a/packages/kogito-db-migrator-tool-image/package.json b/packages/kogito-db-migrator-tool-image/package.json new file mode 100644 index 00000000000..8eac25601cd --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/package.json @@ -0,0 +1,44 @@ +{ + "private": true, + "name": "@kie-tools/kogito-db-migrator-tool-image", + "version": "0.0.0", + "description": "", + "license": "Apache-2.0", + "homepage": "https://github.com/apache/incubator-kie-tools", + "repository": { + "type": "git", + "url": "https://github.com/apache/incubator-kie-tools.git" + }, + "bugs": { + "url": "https://github.com/apache/incubator-kie-tools/issues" + }, + "scripts": { + "build": ". ./node_modules/@kie-tools/python-venv/venv/bin/activate && pnpm copy-assets && pnpm image:build && rimraf target && rimraf dist-tests-e2e", + "build:dev": "run-script-os", + "build:dev:linux": "pnpm build", + "build:dev:win32:darwin": "echo \"Build skipped on Windows and MacOS\"", + "build:prod": "run-script-os", + "build:prod:linux": "pnpm build", + "build:prod:win32:darwin": "echo \"Build skipped on Windows and MacOS\"", + "copy-assets": "run-script-os", + "copy-assets:linux:darwin": "rimraf build && cp -R ./node_modules/@kie-tools/sonataflow-image-common/resources build && cp -R resources/* build && mkdir -p build/modules/kogito-postgres-db-migration-deps/quarkus-app && cp -R ./node_modules/@kie-tools/kogito-db-migrator-tool/target/quarkus-app/* build/modules/kogito-postgres-db-migration-deps/quarkus-app", + "format": "prettier --write . --ignore-path=../../.prettierignore --ignore-path=../../.gitignore", + "image:build": "run-script-os", + "image:build:linux": "pnpm setup:env make -C ./build build", + "image:build:win32:darwin": "echo \"Build skipped on Windows and MacOS\"", + "install": "node install.js && pnpm format", + "setup:env": ". ./node_modules/@kie-tools/python-venv/venv/bin/activate && cross-env KOGITO_IMAGE_REGISTRY=$(build-env kogitoDbMigratorToolImage.registry) KOGITO_IMAGE_REGISTRY_ACCOUNT=$(build-env kogitoDbMigratorToolImage.account) KOGITO_IMAGE_NAME=$(build-env kogitoDbMigratorToolImage.name) KOGITO_IMAGE_TAG=$(build-env kogitoDbMigratorToolImage.buildTag) QUARKUS_PLATFORM_GROUPID=$(build-env kogitoImagesCekitModules.quarkusGroupId) QUARKUS_PLATFORM_VERSION=$(build-env versions.quarkus) KOGITO_VERSION=$(build-env versions.kogito)" + }, + "dependencies": { + "@kie-tools/kogito-db-migrator-tool": "workspace:*", + "@kie-tools/python-venv": "workspace:*", + "@kie-tools/root-env": "workspace:*", + "@kie-tools/sonataflow-image-common": "workspace:*" + }, + "devDependencies": { + "cross-env": "^7.0.3", + "replace-in-file": "^7.1.0", + "rimraf": "^3.0.2", + "run-script-os": "^1.1.6" + } +} diff --git a/packages/kogito-db-migrator-tool-image/resources/incubator-kie-kogito-db-migrator-tool-image.yaml b/packages/kogito-db-migrator-tool-image/resources/incubator-kie-kogito-db-migrator-tool-image.yaml new file mode 100644 index 00000000000..fd29a2f39e5 --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/resources/incubator-kie-kogito-db-migrator-tool-image.yaml @@ -0,0 +1,50 @@ +# +# 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. +# +name: "docker.io/apache/incubator-kie-kogito-db-migrator-tool" +version: "main" +from: registry.access.redhat.com/ubi8/openjdk-17-runtime:1.20 +description: DBMigratorTool image for Data Index and Jobs Service database migration + +labels: + - name: "org.kie.kogito.version" + value: "### SET ME DURING BUILD PROCESS ###" + - name: "maintainer" + value: "Apache KIE " + - name: "io.k8s.description" + value: "Kogito DB Migration creates schemas and tables for Data Index and Jobs Service for PostgreSQL database" + - name: "io.k8s.display-name" + value: "Kogito DB Migration for Data Index and Jobs Service - PostgreSQL" + - name: "io.openshift.tags" + value: "kogito,db-migration" + +modules: + repositories: + - path: modules + install: + - name: org.kie.kogito.system.user + - name: org.kie.kogito.project.versions + - name: org.kie.kogito.postgresql.db-migration-deps + +run: + workdir: "/home/kogito/bin" + entrypoint: + - "java" + cmd: + - "-jar" + - "./quarkus-run.jar" diff --git a/packages/kogito-db-migrator-tool-image/resources/modules/kogito-postgres-db-migration-deps/module.yaml b/packages/kogito-db-migrator-tool-image/resources/modules/kogito-postgres-db-migration-deps/module.yaml new file mode 100644 index 00000000000..3cb2a3a8a85 --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/resources/modules/kogito-postgres-db-migration-deps/module.yaml @@ -0,0 +1,26 @@ +# +# 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. +# +schema_version: 1 +name: org.kie.kogito.postgresql.db-migration-deps +version: "main" +artifacts: + # The following artifact comes from the package kogito-db-migrator-tool during the pnpm build:dev phase + - name: kogito-db-migrator-tool-quarkus-app + path: ./quarkus-app + dest: /home/kogito/bin diff --git a/packages/kogito-db-migrator-tool/.gitignore b/packages/kogito-db-migrator-tool/.gitignore new file mode 100644 index 00000000000..8404612fa0f --- /dev/null +++ b/packages/kogito-db-migrator-tool/.gitignore @@ -0,0 +1,4 @@ +# kogito-db-migrator-tool +src/main/resources/postgresql +src/main/resources/ansi +src/main/resources/h2 \ No newline at end of file diff --git a/packages/kogito-db-migrator-tool/README.md b/packages/kogito-db-migrator-tool/README.md new file mode 100644 index 00000000000..18e28f7eea9 --- /dev/null +++ b/packages/kogito-db-migrator-tool/README.md @@ -0,0 +1,31 @@ +# Kogito Postgres DB Migrator Tool + +This is a Java, Quarkus-based PostgreSQL database migrator application for Data-Index and Jobs Service applications for use by SonataFlow Operator. + +_NOTE_: This postgres database migrator application and its corresponding images are only envisaged to be made use of by SonataFlow Operator, Data Index and Jobs Service internally. Conversely this application is of no use outside the usecases involved with SonataFlow Operator, Data Index and Jobs Service applications. + +--- + +Apache KIE (incubating) is an effort undergoing incubation at The Apache Software +Foundation (ASF), sponsored by the name of Apache Incubator. Incubation is +required of all newly accepted projects until a further review indicates that +the infrastructure, communications, and decision making process have stabilized +in a manner consistent with other successful ASF projects. While incubation +status is not necessarily a reflection of the completeness or stability of the +code, it does indicate that the project has yet to be fully endorsed by the ASF. + +Some of the incubating project’s releases may not be fully compliant with ASF +policy. For example, releases may have incomplete or un-reviewed licensing +conditions. What follows is a list of known issues the project is currently +aware of (note that this list, by definition, is likely to be incomplete): + +- Hibernate, an LGPL project, is being used. Hibernate is in the process of + relicensing to ASL v2 +- Some files, particularly test files, and those not supporting comments, may + be missing the ASF Licensing Header + +If you are planning to incorporate this work into your product/project, please +be aware that you will need to conduct a thorough licensing review to determine +the overall implications of including this work. For the current status of this +project through the Apache Incubator visit: +https://incubator.apache.org/projects/kie.html diff --git a/packages/kogito-db-migrator-tool/env/index.js b/packages/kogito-db-migrator-tool/env/index.js new file mode 100644 index 00000000000..fa7de21dad9 --- /dev/null +++ b/packages/kogito-db-migrator-tool/env/index.js @@ -0,0 +1,31 @@ +/* + * 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. + */ + +const { varsWithName, composeEnv } = require("@kie-tools-scripts/build-env"); + +module.exports = composeEnv([require("@kie-tools/root-env/env"), require("@kie-tools/maven-base/env")], { + vars: varsWithName({}), + get env() { + return { + kogitoDBMigratorTool: { + version: require("../package.json").version, + }, + }; + }, +}); diff --git a/packages/kogito-db-migrator-tool/install.js b/packages/kogito-db-migrator-tool/install.js new file mode 100644 index 00000000000..281ee1f9f2a --- /dev/null +++ b/packages/kogito-db-migrator-tool/install.js @@ -0,0 +1,27 @@ +/* + * 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. + */ +const { env } = require("./env"); +const { setupMavenConfigFile, buildTailFromPackageJsonDependencies } = require("@kie-tools/maven-base"); + +setupMavenConfigFile( + ` + -Drevision=${env.kogitoDBMigratorTool.version} + -Dmaven.repo.local.tail=${buildTailFromPackageJsonDependencies()} +` +); diff --git a/packages/kogito-db-migrator-tool/package.json b/packages/kogito-db-migrator-tool/package.json new file mode 100644 index 00000000000..7ca5e31839b --- /dev/null +++ b/packages/kogito-db-migrator-tool/package.json @@ -0,0 +1,37 @@ +{ + "private": true, + "name": "@kie-tools/kogito-db-migrator-tool", + "version": "0.0.0", + "description": "", + "license": "Apache-2.0", + "homepage": "https://github.com/apache/incubator-kie-tools", + "repository": { + "type": "git", + "url": "https://github.com/apache/incubator-kie-tools.git" + }, + "bugs": { + "url": "https://github.com/apache/incubator-kie-tools/issues" + }, + "scripts": { + "build:dev": "pnpm pre-build && mvn clean install -DskipTests", + "build:prod": "pnpm pre-build && run-script-os", + "build:prod:darwin:linux": "mvn clean install -DskipTests=$(build-env tests.run --not) -Dmaven.test.failure.ignore=$(build-env tests.ignoreFailures)", + "build:prod:win32": "pnpm powershell \"mvn clean install `-DskipTests=$(build-env tests.run --not) `-Dmaven.test.failure.ignore=$(build-env tests.ignoreFailures)\"", + "install": "node install.js", + "pre-build": "mvn dependency:unpack" + }, + "dependencies": { + "@kie-tools/maven-base": "workspace:*" + }, + "devDependencies": { + "@kie-tools/root-env": "workspace:*", + "rimraf": "^3.0.2", + "run-script-os": "^1.1.6" + }, + "kieTools": { + "requiredPreinstalledCliCommands": [ + "java", + "mvn" + ] + } +} diff --git a/packages/kogito-db-migrator-tool/pom.xml b/packages/kogito-db-migrator-tool/pom.xml new file mode 100644 index 00000000000..92cdeaaa5ea --- /dev/null +++ b/packages/kogito-db-migrator-tool/pom.xml @@ -0,0 +1,107 @@ + + + + + org.kie + kie-tools-maven-base + ${revision} + ./node_modules/@kie-tools/maven-base/pom.xml + + + 4.0.0 + org.kie.kogito + kogito-db-migrator-tool + + + + io.quarkus + quarkus-flyway + + + io.quarkus + quarkus-jdbc-postgresql + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-junit5 + test + + + org.mockito + mockito-junit-jupiter + test + + + org.mockito + mockito-core + test + + + + + src/test/java + + + + maven-dependency-plugin + + + + org.kie.kogito + kogito-ddl + ${version.org.kie.kogito} + db-scripts + zip + true + src/main/resources + kogito-ddl-db-scripts.zip + ansi/* + + + + + + io.quarkus + quarkus-maven-plugin + true + + + + build + + + + + + -parameters + + + + + + diff --git a/packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/DBConnectionChecker.java b/packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/DBConnectionChecker.java new file mode 100644 index 00000000000..c9143f850cc --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/DBConnectionChecker.java @@ -0,0 +1,66 @@ +/* + * 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.kie.kogito.postgresql.migrator; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import io.quarkus.logging.Log; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import jakarta.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class DBConnectionChecker { + @ConfigProperty(name = "quarkus.datasource.dataindex.jdbc.url") + String dataIndexDBURL; + + @ConfigProperty(name = "quarkus.datasource.dataindex.username") + String dataIndexDBUserName; + + @ConfigProperty(name = "quarkus.datasource.dataindex.password") + String dataIndexDBPassword; + + @ConfigProperty(name = "quarkus.datasource.jobsservice.jdbc.url") + String jobsServiceDBURL; + + @ConfigProperty(name = "quarkus.datasource.jobsservice.username") + String jobsServiceDBUserName; + + @ConfigProperty(name = "quarkus.datasource.jobsservice.password") + String jobsServiceDBPassword; + + private void checkDBConnection(String dbURL, String dbUser, String dbPassword) throws SQLException { + try (Connection db = DriverManager.getConnection(dbURL, dbUser, dbPassword)) { + Log.infof("Checking DB connection: %s - success", dbURL); + } catch (SQLException sqe) { + Log.infof("Checking DB connection %s- failed", dbURL); + throw sqe; + } + } + + public void checkDataIndexDBConnection() throws SQLException { + checkDBConnection(dataIndexDBURL, dataIndexDBUserName, dataIndexDBPassword); + } + + public void checkJobsServiceDBConnection() throws SQLException { + checkDBConnection(jobsServiceDBURL, jobsServiceDBUserName, jobsServiceDBPassword); + } +} diff --git a/packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/DBMigrator.java b/packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/DBMigrator.java new file mode 100644 index 00000000000..67c3f5e46a9 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/DBMigrator.java @@ -0,0 +1,94 @@ +/* + * 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.kie.kogito.postgresql.migrator; + +import io.quarkus.runtime.Quarkus; +import io.quarkus.runtime.QuarkusApplication; +import io.quarkus.runtime.annotations.QuarkusMain; +import jakarta.inject.Inject; +import io.quarkus.logging.Log; +import org.flywaydb.core.api.FlywayException; + +import java.sql.SQLException; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +@QuarkusMain +public class DBMigrator implements QuarkusApplication { + + private int SUCCESS_DB_MIGRATION = 0; + private int ERR_DATA_INDEX_DB_CONN = -1; + private int ERR_JOBS_SERVICE_DB_CONN = -2; + private int ERR_DATA_INDEX_MIGRATION = -3; + private int ERR_JOBS_SERVICE_MIGRATION = -4; + + @Inject + MigrationService service; + + @Inject + DBConnectionChecker dbConnectionChecker; + + @ConfigProperty(name = "migrate.db.dataindex") + Boolean migrateDataIndex; + + @ConfigProperty(name = "migrate.db.jobsservice") + Boolean migrateJobsService; + + @Override + public int run(String... args) { + if (migrateDataIndex) { + try { + dbConnectionChecker.checkDataIndexDBConnection(); + } catch (SQLException e) { + Log.error( "Error obtaining data index database connection. Cannot proceed, exiting."); + Quarkus.asyncExit(ERR_DATA_INDEX_DB_CONN); + return ERR_DATA_INDEX_DB_CONN; + } + + try{ + service.migrateDataIndex(); + } catch ( FlywayException fe ){ + Log.error( "Error migrating data index database, flyway service exception occured, please check logs."); + Quarkus.asyncExit(ERR_DATA_INDEX_MIGRATION); + return ERR_DATA_INDEX_MIGRATION; + } + } + + if (migrateJobsService) { + try { + dbConnectionChecker.checkJobsServiceDBConnection(); + } catch (SQLException e) { + Log.error( "Error obtaining jobs service database connection. Cannot proceed, exiting."); + Quarkus.asyncExit(ERR_JOBS_SERVICE_DB_CONN); + return ERR_JOBS_SERVICE_DB_CONN; + } + + try{ + service.migrateJobsService(); + } catch ( FlywayException fe ){ + Log.error( "Error migrating jobs service database, flyway service exception occured, please check logs."); + Quarkus.asyncExit(ERR_JOBS_SERVICE_MIGRATION); + return ERR_JOBS_SERVICE_MIGRATION; + } + } + + Quarkus.asyncExit(SUCCESS_DB_MIGRATION); + return SUCCESS_DB_MIGRATION; + } +} \ No newline at end of file diff --git a/packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/MigrationService.java b/packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/MigrationService.java new file mode 100644 index 00000000000..f6a86d704e0 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/MigrationService.java @@ -0,0 +1,66 @@ +/* + * 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.kie.kogito.postgresql.migrator; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import io.quarkus.logging.Log; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.flywaydb.core.Flyway; + +import io.quarkus.flyway.FlywayDataSource; + +@ApplicationScoped +public class MigrationService { + @Inject + @FlywayDataSource("dataindex") + Flyway flywayDataIndex; + + @Inject + @FlywayDataSource("jobsservice") + Flyway flywayJobsService; + + @ConfigProperty(name = "quarkus.flyway.dataindex.clean-at-start") + Boolean cleanDataIndex; + + @ConfigProperty(name = "quarkus.flyway.jobsservice.clean-at-start") + Boolean cleanJobsService; + + private void migrateDB(Flyway flywayService, Boolean clean, String serviceName) { + Log.info("Migrating " + serviceName); + if (clean) { + Log.info("Cleaned the " + serviceName); + flywayService.clean(); + } + flywayService.migrate(); + if (flywayService.info() != null) { + Log.info("Migrated to version " + flywayService.info().current().toString()); + } + } + + public void migrateDataIndex() { + migrateDB(flywayDataIndex, cleanDataIndex, "data-index"); + } + + public void migrateJobsService() { + migrateDB(flywayJobsService, cleanJobsService, "jobs-service"); + } +} \ No newline at end of file diff --git a/packages/kogito-db-migrator-tool/src/main/resources/application.properties b/packages/kogito-db-migrator-tool/src/main/resources/application.properties new file mode 100644 index 00000000000..5f18a83ff28 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/main/resources/application.properties @@ -0,0 +1,40 @@ +# +# 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. +# + +# Data Index data source +migrate.db.dataindex=false +quarkus.datasource.dataindex.db-kind=postgresql +quarkus.datasource.dataindex.username=postgres +quarkus.datasource.dataindex.password=postgres +quarkus.datasource.dataindex.jdbc.url=jdbc:postgresql://localhost:5432/postgres +quarkus.flyway.dataindex.locations=classpath:postgresql/data-index +quarkus.flyway.dataindex.schemas=data-index-service +quarkus.flyway.dataindex.migrate-at-start=false +quarkus.flyway.dataindex.clean-at-start=false + +# Jobs Service data source +migrate.db.jobsservice=false +quarkus.datasource.jobsservice.db-kind=postgresql +quarkus.datasource.jobsservice.username=postgres +quarkus.datasource.jobsservice.password=postgres +quarkus.datasource.jobsservice.jdbc.url=jdbc:postgresql://localhost:5432/postgres +quarkus.flyway.jobsservice.locations=classpath:postgresql/jobs-service +quarkus.flyway.jobsservice.schemas=jobs-service +quarkus.flyway.jobsservice.migrate-at-start=false +quarkus.flyway.jobsservice.clean-at-start=false \ No newline at end of file diff --git a/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/DBConnectionCheckerTest.java b/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/DBConnectionCheckerTest.java new file mode 100644 index 00000000000..77e23482f76 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/DBConnectionCheckerTest.java @@ -0,0 +1,72 @@ +/* + * 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.kie.kogito.postgresql.migrator; + +import io.quarkus.test.Mock; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mockStatic; + +class DBConnectionCheckerTest { + DBConnectionChecker dbConnectionChecker = new DBConnectionChecker(); + + @Mock + static DriverManager driverManager; + + @BeforeAll + static void init() { + mockStatic(DriverManager.class); + } + + @BeforeEach + void setupEach() { + dbConnectionChecker.dataIndexDBURL = "jdbc:postgresql://db-service:5432/di"; + dbConnectionChecker.dataIndexDBUserName = "postgres"; + dbConnectionChecker.dataIndexDBPassword = "postgres"; + + dbConnectionChecker.jobsServiceDBURL = "jdbc:postgresql://db-service:5432/js"; + dbConnectionChecker.jobsServiceDBUserName = "postgres"; + dbConnectionChecker.jobsServiceDBPassword = "postgres"; + } + + @Test + void testCheckDBConnections() throws SQLException { + Mockito.when(driverManager.getConnection(anyString(), anyString(), anyString())).thenReturn(Mockito.mock(Connection.class)); + assertDoesNotThrow(() -> dbConnectionChecker.checkDataIndexDBConnection()); + assertDoesNotThrow(() -> dbConnectionChecker.checkJobsServiceDBConnection()); + } + + @Test + void testCheckDBConnectionsThrowSQLException() throws SQLException { + Mockito.when(driverManager.getConnection(anyString(), anyString(), anyString())).thenThrow(SQLException.class); + assertThrows(SQLException.class, () -> dbConnectionChecker.checkDataIndexDBConnection()); + assertThrows(SQLException.class, () -> dbConnectionChecker.checkJobsServiceDBConnection()); + } +} diff --git a/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/DBMigratorTest.java b/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/DBMigratorTest.java new file mode 100644 index 00000000000..abb0516f364 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/DBMigratorTest.java @@ -0,0 +1,102 @@ +/* + * 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.kie.kogito.postgresql.migrator; + +import io.quarkus.test.Mock; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.sql.SQLException; + +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.times; + +class DBMigratorTest { + + @Mock + MigrationService migrationService; + + @Mock + DBConnectionChecker dbConnectionChecker; + + DBMigrator dbMigrator = new DBMigrator(); + + @BeforeEach + void setupEach() { + migrationService = mock(MigrationService.class); + dbConnectionChecker = mock(DBConnectionChecker.class); + } + + @Test + void testMigratorWithNoMigrations() throws Exception { + dbMigrator.migrateDataIndex = false; + dbMigrator.migrateJobsService = false; + + dbMigrator.run(); + verify(dbConnectionChecker, times(0)).checkDataIndexDBConnection(); + verify(dbConnectionChecker, times(0)).checkJobsServiceDBConnection(); + verify(migrationService, times(0)).migrateDataIndex(); + verify(migrationService, times(0)).migrateJobsService(); + } + + @Test + void testMigratorWithAllMigrations() throws Exception { + dbMigrator.migrateDataIndex = true; + dbMigrator.migrateJobsService = true; + dbMigrator.dbConnectionChecker = dbConnectionChecker; + dbMigrator.service = migrationService; + + dbMigrator.run(); + verify(dbConnectionChecker, times(1)).checkDataIndexDBConnection(); + verify(dbConnectionChecker, times(1)).checkJobsServiceDBConnection(); + verify(migrationService, times(1)).migrateDataIndex(); + verify(migrationService, times(1)).migrateJobsService(); + } + + @Test + void testDataIndexMigrationWithException() throws Exception { + dbMigrator.migrateDataIndex = true; + dbMigrator.migrateJobsService = false; + dbMigrator.dbConnectionChecker = dbConnectionChecker; + dbMigrator.service = migrationService; + + doThrow(new SQLException()).when(dbConnectionChecker).checkDataIndexDBConnection(); + + dbMigrator.run(); + verify(migrationService, times(0)).migrateDataIndex(); + verify(migrationService, times(0)).migrateJobsService(); + } + + @Test + void testJobsServiceWithException() throws Exception { + dbMigrator.migrateDataIndex = false; + dbMigrator.migrateJobsService = true; + dbMigrator.dbConnectionChecker = dbConnectionChecker; + dbMigrator.service = migrationService; + + doThrow(new SQLException()).when(dbConnectionChecker).checkJobsServiceDBConnection(); + + dbMigrator.run(); + verify(migrationService, times(0)).migrateDataIndex(); + verify(migrationService, times(0)).migrateJobsService(); + } +} diff --git a/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/MigrationServiceTest.java b/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/MigrationServiceTest.java new file mode 100644 index 00000000000..b64f23f105d --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/MigrationServiceTest.java @@ -0,0 +1,80 @@ +/* + * 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.kie.kogito.postgresql.migrator; + +import io.quarkus.test.Mock; +import org.flywaydb.core.Flyway; +import org.flywaydb.core.api.output.CleanResult; +import org.flywaydb.core.api.output.MigrateResult; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.times; + +class MigrationServiceTest { + @Mock + Flyway flyway; + + MigrationService migrationService = new MigrationService(); + + @BeforeEach + void setupEach() { + flyway = mock(Flyway.class); + when(flyway.migrate()).thenReturn(new MigrateResult("flywayVersion", "db", "schema")); + when(flyway.clean()).thenReturn(new CleanResult("flywayVersion", "db")); + } + + @Test + void testMigrateDataIndexWithNoClean() { + migrationService.cleanDataIndex = false; + migrationService.flywayDataIndex = flyway; + migrationService.migrateDataIndex(); + verify(flyway, times(1)).migrate(); + } + + @Test + void testMigrateDataIndexWithClean() { + migrationService.cleanDataIndex = true; + migrationService.flywayDataIndex = flyway; + migrationService.migrateDataIndex(); + verify(flyway, times(1)).clean(); + verify(flyway, times(1)).migrate(); + } + + @Test + void testMigrateJobsServiceWithNoClean() { + migrationService.cleanJobsService = false; + migrationService.flywayJobsService = flyway; + migrationService.migrateJobsService(); + verify(flyway, times(1)).migrate(); + } + + @Test + void testMigrateJobsServiceWithClean() { + migrationService.cleanJobsService = true; + migrationService.flywayJobsService = flyway; + migrationService.migrateJobsService(); + verify(flyway, times(1)).clean(); + verify(flyway, times(1)).migrate(); + } +} diff --git a/packages/maven-base/pom.xml b/packages/maven-base/pom.xml index dd1518e52a8..b428d372ea8 100644 --- a/packages/maven-base/pom.xml +++ b/packages/maven-base/pom.xml @@ -140,6 +140,7 @@ 4.11.0 0.4 0.23.0 + 0.8.12 @@ -408,6 +409,25 @@ + + org.jacoco + jacoco-maven-plugin + ${version.jacoco.maven.plugin} + + + + prepare-agent + + + + report + prepare-package + + report + + + + diff --git a/packages/sonataflow-image-common/resources/scripts/build-kogito-apps-components.sh b/packages/sonataflow-image-common/resources/scripts/build-kogito-apps-components.sh index 12a2f50f03c..fc661b7dcdc 100755 --- a/packages/sonataflow-image-common/resources/scripts/build-kogito-apps-components.sh +++ b/packages/sonataflow-image-common/resources/scripts/build-kogito-apps-components.sh @@ -56,6 +56,9 @@ if [ ! -z "${CYPRESS_BINARY_URL}" ]; then fi case ${imageName} in + "incubator-kie-kogito-db-migrator-tool") + contextDir="db-migrator-tool/image" + ;; "incubator-kie-kogito-data-index-ephemeral") contextDir="data-index/data-index-service/data-index-service-inmemory" ;; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4ad259e4e41..a18bdfc6c42 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7403,6 +7403,50 @@ importers: specifier: ^1.1.6 version: 1.1.6 + packages/kogito-db-migrator-tool: + dependencies: + '@kie-tools/maven-base': + specifier: workspace:* + version: link:../maven-base + devDependencies: + '@kie-tools/root-env': + specifier: workspace:* + version: link:../root-env + rimraf: + specifier: ^3.0.2 + version: 3.0.2 + run-script-os: + specifier: ^1.1.6 + version: 1.1.6 + + packages/kogito-db-migrator-tool-image: + dependencies: + '@kie-tools/kogito-db-migrator-tool': + specifier: workspace:* + version: link:../kogito-db-migrator-tool + '@kie-tools/python-venv': + specifier: workspace:* + version: link:../python-venv + '@kie-tools/root-env': + specifier: workspace:* + version: link:../root-env + '@kie-tools/sonataflow-image-common': + specifier: workspace:* + version: link:../sonataflow-image-common + devDependencies: + cross-env: + specifier: ^7.0.3 + version: 7.0.3 + replace-in-file: + specifier: ^7.1.0 + version: 7.1.0 + rimraf: + specifier: ^3.0.2 + version: 3.0.2 + run-script-os: + specifier: ^1.1.6 + version: 1.1.6 + packages/kogito-jit-runner-image: devDependencies: '@kie-tools/maven-base': diff --git a/repo/graph.dot b/repo/graph.dot index f8a1a25983c..dab90395800 100644 --- a/repo/graph.dot +++ b/repo/graph.dot @@ -150,6 +150,8 @@ digraph G { "@kie-tools/sonataflow-image-common" [ color = "black", fontcolor = "black", style = "dashed, rounded" ]; "@kie/kogito-data-index-ephemeral-image" [ color = "black", fontcolor = "black", style = "dashed, rounded" ]; "@kie/kogito-data-index-postgresql-image" [ color = "black", fontcolor = "black", style = "dashed, rounded" ]; + "@kie-tools/kogito-db-migrator-tool" [ color = "black", fontcolor = "black", style = "dashed, rounded" ]; + "@kie-tools/kogito-db-migrator-tool-image" [ color = "black", fontcolor = "black", style = "dashed, rounded" ]; "@kie/kogito-jit-runner-image" [ color = "black", fontcolor = "black", style = "dashed, rounded" ]; "@kie/kogito-jobs-service-allinone-image" [ color = "black", fontcolor = "black", style = "dashed, rounded" ]; "@kie/kogito-jobs-service-ephemeral-image" [ color = "black", fontcolor = "black", style = "dashed, rounded" ]; @@ -471,6 +473,9 @@ digraph G { "@kie/kogito-data-index-ephemeral-image" -> "@kie-tools/sonataflow-image-common" [ style = "dashed", color = "black" ]; "@kie/kogito-data-index-postgresql-image" -> "@kie-tools/maven-base" [ style = "dashed", color = "black" ]; "@kie/kogito-data-index-postgresql-image" -> "@kie-tools/sonataflow-image-common" [ style = "dashed", color = "black" ]; + "@kie-tools/kogito-db-migrator-tool" -> "@kie-tools/maven-base" [ style = "solid", color = "black" ]; + "@kie-tools/kogito-db-migrator-tool-image" -> "@kie-tools/kogito-db-migrator-tool" [ style = "solid", color = "black" ]; + "@kie-tools/kogito-db-migrator-tool-image" -> "@kie-tools/sonataflow-image-common" [ style = "solid", color = "black" ]; "@kie/kogito-jit-runner-image" -> "@kie-tools/maven-base" [ style = "dashed", color = "black" ]; "@kie/kogito-jit-runner-image" -> "@kie-tools/sonataflow-image-common" [ style = "dashed", color = "black" ]; "@kie/kogito-jobs-service-allinone-image" -> "@kie-tools/maven-base" [ style = "dashed", color = "black" ]; diff --git a/repo/graph.json b/repo/graph.json index 5a4b9408bc6..55c4a9b4b5e 100644 --- a/repo/graph.json +++ b/repo/graph.json @@ -192,6 +192,8 @@ { "id": "@kie/kogito-base-builder-image" }, { "id": "@kie-tools/sonataflow-image-common" }, { "id": "@kie-tools/python-venv" }, + { "id": "@kie-tools/kogito-db-migrator-tool" }, + { "id": "@kie-tools/kogito-db-migrator-tool-image" }, { "id": "@kie/kogito-jit-runner-image" }, { "id": "@kie/kogito-jobs-service-allinone-image" }, { "id": "@kie-tools/unitables" }, @@ -2102,6 +2104,21 @@ "target": "@kie-tools/root-env", "weight": 1 }, + { + "source": "@kie-tools/kogito-db-migrator-tool", + "target": "@kie-tools/maven-base", + "weight": 1 + }, + { + "source": "@kie-tools/kogito-db-migrator-tool-image", + "target": "@kie-tools/kogito-db-migrator-tool", + "weight": 1 + }, + { + "source": "@kie-tools/kogito-db-migrator-tool-image", + "target": "@kie-tools/sonataflow-image-common", + "weight": 1 + }, { "source": "@kie/kogito-jit-runner-image", "target": "@kie-tools/maven-base", @@ -2831,6 +2848,8 @@ ["@kie/kogito-base-builder-image", "packages/kogito-base-builder-image"], ["@kie/kogito-data-index-ephemeral-image", "packages/kogito-data-index-ephemeral-image"], ["@kie/kogito-data-index-postgresql-image", "packages/kogito-data-index-postgresql-image"], + ["@kie-tools/kogito-db-migrator-tool", "packages/kogito-db-migrator-tool"], + ["@kie-tools/kogito-db-migrator-tool-image", "packages/kogito-db-migrator-tool-image"], ["@kie/kogito-jit-runner-image", "packages/kogito-jit-runner-image"], ["@kie/kogito-jobs-service-allinone-image", "packages/kogito-jobs-service-allinone-image"], ["@kie/kogito-jobs-service-ephemeral-image", "packages/kogito-jobs-service-ephemeral-image"],