Database commands (restoring, copying, downgrading, etc) - alphagov/notifications-manuals GitHub Wiki
Instructions for restoring a database and/or failing over are in the Postgres page in the Notify team manual.
You can copy a database - you might want to take the preview DB and put it on a dev env for example.
To dump the db from RDS replace FROM-ENV
with the environment you wish to take the database from. This might take a few minutes depending on the database size.
gds aws notify-FROM-ENV -- db-connect.sh notifydb -- pg_dump -Fc -f db_copy --exclude-schema=hint_plan --exclude-schema=pglogical
To drop the old db we'll need to connect with admin
gds aws notify-TO-ENV-admin -- db-connect.sh notifydb -- pgcli
Drop all the tables from public
schema with table owner notify_schema_owner
.
DO $$
DECLARE
r RECORD;
BEGIN
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public' and tableowner='notify_schema_owner') LOOP
EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
END $$;
After running this command double check that all tables are deleted.
Get the RDS database name from gds cli
gds aws notify-TO-ENV-admin -- db-connect.sh notifydb
It's the field that reads Database: rdsbroker_[....]
Use the db name you've grabbed from above.
gds aws notify-dev-TO-ENV-admin -- db-connect.sh notifydb -- pg_restore -d rdsbroker_xxxx -c -v db_copy
If you only wish to revert changes to the database schema you can run a database downgrade using alembic downgrade. You need to exec into a container running notifications-api (api-web) and run the following command where is formatted like ["dev-a", "development", "preview", "staging", "production"]:
FLASK_APP=application.py NOTIFY_ENVIRONMENT=<env-name> flask db downgrade