From 1ec50dffe34db8765adb8deaf35becc5781f0bb7 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Thu, 31 Aug 2023 10:35:04 +0200 Subject: [PATCH] Update documentation for db import from one environment to another --- docs/clean-up-prod-db-for-import.md | 61 --------------------------- docs/import-prod-data-heroku.md | 65 +++++++++++++++++++++++++++++ server/nuke_users.py | 5 +++ 3 files changed, 70 insertions(+), 61 deletions(-) delete mode 100644 docs/clean-up-prod-db-for-import.md create mode 100644 docs/import-prod-data-heroku.md diff --git a/docs/clean-up-prod-db-for-import.md b/docs/clean-up-prod-db-for-import.md deleted file mode 100644 index d68942ba..00000000 --- a/docs/clean-up-prod-db-for-import.md +++ /dev/null @@ -1,61 +0,0 @@ -# Update prepreprod Database Contents - - -This document describes the process to import data from a Production database to a preprod database, whith -the removal of all users and their content that are not admin users. -Checkout the commit that is deployed on your target preprod app. - - -``` - -Download latest backup from Heroku - -``` -heroku pg:backups:download --app appname -``` - -Load backup locally - -``` -# assumes your POSTGRES env variables are set correctly -./bin/restore-database.sh -f -d -``` - -Remove all users that are not admin users, also their created contents - -```bash -pip install shell_plus --upgrade -python mangage.py migrate -python manage.py shell -``` - -```python -Objective.objects.exclude(owner=None).delete() -Comment.objects.all().delete() -Assignment.objects.exclude(owner=None).exclude(owner__username="guru").delete() -User.objects.filter(is_superuser=False).exclude(username="mia.teacher@getnada.com").delete() -``` - -Make local dump of database - -``` -./bin/dump_local_database.sh -``` - -Copy local dump to s3 bucket - -``` -aws s3 cp local.dump s3://skillbox-developer-bucket/ -``` - -Get a presigned url from S3 for the dump (supposedly works from the command line, I had to use the S3 web UI) - -``` -aws s3 presign s3://skillbox-developer-bucket/local.dump -``` - -Load backup in heroku - -``` -heroku pg:backups:restore --app skillbox-preprod 'SIGNED URL' DATABASE_URL -``` diff --git a/docs/import-prod-data-heroku.md b/docs/import-prod-data-heroku.md new file mode 100644 index 00000000..68db8489 --- /dev/null +++ b/docs/import-prod-data-heroku.md @@ -0,0 +1,65 @@ +# Update prepreprod Database Contents + +This document describes the process to import the database from one heroku environment to another (e.g. production to pre-prod). +The goal is then to remove all users, with the exception of superusers and some whitelisted ones, and also their custom content. + +```bash +heroku pg:backups:restore my-dhf-prod::b313 --app my-dhf-preprod +``` + +Check latest backup on the _source_ (where the data comes from) environment, note the backup id + +```bash +heroku pg:backups --app + +# example +# 21:56:52 $ heroku pg:backups --app my-dhf-prod + # › Warning: heroku update available from 7.60.1 to 8.1.9. +# === Backups +# ID Created at Status Size Database +# ──── ───────────────────────── ─────────────────────────────────── ────── ──────── +# a314 2023-08-31 01:02:23 +0000 Completed 2023-08-31 01:03:52 +0000 7.52MB DATABASE + +``` + +If desired, capture a new backup + +```bash +heroku pg:backups:capture --app +``` + +For redundancy's sake, create backup on _target_ app + +```bash +heroku pg:backups:capture --app +``` + +Restore the backup from the _source_ to the _target_ (where the data goes, and all the old data is _lost_) app, using the backup id noted before + +```bash +heroku pg:backups:restore :: --app + +# example +# +# heroku pg:backups:restore my-dhf-prod::b313 --app my-dhf-preprod +``` + +Connect to target app and run a bash prompt + +```bash +heroku run --app /bin/bash +``` + +Go to server folder, run the python shell + +```bash +cd server +python manage.py shell +``` + +Import the nuke_user script, run it + +```python +import nuke_users from nuke_users +nuke_users() +``` diff --git a/server/nuke_users.py b/server/nuke_users.py index cca208d5..91f13118 100644 --- a/server/nuke_users.py +++ b/server/nuke_users.py @@ -5,6 +5,11 @@ from rooms.models import Comment def nuke_users(): + # todo: exclude some users, but exclude them from all the deletion steps + # e.g. + # excluded_users=[] + # Objective.objects.exclude(owner=None).exclude(owner__username__in=excluded_users).delete() + # # todo: make sure the user confirms this when called from a django command Objective.objects.exclude(owner=None).delete() Comment.objects.all().delete()