diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 97bf5af3..8ec502d7 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -3,7 +3,8 @@ on: [pull_request] jobs: tests: - runs-on: [ ubuntu-latest ] + runs-on: ubuntu-latest + env: DB_USER: my_username DB_PASSWORD: my_password @@ -17,6 +18,7 @@ jobs: AUTHZ_FGA_API_TOKEN: test_token AUTHZ_FGA_STORE_NAME: heureka_store AUTHZ_MODEL_FILE_PATH: ./../internal/openfga/model/model.fga + services: mariadb: image: mariadb:latest @@ -28,27 +30,71 @@ jobs: MARIADB_DATABASE: heureka MARIADB_ROOT_PASSWORD: my_password options: --health-cmd="healthcheck.sh --connect --innodb_initialized" --health-interval=10s --health-timeout=5s --health-retries=3 + valkey: image: valkey/valkey:7.2 ports: - "6379:6379" + steps: - uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - name: Detect migration changes + id: migration_check + run: | + if make BASE_REF=${{ github.base_ref }} migration-change-detected; then + echo "changed=true" >> $GITHUB_OUTPUT + else + echo "changed=false" >> $GITHUB_OUTPUT + fi + + - name: Install MariaDB client + if: steps.migration_check.outputs.changed == 'true' + run: sudo apt-get update && sudo apt-get install -y mariadb-client + + - name: Install migrate + if: steps.migration_check.outputs.changed == 'true' + run: | + curl -L https://github.com/golang-migrate/migrate/releases/latest/download/migrate.linux-amd64.tar.gz \ + | tar xvz + sudo mv migrate /usr/local/bin/ + + - name: Wait for DB ready + if: steps.migration_check.outputs.changed == 'true' + run: | + for i in {1..30}; do + mariadb -h 127.0.0.1 -P 3306 -u my_username -pmy_password -e "SELECT 1" && break + sleep 2 + done + + - name: Run migrations + if: steps.migration_check.outputs.changed == 'true' + run: make migration-test + - name: Set up Go uses: actions/setup-go@v6 with: go-version-file: 'go.mod' + - name: Set up Docker Compose uses: docker/setup-compose-action@v2 + - name: Install Ginkgo run: go install github.com/onsi/ginkgo/v2/ginkgo + - name: Install Mockery run: go install github.com/vektra/mockery/v2@v2.53.5 + - name: Generate Mockery code run: make mockery + - name: Generate Gqlgen code run: make gqlgen + - name: Start OpenFGA server run: docker compose -f docker-compose.yaml --profile openfga up -d + - name: Run Tests run: ginkgo --trace -r -randomize-all -randomize-suites diff --git a/Makefile b/Makefile index e0b848d8..450f99c9 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,12 @@ SERVER_IMAGE := ghcr.io/cloudoperators/heureka VERSION ?= $(shell git log -1 --pretty=format:"%H") OS := $(shell go env GOOS) ARCH := $(shell go env GOARCH) - SHELL=/bin/bash -MIGRATIONS_DIR=internal/database/mariadb/migrations/ +MIGRATIONS_DIR ?=internal/database/mariadb/migrations/ +BASE_REF ?= main -.PHONY: all test doc gqlgen mockery test-all test-e2e test-app test-db fmt compose-prepare compose-up compose-down compose-restart compose-build check-call-cached check-migration-files +.PHONY: all test doc gqlgen mockery test-all test-e2e test-app test-db fmt compose-prepare compose-up compose-down compose-restart compose-build check-call-cached check-migration-files \ +migration-change-detected migration-test-conditional migration-test # Source the .env file to use the env vars with make -include .env @@ -122,7 +123,38 @@ install-migrate: go install -tags 'heureka-migration' github.com/golang-migrate/migrate/v4/cmd/migrate@latest create-migration: - @(test -v MIGRATION_NAME && migrate create -ext sql -dir internal/database/mariadb/migrations ${MIGRATION_NAME}) || echo MIGRATION_NAME not specified >&2 + @(test -v MIGRATION_NAME && migrate create -ext sql -dir ${MIGRATIONS_DIR} ${MIGRATION_NAME}) || echo MIGRATION_NAME not specified >&2 + +MARIADB_PASSWORD ?= my_password +MARIADB_USER ?= root +MARIADB_HOST ?= 127.0.0.1 +MARIADB_PORT ?= 3306 +MARIADB_CMD ?= mariadb -h ${MARIADB_HOST} -P ${MARIADB_PORT} -u ${MARIADB_USER} -p'${MARIADB_PASSWORD}' +MARIADB_TEST_SCHEMA ?= heurekaTestSchema + +migration-change-detected: + @if git diff --quiet origin/${BASE_REF} -- ${MIGRATIONS_DIR}; then \ + exit 1; \ + else \ + exit 0; \ + fi + +migration-test-conditional: + @if git diff --quiet origin/${BASE_REF} -- ${MIGRATIONS_DIR}; then \ + echo "No migration changes detected. Skipping."; \ + exit 0; \ + else \ + @echo "Migration changes detected. Running tests..."; \ + $(MAKE) migration-test; \ + fi + +migration-test: + $(MARIADB_CMD) -e "DROP SCHEMA IF EXISTS ${MARIADB_TEST_SCHEMA};" + $(MARIADB_CMD) -e "CREATE SCHEMA ${MARIADB_TEST_SCHEMA};" + migrate -path ${MIGRATIONS_DIR} \ + -database "mysql://${MARIADB_USER}:${MARIADB_PASSWORD}@tcp(${MARIADB_HOST}:${MARIADB_PORT})/${MARIADB_TEST_SCHEMA}" up + migrate -path ${MIGRATIONS_DIR} \ + -database "mysql://${MARIADB_USER}:${MARIADB_PASSWORD}@tcp(${MARIADB_HOST}:${MARIADB_PORT})/${MARIADB_TEST_SCHEMA}" down -all check-call-cached: go generate ./internal/cache @@ -133,8 +165,8 @@ check-call-cached: # if smart commit was not found, check for modification of any migration file existing in main branch # NOTE: new files created in your branch will not be checked for modification/creation check-migration-files: - @git log origin/main..HEAD --pretty=%B | grep -Eq "[!]changeexistingmigration" || \ - (comm -12 <(git diff --name-only origin/main...HEAD | grep '^$(MIGRATIONS_DIR)' | sort) <(git ls-tree -r --name-only origin/main | grep '^$(MIGRATIONS_DIR)' | sort) | grep -q . && { echo "Forbidden change detected!"; exit 1; } || echo "No forbidden changes.") + @git log origin/$(BASE_REF)..HEAD --pretty=%B | grep -Eq "[!]changeexistingmigration" || \ + (comm -12 <(git diff --name-only origin/${BASE_REF}...HEAD | grep '^$(MIGRATIONS_DIR)' | sort) <(git ls-tree -r --name-only origin/main | grep '^$(MIGRATIONS_DIR)' | sort) | grep -q . && { echo "Forbidden change detected!"; exit 1; } || echo "No forbidden changes.") ui-up: $(DOCKER_COMPOSE) --profile ui up -d