From c2e2c702e3a41732e58aad7730893dbf1d7e11b2 Mon Sep 17 00:00:00 2001 From: Dinush Chathurya Date: Thu, 26 Dec 2024 23:48:27 +0000 Subject: [PATCH] feat: Add migration workflow --- .github/workflows/docker.yml | 19 ---------- .github/workflows/migration.yml | 65 +++++++++++++++++++++++++++++++++ Dockerfile | 40 -------------------- run-gh-ost.sh | 41 +++++++++++++++++++++ 4 files changed, 106 insertions(+), 59 deletions(-) delete mode 100644 .github/workflows/docker.yml create mode 100644 .github/workflows/migration.yml delete mode 100644 Dockerfile create mode 100644 run-gh-ost.sh diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml deleted file mode 100644 index b62d0d9..0000000 --- a/.github/workflows/docker.yml +++ /dev/null @@ -1,19 +0,0 @@ -on: [push] - -jobs: - docker-login-build-tag-push: - runs-on: ubuntu-latest - name: Docker Login, Build, Tag & Push - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Build, Tag & Publish Image to DockerHub - uses: dinushchathurya/build-tag-push-action@v1.2.1 - with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} - context: . - file: Dockerfile - repoOwner: ${{ secrets.DOCKER_USERNAME }} - repository: gh-ost-image - tag: 1.0.0 \ No newline at end of file diff --git a/.github/workflows/migration.yml b/.github/workflows/migration.yml new file mode 100644 index 0000000..565377a --- /dev/null +++ b/.github/workflows/migration.yml @@ -0,0 +1,65 @@ +name: Deploy Database Changes + +on: + push: + branches: + - dev + pull_request: + branches: + - stg + - prod + workflow_dispatch: + +jobs: + deploy-database: + name: Deploy Database Changes to ${{ matrix.environment }} + runs-on: ubuntu-latest + strategy: + matrix: + environment: [dev, stg, prod] + if: | + (github.event_name == 'push' && matrix.environment == 'dev' && github.ref == 'refs/heads/dev') || + (github.event_name == 'pull_request' && matrix.environment == 'stg' && github.base_ref == 'stg' && github.event.pull_request.merged == true) || + (github.event_name == 'pull_request' && matrix.environment == 'prod' && github.base_ref == 'prod' && github.event.pull_request.merged == true) + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 2 + + - name: Install gh-ost + run: | + wget https://github.com/github/gh-ost/releases/download/v1.1.4/gh-ost-linux-amd64 -O gh-ost # Replace with latest version + chmod +x gh-ost + sudo mv gh-ost /usr/local/bin/ + + - name: Install doctrine/dbal + run: composer require doctrine/dbal --no-interaction --no-dev + + - name: Check for migration changes + id: check_migrations + run: | + if [[ -n "$(git diff --name-only HEAD^ HEAD -- database/migrations)" ]]; then + echo "::set-output name=migrations_changed::true" + else + echo "::set-output name=migrations_changed::false" + fi + + - name: Run gh-ost migrations (only if migrations changed) + if: steps.check_migrations.outputs.migrations_changed == 'true' + env: + DB_HOST: ${{ secrets.DB_HOST_dev || '127.0.0.1' }} # Default for dev + DB_PORT: ${{ secrets.DB_PORT_dev || '3306' }} # Default for dev + DB_USER: ${{ secrets.DB_USER_dev || 'root' }} # Default for dev + DB_PASSWORD: ${{ secrets.DB_PASSWORD_dev || '' }} # Default for dev (empty password) + DB_NAME: ${{ secrets.DB_NAME_dev || 'your_local_db_name' }} # Default for dev + DB_HOST_stg: ${{ secrets.DB_HOST_stg }} + DB_USER_stg: ${{ secrets.DB_USER_stg }} + DB_PASSWORD_stg: ${{ secrets.DB_PASSWORD_stg }} + DB_NAME_stg: ${{ secrets.DB_NAME_stg }} + DB_HOST_prod: ${{ secrets.DB_HOST_prod }} + DB_USER_prod: ${{ secrets.DB_USER_prod }} + DB_PASSWORD_prod: ${{ secrets.DB_PASSWORD_prod }} + DB_NAME_prod: ${{ secrets.DB_NAME_prod }} + run: | + chmod +x ./scripts/run-gh-ost.sh + ./scripts/run-gh-ost.sh ${{ matrix.environment }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 961964a..0000000 --- a/Dockerfile +++ /dev/null @@ -1,40 +0,0 @@ -FROM php:8.1-cli - -# Install essential packages -RUN apt-get update && apt-get install -y --no-install-recommends \ - wget \ - build-essential \ - git \ - libzip-dev \ - zip \ - libpq-dev \ - default-mysql-client - -# Install additional PHP extensions (if needed) -RUN docker-php-ext-install pdo pdo_mysql pgsql zip bcmath mbstring xml - -# Download and install gh-ost (specific version) -WORKDIR /tmp -RUN wget https://github.com/github/gh-ost/archive/refs/tags/v1.1.7.tar.gz -RUN tar -xzf v1.1.7.tar.gz -WORKDIR /tmp/gh-ost-1.1.7 -RUN make -RUN cp gh-ost /usr/local/bin - -# Install Composer -RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer - -# Set working directory for the application -WORKDIR /app - -# Copy application files -COPY . /app - -# Set permissions (if needed) -# RUN chown -R www-data:www-data /app/storage /app/bootstrap/cache - -# Expose port (if needed) -# EXPOSE 80 - -# Set default command (if needed) -# CMD ["php-fpm", "-F"] \ No newline at end of file diff --git a/run-gh-ost.sh b/run-gh-ost.sh new file mode 100644 index 0000000..4f0d83f --- /dev/null +++ b/run-gh-ost.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +ENVIRONMENT="$1" + +# Database connection settings from environment-specific secrets +DB_HOST="${DB_HOST}" +DB_USER="${DB_USER}" +DB_PASSWORD="${DB_PASSWORD}" +DB_NAME="${DB_NAME}" + +MIGRATIONS_PATH="database/migrations" + +# Check if there are any migration files changed (duplicate check for safety) +if [[ -z "$(git diff --name-only HEAD^ HEAD -- database/migrations)" ]]; then + echo "No migration files changed. Skipping gh-ost process." + exit 0 +fi + +find "$MIGRATIONS_PATH" -name "*.php" -print0 | while IFS= read -r -d $'\0' file; do + echo "Processing migration file: $file" + + php artisan migrate:refresh --path="$file" --database=mysql --pretend | grep "ALTER" | while IFS= read -r sql_statement; do + + TABLE_NAME=$(echo "$sql_statement" | sed -E 's/ALTER TABLE `(.*?)`.*$/\1/') + if [[ -z "$TABLE_NAME" ]]; then + echo "Could not extract table name from: $sql_statement" + continue + fi + + echo "Executing gh-ost for table: $TABLE_NAME with statement: $sql_statement" + gh-ost \ + --host="$DB_HOST" \ + --port="$DB_PORT" \ + --user="$DB_USER" \ + --password="$DB_PASSWORD" \ + --database="$DB_NAME" \ + --table="$TABLE_NAME" \ + --alter="$sql_statement" \ + --execute + done +done \ No newline at end of file