-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrun-database-migrations
More file actions
executable file
·100 lines (81 loc) · 3.38 KB
/
Copy pathrun-database-migrations
File metadata and controls
executable file
·100 lines (81 loc) · 3.38 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/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
#
# Optional parameters:
# --workspace <name> – Terraform workspace to select (for PR environments)
#
# 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
workspace=""
while :; do
case "${1:-}" in
--workspace)
workspace="$2"
shift 2
;;
*)
break
;;
esac
done
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
db_migrator_user=$(terraform -chdir="infra/${app_name}/app-config" output -json environment_configs | jq -r ".${environment}.database_config.migrator_username")
db_app_user=$(terraform -chdir="infra/${app_name}/app-config" output -json environment_configs | jq -r ".${environment}.database_config.app_username")
./bin/terraform-init "infra/${app_name}/service" "${environment}"
if [ -n "${workspace}" ]; then
echo "Selecting Terraform workspace: ${workspace}"
terraform -chdir="infra/${app_name}/service" workspace select "${workspace}"
fi
migrator_role_arn=$(terraform -chdir="infra/${app_name}/service" output -raw migrator_role_arn)
echo
if [ -n "${workspace}" ]; then
# For PR environments, the task definition was already updated by the caller's
# terraform apply, so skip the targeted apply (which would reinitialize terraform
# and reset the workspace).
echo "Skipping task definition update (already applied in workspace ${workspace})"
else
echo "::group::Step 1. Update task definition without updating service"
TF_CLI_ARGS_apply="-input=false -auto-approve -var=image_tag=${image_tag}
-target=module.service.aws_ecs_task_definition.app
-target=module.service.aws_iam_role_policy.task_executor" \
make infra-update-app-service APP_NAME="${app_name}" ENVIRONMENT="${environment}"
echo "::endgroup::"
fi
echo
echo 'Step 2. Run "db-migrate" command'
command='["db-migrate"]'
# Indent the later lines more to make the output of run-command prettier
environment_variables=$(cat << EOF
[{ "name" : "DB_USER", "value" : "${db_migrator_user}" },
{ "name" : "APP_USER", "value" : "${db_app_user}" }]
EOF
)
./bin/run-command ${workspace:+--workspace "${workspace}"} --task-role-arn "${migrator_role_arn}" --environment-variables "${environment_variables}" "${app_name}" "${environment}" "${command}"