-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1937a03
commit c2e2c70
Showing
4 changed files
with
106 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |