generated from pagopa/terraform-infrastructure-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
postgres database migration for party app (#37)
Co-authored-by: anttorre <[email protected]>
- Loading branch information
1 parent
1b499ca
commit 36c3d93
Showing
8 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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,80 @@ | ||
#!/usr/bin/env bash | ||
|
||
# | ||
# Apply the configuration relative to a given subscription | ||
# Usage: | ||
# ./flyway.sh info|validate|migrate ENV-SelfCare selc | ||
# | ||
# ./flyway.sh migrate DEV-SelfCare selc | ||
# ./flyway.sh migrate UAT-SelfCare selc | ||
# ./flyway.sh migrate PROD-SelfCare selc | ||
|
||
BASHDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | ||
WORKDIR="$BASHDIR" | ||
|
||
set -e | ||
|
||
COMMAND=$1 | ||
SUBSCRIPTION=$2 | ||
DATABASE=$3 | ||
shift 3 | ||
other=$@ | ||
|
||
if [ -z "${SUBSCRIPTION}" ]; then | ||
printf "\e[1;31mYou must provide a subscription as first argument.\n" | ||
exit 1 | ||
fi | ||
|
||
az account set -s "${SUBSCRIPTION}" | ||
|
||
# shellcheck disable=SC2154 | ||
printf "Subscription: %s\n" "${SUBSCRIPTION}" | ||
|
||
psql_server_name=$(az postgres server list -o tsv --query "[?contains(name,'postgresql')].{Name:name}" | head -1) | ||
psql_server_private_fqdn=$(az postgres server list -o tsv --query "[?contains(name,'postgresql')].{Name:fullyQualifiedDomainName}" | head -1) | ||
keyvault_name=$(az keyvault list -o tsv --query "[?contains(name,'kv')].{Name:name}") | ||
|
||
# in widows, even if using cygwin, these variables will contain a landing \r character | ||
psql_server_name=${psql_server_name//[$'\r']} | ||
psql_server_private_fqdn=${psql_server_private_fqdn//[$'\r']} | ||
keyvault_name=${keyvault_name//[$'\r']} | ||
|
||
administrator_login=$(az keyvault secret show --name postgres-administrator-login --vault-name "${keyvault_name}" -o tsv --query value) | ||
administrator_login_password=$(az keyvault secret show --name postgres-administrator-login-password --vault-name "${keyvault_name}" -o tsv --query value) | ||
|
||
# in widows, even if using cygwin, these variables will contain a landing \r character | ||
administrator_login=${administrator_login//[$'\r']} | ||
administrator_login_password=${administrator_login_password//[$'\r']} | ||
|
||
export FLYWAY_URL="jdbc:postgresql://${psql_server_private_fqdn}:5432/${DATABASE}?sslmode=require" | ||
export FLYWAY_USER="${administrator_login}@${psql_server_name}" | ||
export FLYWAY_PASSWORD="${administrator_login_password}" | ||
export SERVER_NAME="${psql_server_name}" | ||
export FLYWAY_DOCKER_TAG="7.11.1-alpine" | ||
|
||
selc_user_password=$(az keyvault secret show --name postgres-selc-user-password --vault-name "${keyvault_name}" -o tsv --query value) | ||
monitoring_user_password=$(az keyvault secret show --name postgres-monitoring-user-password --vault-name "${keyvault_name}" -o tsv --query value) | ||
monitoring_external_user_password=$(az keyvault secret show --name postgres-monitoring-external-user-password --vault-name "${keyvault_name}" -o tsv --query value) | ||
|
||
# in widows, even if using cygwin, these variables will contain a landing \r character | ||
selc_user_password=${selc_user_password//[$'\r']} | ||
monitoring_user_password=${monitoring_user_password//[$'\r']} | ||
monitoring_external_user_password=${monitoring_external_user_password//[$'\r']} | ||
|
||
export SELC_USER_PASSWORD="${selc_user_password}" | ||
export MONITORING_USER_PASSWORD="${monitoring_user_password}" | ||
export MONITORING_EXTERNAL_USER_PASSWORD="${monitoring_external_user_password}" | ||
|
||
if [[ $WORKDIR == /cygdrive/* ]]; then | ||
WORKDIR=$(cygpath -w ${WORKDIR}) | ||
WORKDIR=${WORKDIR//\\//} | ||
fi | ||
|
||
docker run --rm --network=host -v "${WORKDIR}/migrations/${DATABASE}":/flyway/sql \ | ||
flyway/flyway:"${FLYWAY_DOCKER_TAG}" \ | ||
-url="${FLYWAY_URL}" -user="${FLYWAY_USER}" -password="${FLYWAY_PASSWORD}" \ | ||
-validateMigrationNaming=true \ | ||
-placeholders.selcUserPassword="${SELC_USER_PASSWORD}" \ | ||
-placeholders.monitoringUserPassword="${MONITORING_USER_PASSWORD}" \ | ||
-placeholders.monitoringExternalUserPassword="${MONITORING_EXTERNAL_USER_PASSWORD}" \ | ||
-placeholders.serverName="${SERVER_NAME}" "${COMMAND}" ${other} |
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,16 @@ | ||
ALTER DEFAULT PRIVILEGES IN SCHEMA public REVOKE ALL PRIVILEGES ON TABLES FROM "SELC_USER"; | ||
ALTER DEFAULT PRIVILEGES IN SCHEMA public REVOKE USAGE ON SEQUENCES FROM "SELC_USER"; | ||
REVOKE ALL ON ALL TABLES IN SCHEMA public FROM "SELC_USER"; | ||
REVOKE ALL ON ALL SEQUENCES IN SCHEMA public FROM "SELC_USER"; | ||
REVOKE ALL ON DATABASE selc FROM "SELC_USER"; | ||
DROP ROLE "SELC_USER"; | ||
|
||
ALTER DEFAULT PRIVILEGES IN SCHEMA public REVOKE SELECT ON TABLES FROM "MONITORING_USER"; | ||
REVOKE ALL ON ALL TABLES IN SCHEMA public FROM "MONITORING_USER"; | ||
REVOKE ALL ON DATABASE selc FROM "MONITORING_USER"; | ||
DROP ROLE "MONITORING_USER"; | ||
|
||
ALTER DEFAULT PRIVILEGES IN SCHEMA public REVOKE SELECT ON TABLES FROM "MONITORING_EXTERNAL_USER"; | ||
REVOKE ALL ON ALL TABLES IN SCHEMA public FROM "MONITORING_EXTERNAL_USER"; | ||
REVOKE ALL ON DATABASE selc FROM "MONITORING_EXTERNAL_USER"; | ||
DROP ROLE "MONITORING_EXTERNAL_USER"; |
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,3 @@ | ||
DROP TABLE IF EXISTS public.event_journal CASCADE; | ||
DROP TABLE IF EXISTS public.event_tag CASCADE; | ||
DROP TABLE IF EXISTS public.snapshot CASCADE; |
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,31 @@ | ||
-- | ||
-- Roles | ||
-- | ||
|
||
CREATE ROLE "SELC_USER"; | ||
ALTER ROLE "SELC_USER" WITH NOSUPERUSER INHERIT NOCREATEROLE NOCREATEDB LOGIN NOREPLICATION NOBYPASSRLS; | ||
ALTER USER "SELC_USER" WITH PASSWORD '${selcUserPassword}'; | ||
|
||
CREATE ROLE "MONITORING_USER"; | ||
ALTER ROLE "MONITORING_USER" WITH NOSUPERUSER INHERIT NOCREATEROLE NOCREATEDB LOGIN NOREPLICATION NOBYPASSRLS; | ||
ALTER USER "MONITORING_USER" WITH PASSWORD '${monitoringUserPassword}'; | ||
|
||
CREATE ROLE "MONITORING_EXTERNAL_USER"; | ||
ALTER ROLE "MONITORING_EXTERNAL_USER" WITH NOSUPERUSER INHERIT NOCREATEROLE NOCREATEDB LOGIN NOREPLICATION NOBYPASSRLS; | ||
ALTER USER "MONITORING_EXTERNAL_USER" WITH PASSWORD '${monitoringExternalUserPassword}'; | ||
|
||
-- Database creation | ||
-- | ||
|
||
GRANT ALL ON DATABASE selc TO "SELC_USER"; | ||
GRANT CONNECT ON DATABASE selc TO "MONITORING_USER"; | ||
GRANT CONNECT ON DATABASE selc TO "MONITORING_EXTERNAL_USER"; | ||
|
||
-- schema grants | ||
-- | ||
|
||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TABLES TO "SELC_USER"; | ||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE ON SEQUENCES TO "SELC_USER"; | ||
|
||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO "MONITORING_USER"; | ||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO "MONITORING_EXTERNAL_USER"; |
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,48 @@ | ||
CREATE TABLE IF NOT EXISTS public.event_journal( | ||
ordering BIGSERIAL, | ||
persistence_id VARCHAR(255) NOT NULL, | ||
sequence_number BIGINT NOT NULL, | ||
deleted BOOLEAN DEFAULT FALSE NOT NULL, | ||
|
||
writer VARCHAR(255) NOT NULL, | ||
write_timestamp BIGINT, | ||
adapter_manifest VARCHAR(255), | ||
|
||
event_ser_id INTEGER NOT NULL, | ||
event_ser_manifest VARCHAR(255) NOT NULL, | ||
event_payload BYTEA NOT NULL, | ||
|
||
meta_ser_id INTEGER, | ||
meta_ser_manifest VARCHAR(255), | ||
meta_payload BYTEA, | ||
|
||
PRIMARY KEY(persistence_id, sequence_number) | ||
); | ||
|
||
CREATE UNIQUE INDEX event_journal_ordering_idx ON public.event_journal(ordering); | ||
|
||
CREATE TABLE IF NOT EXISTS public.event_tag( | ||
event_id BIGINT, | ||
tag VARCHAR(256), | ||
PRIMARY KEY(event_id, tag), | ||
CONSTRAINT fk_event_journal | ||
FOREIGN KEY(event_id) | ||
REFERENCES event_journal(ordering) | ||
ON DELETE CASCADE | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS public.snapshot ( | ||
persistence_id VARCHAR(255) NOT NULL, | ||
sequence_number BIGINT NOT NULL, | ||
created BIGINT NOT NULL, | ||
|
||
snapshot_ser_id INTEGER NOT NULL, | ||
snapshot_ser_manifest VARCHAR(255) NOT NULL, | ||
snapshot_payload BYTEA NOT NULL, | ||
|
||
meta_ser_id INTEGER, | ||
meta_ser_manifest VARCHAR(255), | ||
meta_payload BYTEA, | ||
|
||
PRIMARY KEY(persistence_id, sequence_number) | ||
); |