@@ -445,3 +445,84 @@ load ../_helper.bash
445445
446446 popd > /dev/null
447447}
448+
449+ @test " download-db-acquia: Create fresh backup when requested" {
450+ pushd " ${LOCAL_REPO_DIR} " > /dev/null || exit 1
451+
452+ # Clean up any existing test files
453+ rm -rf .data
454+ mkdir -p .data
455+
456+ # Create .env.local with the fresh flag
457+ echo " VORTEX_DB_DOWNLOAD_FRESH=1" > .env.local
458+
459+ declare -a STEPS=(
460+ " [INFO] Started database dump download from Acquia."
461+
462+ # Authentication
463+ " [TASK] Retrieving authentication token."
464+ ' @curl -s -L https://accounts.acquia.com/api/auth/oauth/token --data-urlencode client_id=test-key --data-urlencode client_secret=test-secret --data-urlencode grant_type=client_credentials # {"access_token":"test-token","expires_in":3600}'
465+
466+ # Application UUID
467+ " [TASK] Retrieving testapp application UUID."
468+ ' @curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/applications?filter=name%3Dtestapp # {"_embedded":{"items":[{"uuid":"app-uuid-123","name":"testapp"}]}}'
469+
470+ # Environment ID
471+ " [TASK] Retrieving prod environment ID."
472+ ' @curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/applications/app-uuid-123/environments?filter=name%3Dprod # {"_embedded":{"items":[{"id":"env-id-456","name":"prod"}]}}'
473+
474+ # Create backup
475+ " [TASK] Creating new database backup for testdb."
476+ ' @curl -s -L -X POST -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/environments/env-id-456/databases/testdb/backups # {"_links":{"notification":{"href":"https://cloud.acquia.com/api/notifications/notification-uuid-123"}}}'
477+
478+ # Wait for backup - mock status checks
479+ " [TASK] Waiting for backup to complete."
480+ ' @sleep 10 # 0'
481+ ' @curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/notifications/notification-uuid-123 # {"status":"in-progress"}'
482+ " Backup in progress (10s elapsed)..."
483+ ' @sleep 10 # 0'
484+ ' @curl -s -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/notifications/notification-uuid-123 # {"status":"completed"}'
485+ " [ OK ] Backup completed successfully."
486+ " Fresh backup will be downloaded."
487+
488+ # Continue with normal download flow
489+ " [TASK] Discovering latest backup ID for DB testdb."
490+ ' @curl --progress-bar -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/environments/env-id-456/databases/testdb/backups?sort=created # {"_embedded":{"items":[{"id":"backup-id-new-123","completed":"2024-01-02T00:00:00+00:00"}]}}'
491+
492+ # Rest of download steps...
493+ " [TASK] Discovering backup URL."
494+ ' @curl --progress-bar -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://cloud.acquia.com/api/environments/env-id-456/databases/testdb/backups/backup-id-new-123/actions/download # {"url":"https://backup.example.com/db-fresh.sql.gz"}'
495+
496+ " [TASK] Downloading DB dump into file .data/testdb_backup_backup-id-new-123.sql.gz."
497+ ' @curl --progress-bar -L -H Accept: application/json, version=2 -H Authorization: Bearer test-token https://backup.example.com/db-fresh.sql.gz -o .data/testdb_backup_backup-id-new-123.sql.gz # 0 # # echo "CREATE TABLE fresh (id INT);" | gzip > .data/testdb_backup_backup-id-new-123.sql.gz'
498+
499+ " [TASK] Expanding DB file .data/testdb_backup_backup-id-new-123.sql.gz into .data/testdb_backup_backup-id-new-123.sql."
500+ " @gunzip -t .data/testdb_backup_backup-id-new-123.sql.gz # 0"
501+ " @gunzip -c .data/testdb_backup_backup-id-new-123.sql.gz # 0 # CREATE TABLE fresh (id INT);"
502+
503+ ' [TASK] Renaming file ".data/testdb_backup_backup-id-new-123.sql" to ".data/db.sql".'
504+ ' @mv .data/testdb_backup_backup-id-new-123.sql .data/db.sql # 0 # # echo "CREATE TABLE fresh (id INT);" > .data/db.sql'
505+
506+ " [ OK ] Finished database dump download from Acquia."
507+ )
508+
509+ export VORTEX_ACQUIA_KEY=" test-key"
510+ export VORTEX_ACQUIA_SECRET=" test-secret"
511+ export VORTEX_ACQUIA_APP_NAME=" testapp"
512+ export VORTEX_DB_DOWNLOAD_ENVIRONMENT=" prod"
513+ export VORTEX_DB_DOWNLOAD_ACQUIA_DB_NAME=" testdb"
514+ export VORTEX_DB_DIR=" .data"
515+ export VORTEX_DB_FILE=" db.sql"
516+ export VORTEX_DB_DOWNLOAD_FRESH=" 1"
517+
518+ mocks=" $( run_steps " setup" ) "
519+ run scripts/vortex/download-db-acquia.sh
520+ run_steps " assert" " ${mocks} "
521+
522+ assert_success
523+ assert_file_exists " .data/db.sql"
524+ assert_file_contains " .data/db.sql" " CREATE TABLE fresh"
525+
526+ rm -rf .data
527+ popd > /dev/null
528+ }
0 commit comments