-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-database-migrations
More file actions
executable file
·74 lines (60 loc) · 2.84 KB
/
run-database-migrations
File metadata and controls
executable file
·74 lines (60 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Run database migrations
# 1. Update the application's task definition with the latest build, but
# do not update the service
# 2. Run the "db-migrate" command in the container as a new task
#
# Positional parameters:
# app_name (required) – the name of subdirectory of /infra that holds the
# application's infrastructure code.
# image_tag (required) – the tag of the latest build
# environment (required) – the name of the application environment (e.g. dev,
# staging, prod)
# -----------------------------------------------------------------------------
set -euo pipefail
app_name="$1"
image_tag="$2"
environment="$3"
echo "=================="
echo "Running migrations"
echo "=================="
echo "Input parameters"
echo " app_name=${app_name}"
echo " image_tag=${image_tag}"
echo " environment=${environment}"
echo
echo "Step 0. Check if app has a database"
terraform -chdir="infra/${app_name}/app-config" init > /dev/null
terraform -chdir="infra/${app_name}/app-config" apply -auto-approve > /dev/null
has_database=$(terraform -chdir="infra/${app_name}/app-config" output -raw has_database)
if [ "${has_database}" = "false" ]; then
echo "Application does not have a database, no migrations to run"
exit 0
fi
./bin/terraform-init "infra/${app_name}/service" "${environment}"
db_migrator_user=$(terraform -chdir="infra/${app_name}/service" output -raw migrator_username)
db_migrator_user_client_id=$(terraform -chdir="infra/${app_name}/service" output -raw migrator_user_client_id)
job_name=$(terraform -chdir="infra/${app_name}/service" output -raw service_job_name)
resource_group=$(terraform -chdir="infra/${app_name}/service" output -raw service_resource_group)
echo
echo "::group::Step 1. Update job definition"
TF_CLI_ARGS_apply="-input=false -auto-approve -var=image_tag=${image_tag}
-target=module.service.azurerm_container_app_job.service_job
" \
make infra-update-app-service APP_NAME="${app_name}" ENVIRONMENT="${environment}"
echo "::endgroup::"
echo
echo 'Step 2. Run "db-migrate" command'
command='["db-migrate"]'
# later lines indented to look nicer in the output of run-app-job
environment_variables=$(cat << EOF
[
{ "name": "DB_USER", "value": "$db_migrator_user" },
{ "name": "AZURE_CLIENT_ID", "value": "$db_migrator_user_client_id" },
{ "name": "APPSETTING_WEBSITE_SITE_NAME", "value": "https://github.com/microsoft/azure-container-apps/issues/502" }
]
EOF
)
subscription_id=$(./bin/network-name-for-app-environment "${app_name}" "${environment}" | ./bin/account-name-for-network | { network_name=$(cat); ./bin/account-ids-by-name | jq -r ".${network_name}"; })
./bin/run-app-job --subscription-id "${subscription_id}" --environment-variables "${environment_variables}" "${job_name}" "${resource_group}" "${command}"