Update documentation for db import from one environment to another

This commit is contained in:
Ramon Wenger 2023-08-31 10:35:04 +02:00
parent c533decd0b
commit 1ec50dffe3
3 changed files with 70 additions and 61 deletions

View File

@ -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 <dumpfile> -d <database>
```
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
```

View File

@ -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 <source-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 <source-app>
```
For redundancy's sake, create backup on _target_ app
```bash
heroku pg:backups:capture --app <target-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 <source-app>::<backup-id> --app <target-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 <target-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()
```

View File

@ -5,6 +5,11 @@ from rooms.models import Comment
def nuke_users(): 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 # todo: make sure the user confirms this when called from a django command
Objective.objects.exclude(owner=None).delete() Objective.objects.exclude(owner=None).delete()
Comment.objects.all().delete() Comment.objects.all().delete()