From 987bb3e3dd9198d354399acab2ab63001a323bb6 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 | 3 + .../kogito-db-migrator-tool-image/README.md | 47 ++++ .../build-container-image.sh | 40 ++++ .../cekit/image.yaml | 45 ++++ .../kogito-postgres-db-migration-deps/install | 32 +++ .../migration.sh | 36 +++ .../module.yaml | 30 +++ .../env/index.js | 20 ++ .../kogito-db-migrator-tool-image/install.js | 20 ++ .../package.json | 26 +++ .../kogito-db-migrator-tool/.dockerignore | 5 + packages/kogito-db-migrator-tool/.gitignore | 44 ++++ packages/kogito-db-migrator-tool/README.md | 25 ++ .../build-db-migrator-jar.sh | 45 ++++ packages/kogito-db-migrator-tool/env/index.js | 31 +++ .../get-kogito-ddl-scripts.sh | 64 ++++++ packages/kogito-db-migrator-tool/install.js | 35 +++ packages/kogito-db-migrator-tool/package.json | 33 +++ packages/kogito-db-migrator-tool/pom.xml | 213 ++++++++++++++++++ .../src/main/cekit/image.yaml | 45 ++++ .../kogito-postgres-db-migration-deps/install | 32 +++ .../migration.sh | 36 +++ .../module.yaml | 30 +++ .../migrator/DBConnectionChecker.java | 66 ++++++ .../postgresql/migrator/DBMigrator.java | 73 ++++++ .../postgresql/migrator/MigrationService.java | 66 ++++++ .../src/main/resources/application.properties | 40 ++++ .../migrator/DBConnectionCheckerTest.java | 68 ++++++ .../postgresql/migrator/DBMigratorTest.java | 94 ++++++++ .../migrator/MigrationServiceTest.java | 69 ++++++ 30 files changed, 1413 insertions(+) create mode 100644 packages/kogito-db-migrator-tool-image/README.md create mode 100755 packages/kogito-db-migrator-tool-image/build-container-image.sh create mode 100644 packages/kogito-db-migrator-tool-image/cekit/image.yaml create mode 100644 packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/install create mode 100644 packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/migration.sh create mode 100644 packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/module.yaml 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/.dockerignore create mode 100644 packages/kogito-db-migrator-tool/.gitignore create mode 100644 packages/kogito-db-migrator-tool/README.md create mode 100755 packages/kogito-db-migrator-tool/build-db-migrator-jar.sh create mode 100644 packages/kogito-db-migrator-tool/env/index.js create mode 100755 packages/kogito-db-migrator-tool/get-kogito-ddl-scripts.sh 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/cekit/image.yaml create mode 100644 packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/install create mode 100644 packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/migration.sh create mode 100644 packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/module.yaml 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 0fa740642fb..750f639a452 100644 --- a/.gitignore +++ b/.gitignore @@ -346,3 +346,6 @@ turbo.json # IntelliJ projects should be on _intellij-projects folder, never on specific packages. packages/*/.idea examples/*/.idea + +# Jar files +**/*.jar \ No newline at end of file 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..db325462e39 --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/README.md @@ -0,0 +1,47 @@ +# kogito-db-migrator-tool-image + +This is a cekit based image builder for the database migrator application. Details about the database migrator application are [here](../kogito-db-migrator-tool/README.md) + +## Build and Run container image locally + +You can build the cekit build of container image by using the provided image builder shell script + +```shell +./build-container-image.sh +``` + +_NOTE_: Before performing this step, be sure that the database migrator application jar has been built and available for inclusion in the image. + +Ensure the script completes without errors. + +Assuming you have a Postgres database running locally, e.g., a `di` database for data index and a `js` database for jobs service, you can run the image with the following command. Substitute appropriate values: + +```shell +podman 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=dataindex \ +--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=jobsservice \ +docker.io/apache/incubator-kie-kogito-service-db-migration-postgresql:999-SNAPSHOT +``` + +### 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 | dataindex | +| 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 | jobsservice | diff --git a/packages/kogito-db-migrator-tool-image/build-container-image.sh b/packages/kogito-db-migrator-tool-image/build-container-image.sh new file mode 100755 index 00000000000..d4bac957570 --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/build-container-image.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# +# 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. +# + +set -x +set -e + +CEKIT_BUILDER=docker +SOURCE_FILE=/tmp/kogito-db-migrator-tool/sonataflow-db-migrator-0.0.0-runner.jar +TARGET_FILE=./cekit/modules/kogito-postgres-db-migration-deps/sonataflow-db-migrator-runner.jar + +if [ -f "$SOURCE_FILE" ]; then + echo "The file: $SOURCE_FILE found and will be used to build image." + rm -f $TARGET_FILE + cp $SOURCE_FILE $TARGET_FILE + + # Build the container image + cd ./cekit || exit + cekit -v build "$CEKIT_BUILDER" + rm -f $TARGET_FILE +else + echo "The file: $SOURCE_FILE not found. Please build kogito-db-migrator-tool package first before building the image." + exit 1 +fi \ No newline at end of file diff --git a/packages/kogito-db-migrator-tool-image/cekit/image.yaml b/packages/kogito-db-migrator-tool-image/cekit/image.yaml new file mode 100644 index 00000000000..a2517ee0154 --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/cekit/image.yaml @@ -0,0 +1,45 @@ +# +# 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-service-db-migration-postgresql" +version: "999-SNAPSHOT" +from: registry.access.redhat.com/ubi8/openjdk-17-runtime:1.19 +description: Flyway image for DI/JS database migration + +labels: + - name: "org.kie.kogito.version" + value: "999-SNAPSHOT" + - 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: kogito-postgres-db-migration-deps + +run: + workdir: "/home/kogito" + entrypoint: + - "/home/kogito/migration.sh" diff --git a/packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/install b/packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/install new file mode 100644 index 00000000000..5fa1a3e0e86 --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/install @@ -0,0 +1,32 @@ +#!/bin/sh +# +# 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. +# + +set -x +set -e + +useradd -m kogito + +cd /home/kogito || exit + +chgrp -R 0 /home/kogito +chown -R 0 /home/kogito +chmod -R g=u /home/kogito + +chmod +x /home/kogito/migration.sh \ No newline at end of file diff --git a/packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/migration.sh b/packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/migration.sh new file mode 100644 index 00000000000..fe41d336d5b --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/migration.sh @@ -0,0 +1,36 @@ +#!/bin/bash +# +# 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. +# + +set -x +set -e + +OPERATOR_VERSION=999.0.0 + +for arg in "$@"; do + case "$arg" in + OPERATOR_VERSION=*) OPERATOR_VERSION="${arg#*=}" ;; + esac +done + +cd /home/kogito/ + +mv sonataflow-db-migrator-runner.jar sonataflow-db-migrator-"$OPERATOR_VERSION"-runner.jar + +java -jar sonataflow-db-migrator-"$OPERATOR_VERSION"-runner.jar \ No newline at end of file diff --git a/packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/module.yaml b/packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/module.yaml new file mode 100644 index 00000000000..0605dacc689 --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/cekit/modules/kogito-postgres-db-migration-deps/module.yaml @@ -0,0 +1,30 @@ +# +# 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: kogito-postgres-db-migration-deps +version: "1.0" +artifacts: + - name: migration.sh + path: migration.sh + dest: /home/kogito + - name: sonataflow-db-migrator-runner.jar + path: sonataflow-db-migrator-runner.jar + dest: /home/kogito +execute: + - script: install 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..c3fe8a9b1e0 --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/env/index.js @@ -0,0 +1,20 @@ +/* + * 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. + */ + +console.log("index.js"); 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..8529104bcd8 --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/install.js @@ -0,0 +1,20 @@ +/* + * 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. + */ + +console.log("install.js"); 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..0580e059efb --- /dev/null +++ b/packages/kogito-db-migrator-tool-image/package.json @@ -0,0 +1,26 @@ +{ + "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": "./build-container-image.sh", + "build:dev": "pnpm build", + "build:prod": "pnpm build", + "install": "node install.js" + }, + "dependencies": { + "@kie-tools/kogito-db-migrator-tool": "workspace:*", + "@kie-tools/python-venv": "workspace:*", + "@kie-tools/root-env": "workspace:*" + } +} diff --git a/packages/kogito-db-migrator-tool/.dockerignore b/packages/kogito-db-migrator-tool/.dockerignore new file mode 100644 index 00000000000..94810d006e7 --- /dev/null +++ b/packages/kogito-db-migrator-tool/.dockerignore @@ -0,0 +1,5 @@ +* +!target/*-runner +!target/*-runner.jar +!target/lib/* +!target/quarkus-app/* \ No newline at end of file diff --git a/packages/kogito-db-migrator-tool/.gitignore b/packages/kogito-db-migrator-tool/.gitignore new file mode 100644 index 00000000000..33b2771547b --- /dev/null +++ b/packages/kogito-db-migrator-tool/.gitignore @@ -0,0 +1,44 @@ +#Maven +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +release.properties +.flattened-pom.xml + +# Eclipse +.project +.classpath +.settings/ +bin/ + +# IntelliJ +.idea +*.ipr +*.iml +*.iws + +# NetBeans +nb-configuration.xml + +# Visual Studio Code +.vscode +.factorypath + +# OSX +.DS_Store + +# Vim +*.swp +*.swo + +# patch +*.orig +*.rej + +# Local environment +.env + +# Plugin directory +/.quarkus/cli/plugins/ +*.iml \ 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..229ce15d3be --- /dev/null +++ b/packages/kogito-db-migrator-tool/README.md @@ -0,0 +1,25 @@ +# kogito-db-migrator + +This is a java quarkus based postgres database migrator application for Sonataflow 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. + +## Running the application in dev mode + +Though you can run the application locally in dev mode but it is advisable to run this application as a container image. +The primary reason not to run as standalone application in dev mode, is that by default there are no DDL migration files included in the source. The script `get-kogito-ddl-scripts.sh` can be used to download the needed postgres DDL files into the application. + +You can run your application in dev mode that enables live coding using: + +```shell script +./get-kogito-ddl-scripts.sh +./mvnw compile quarkus:dev +``` + +## Build jar file + +You can build the jar file with the script `build-db-migrator-jar.sh` which places the jar into /tmp/ kogito-db-migrator-tool directory for use by the corresponding image builder in package kogito-db-migrator-tool-image later, which can be found [here](../kogito-db-migrator-tool-image/README.md). + +```shell +./build-db-migrator-jar.sh +``` diff --git a/packages/kogito-db-migrator-tool/build-db-migrator-jar.sh b/packages/kogito-db-migrator-tool/build-db-migrator-jar.sh new file mode 100755 index 00000000000..6be70586900 --- /dev/null +++ b/packages/kogito-db-migrator-tool/build-db-migrator-jar.sh @@ -0,0 +1,45 @@ +#!/bin/sh +# +# 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. +# + +set -x +set -e + +# Directory where the DB Migrator Jar will be dropped +OUTPUT_DIR=/tmp/kogito-db-migrator-tool +TARGET_JAR_FILE=./target/sonataflow-db-migrator-0.0.0-runner.jar +DDL_DIR=./src/main/resources/postgresql + +rm -rf $OUTPUT_DIR +mkdir -p $OUTPUT_DIR + +./get-kogito-ddl-scripts.sh + +if [ "$1" = "true" ]; then + echo "Using dev version:" + mvn clean install -DskipTests -Dquarkus.package.jar.type=uber-jar +else + echo "Using prod version:" + mvn clean install -DskipTests=$(build-env tests.run --not) -Dmaven.test.failure.ignore=$(build-env tests.ignoreFailures) -Dquarkus.package.jar.type=uber-jar +fi + +cp $TARGET_JAR_FILE $OUTPUT_DIR + +# As the jar is built and ddl files are included, cleanup by deleting the ddl files +rm -rf $DDL_DIR 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..42ad5024d52 --- /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")], { + vars: varsWithName({}), + get env() { + return { + devDeploymentQuarkusApp: { + version: require("../package.json").version, + }, + }; + }, +}); diff --git a/packages/kogito-db-migrator-tool/get-kogito-ddl-scripts.sh b/packages/kogito-db-migrator-tool/get-kogito-ddl-scripts.sh new file mode 100755 index 00000000000..cc88b91eb0b --- /dev/null +++ b/packages/kogito-db-migrator-tool/get-kogito-ddl-scripts.sh @@ -0,0 +1,64 @@ +#!/bin/sh +# +# 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. +# + +set -x +set -e + +# Script variables with default values. These values will be updated by hack/bump-version.sh, don't change it. +DDL_VERSION=10.0.999-SNAPSHOT +DDL_FILE=kogito-ddl-10.0.999-20241018.012042-73-db-scripts.zip +DDL_BASE_URL=https://repository.apache.org/content/groups/snapshots/org/kie/kogito/kogito-ddl +TMP_DIR=./tmp +RESOURCES_DIR=src/main/resources +POSTGRESQL_DDL_DIR=postgresql + +# cleanup temporary files +cleanup () { + echo "Cleaning up" + rm -rf ${RESOURCES_DIR}"/"${POSTGRESQL_DDL_DIR} + rm -rf $TMP_DIR +} + +# Use specific variable values, if passed +for arg in "$@"; do + case "$arg" in + DDL_VERSION=*) DDL_VERSION="${arg#*=}" ;; + DDL_FILE=*) DDL_FILE="${arg#*=}" ;; + DDL_BASE_URL=*) DDL_BASE_URL="${arg#*=}" ;; + esac +done + +DDL_URL=$DDL_BASE_URL/$DDL_VERSION/$DDL_FILE + +printf "Variables being used for the DDL: \n DDL_VERSION: %s, \n DDL_FILE: %s, \n DDL_BASE_URL: %s \n\n" "$DDL_VERSION" "$DDL_FILE" "$DDL_BASE_URL" + +# Start with cleanup +cleanup + +# Get Data Index/ Jobs Service DDL Files +mkdir -p $TMP_DIR +# Change the variables below, as needed +wget "$DDL_URL" +mv "$DDL_FILE" $TMP_DIR +cd $TMP_DIR || exit +unzip "$DDL_FILE" +mv "./"${POSTGRESQL_DDL_DIR} "../"${RESOURCES_DIR} +cd .. || exit +rm -rf $TMP_DIR diff --git a/packages/kogito-db-migrator-tool/install.js b/packages/kogito-db-migrator-tool/install.js new file mode 100644 index 00000000000..61f0e221a7b --- /dev/null +++ b/packages/kogito-db-migrator-tool/install.js @@ -0,0 +1,35 @@ +/* + * 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, installMvnw, buildTailFromPackageJsonDependencies } = require("@kie-tools/maven-base"); + +setupMavenConfigFile( + ` + --batch-mode + -Dstyle.color=always + -Drevision=${env.devDeploymentQuarkusApp.version} + -Dversion.quarkus=${env.versions.quarkus} + -Dversion.org.kie.kogito=${env.versions.kogito} + -Dmaven.repo.local.tail=${buildTailFromPackageJsonDependencies()} +`, + { ignoreDefault: true } // Can't have special configuration that only works inside this repo. +); + +installMvnw(); diff --git a/packages/kogito-db-migrator-tool/package.json b/packages/kogito-db-migrator-tool/package.json new file mode 100644 index 00000000000..8dc4ca795f5 --- /dev/null +++ b/packages/kogito-db-migrator-tool/package.json @@ -0,0 +1,33 @@ +{ + "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": "./build-db-migrator-jar.sh true", + "build:prod": "./build-db-migrator-jar.sh false", + "install": "node install.js" + }, + "dependencies": { + "@kie-tools/maven-base": "workspace:*" + }, + "devDependencies": { + "@kie-tools/root-env": "workspace:*", + "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..25ae0882008 --- /dev/null +++ b/packages/kogito-db-migrator-tool/pom.xml @@ -0,0 +1,213 @@ + + + + + org.kie + kie-tools-maven-base + ${revision} + ./node_modules/@kie-tools/maven-base/pom.xml + + + 4.0.0 + org.kie.kogito + sonataflow-db-migrator + + 2024-11-01T00:00:00Z + 3.13.0 + UTF-8 + UTF-8 + quarkus-bom + io.quarkus.platform + 3.13.0 + true + 3.2.5 + 0.8.12 + 1.19.0 + + + + + + ${quarkus.platform.group-id} + ${quarkus.platform.artifact-id} + ${quarkus.platform.version} + pom + import + + + + + + + io.quarkus + quarkus-flyway + + + io.quarkus + quarkus-jdbc-postgresql + + + org.flywaydb + flyway-database-postgresql + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-junit5 + test + + + org.mockito + mockito-junit-jupiter + test + + + org.mockito + mockito-core + test + + + com.github.stefanbirkner + system-rules + ${systemrules.version} + test + + + org.junit.jupiter + junit-jupiter + test + + + + + src/test/java + + + ${quarkus.platform.group-id} + quarkus-maven-plugin + ${quarkus.platform.version} + true + + + + build + generate-code + generate-code-tests + native-image-agent + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${compiler-plugin.version} + + + -parameters + + + + + maven-surefire-plugin + ${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + ${maven.home} + + + + + maven-failsafe-plugin + ${surefire-plugin.version} + + + + integration-test + verify + + + + + + ${project.build.directory}/${project.build.finalName}-runner + + org.jboss.logmanager.LogManager + ${maven.home} + + + + + org.jacoco + jacoco-maven-plugin + ${jacoco.version} + + + + prepare-agent + + + + report + prepare-package + + report + + + + + + + + + + native + + + native + + + + false + true + + + + + + + apache + Apache Maven + default + https://repo.maven.apache.org/maven2/ + + false + + + + diff --git a/packages/kogito-db-migrator-tool/src/main/cekit/image.yaml b/packages/kogito-db-migrator-tool/src/main/cekit/image.yaml new file mode 100644 index 00000000000..a2517ee0154 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/main/cekit/image.yaml @@ -0,0 +1,45 @@ +# +# 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-service-db-migration-postgresql" +version: "999-SNAPSHOT" +from: registry.access.redhat.com/ubi8/openjdk-17-runtime:1.19 +description: Flyway image for DI/JS database migration + +labels: + - name: "org.kie.kogito.version" + value: "999-SNAPSHOT" + - 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: kogito-postgres-db-migration-deps + +run: + workdir: "/home/kogito" + entrypoint: + - "/home/kogito/migration.sh" diff --git a/packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/install b/packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/install new file mode 100644 index 00000000000..5fa1a3e0e86 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/install @@ -0,0 +1,32 @@ +#!/bin/sh +# +# 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. +# + +set -x +set -e + +useradd -m kogito + +cd /home/kogito || exit + +chgrp -R 0 /home/kogito +chown -R 0 /home/kogito +chmod -R g=u /home/kogito + +chmod +x /home/kogito/migration.sh \ No newline at end of file diff --git a/packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/migration.sh b/packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/migration.sh new file mode 100644 index 00000000000..fe41d336d5b --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/migration.sh @@ -0,0 +1,36 @@ +#!/bin/bash +# +# 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. +# + +set -x +set -e + +OPERATOR_VERSION=999.0.0 + +for arg in "$@"; do + case "$arg" in + OPERATOR_VERSION=*) OPERATOR_VERSION="${arg#*=}" ;; + esac +done + +cd /home/kogito/ + +mv sonataflow-db-migrator-runner.jar sonataflow-db-migrator-"$OPERATOR_VERSION"-runner.jar + +java -jar sonataflow-db-migrator-"$OPERATOR_VERSION"-runner.jar \ No newline at end of file diff --git a/packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/module.yaml b/packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/module.yaml new file mode 100644 index 00000000000..0605dacc689 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/main/cekit/modules/kogito-postgres-db-migration-deps/module.yaml @@ -0,0 +1,30 @@ +# +# 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: kogito-postgres-db-migration-deps +version: "1.0" +artifacts: + - name: migration.sh + path: migration.sh + dest: /home/kogito + - name: sonataflow-db-migrator-runner.jar + path: sonataflow-db-migrator-runner.jar + dest: /home/kogito +execute: + - script: install 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..bb69266f4e0 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/main/java/org/kie/kogito/postgresql/migrator/DBMigrator.java @@ -0,0 +1,73 @@ +/* + * 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 java.sql.SQLException; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +@QuarkusMain +public class DBMigrator implements QuarkusApplication { + + @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(-1); + return -1; + } + service.migrateDataIndex(); + } + + if (migrateJobsService) { + try { + dbConnectionChecker.checkJobsServiceDBConnection(); + } catch (SQLException e) { + Log.error( "Error obtaining jobs service database connection. Cannot proceed, exiting."); + Quarkus.asyncExit(-2); + return -2; + } + service.migrateJobsService(); + } + + Quarkus.asyncExit(0); + return 0; + } +} \ 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..233539cc35a --- /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(flywayService.info().current().getVersion().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..59b8c2382a5 --- /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=dataindex +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=jobsservice +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..935eed5c382 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/DBConnectionCheckerTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2024 Apache Software Foundation (ASF) + * + * Licensed 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.*; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mockStatic; + +public class DBConnectionCheckerTest { + DBConnectionChecker dbConnectionChecker = new DBConnectionChecker(); + + @Mock + static DriverManager driverManager; + + @BeforeAll + public static void init() { + mockStatic(DriverManager.class); + } + + @BeforeEach + public 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 + public void testCheckDBConnections() throws SQLException { + Mockito.when(driverManager.getConnection(anyString(), anyString(), anyString())).thenReturn(Mockito.mock(Connection.class)); + assertDoesNotThrow(() -> dbConnectionChecker.checkDataIndexDBConnection()); + assertDoesNotThrow(() -> dbConnectionChecker.checkJobsServiceDBConnection()); + } + + @Test + public 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..3ee9c6e0555 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/DBMigratorTest.java @@ -0,0 +1,94 @@ +/* + * Copyright 2024 Apache Software Foundation (ASF) + * + * Licensed 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.Rule; +import org.junit.contrib.java.lang.system.ExpectedSystemExit; +import org.junit.jupiter.api.BeforeAll; +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; + +public class DBMigratorTest { + @Rule + public final ExpectedSystemExit exitRule = ExpectedSystemExit.none(); + + @Mock + MigrationService migrationService; + + @Mock + DBConnectionChecker dbConnectionChecker; + + DBMigrator dbMigrator = new DBMigrator(); + + @BeforeEach + public void setupEach() { + migrationService = mock(MigrationService.class); + dbConnectionChecker = mock(DBConnectionChecker.class); + } + + @Test + public void testMigratorWithNoMigrations() throws Exception { + dbMigrator.migrateDataIndex = false; + dbMigrator.migrateJobsService = false; + + exitRule.expectSystemExitWithStatus(0); + dbMigrator.run(); + } + + @Test + public void testMigratorWithAllMigrations() throws Exception { + dbMigrator.migrateDataIndex = true; + dbMigrator.migrateJobsService = true; + dbMigrator.dbConnectionChecker = dbConnectionChecker; + dbMigrator.service = migrationService; + + exitRule.expectSystemExitWithStatus(0); + dbMigrator.run(); + } + + @Test + public void testDataIndexMigrationWithException() throws Exception { + dbMigrator.migrateDataIndex = true; + dbMigrator.migrateJobsService = false; + dbMigrator.dbConnectionChecker = dbConnectionChecker; + dbMigrator.service = migrationService; + + doThrow(new SQLException()).when(dbConnectionChecker).checkDataIndexDBConnection(); + + exitRule.expectSystemExitWithStatus(-1); + dbMigrator.run(); + } + + @Test + public void testJobsServiceWithException() throws Exception { + dbMigrator.migrateDataIndex = false; + dbMigrator.migrateJobsService = true; + dbMigrator.dbConnectionChecker = dbConnectionChecker; + dbMigrator.service = migrationService; + + doThrow(new SQLException()).when(dbConnectionChecker).checkJobsServiceDBConnection(); + + exitRule.expectSystemExitWithStatus(-2); + dbMigrator.run(); + } +} 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..0c4f497cc81 --- /dev/null +++ b/packages/kogito-db-migrator-tool/src/test/java/org/kie/kogito/postgresql/migrator/MigrationServiceTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2024 Apache Software Foundation (ASF) + * + * Licensed 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.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.mockito.Mockito.*; + +public class MigrationServiceTest { + @Mock + Flyway flyway; + + MigrationService migrationService = new MigrationService(); + + @BeforeEach + public void setupEach() { + flyway = mock(Flyway.class); + when(flyway.migrate()).thenReturn(new MigrateResult("flywayVersion", "db", "schema", "postgres")); + when(flyway.clean()).thenReturn(new CleanResult("flywayVersion", "db")); + } + + @Test + public void testMigrateDataIndexWithNoClean() { + migrationService.cleanDataIndex = false; + migrationService.flywayDataIndex = flyway; + migrationService.migrateDataIndex(); + } + + @Test + public void testMigrateDataIndexWithClean() { + migrationService.cleanDataIndex = true; + migrationService.flywayDataIndex = flyway; + migrationService.migrateDataIndex(); + } + + @Test + public void testMigrateJobsServiceWithNoClean() { + migrationService.cleanJobsService = false; + migrationService.flywayJobsService = flyway; + migrationService.migrateJobsService(); + } + + @Test + public void testMigrateJobsServiceWithClean() { + migrationService.cleanJobsService = true; + migrationService.flywayJobsService = flyway; + migrationService.migrateJobsService(); + } +}