Browse Source

build(postgres): permet de remettre a zero une base facilement

pull/71/head
Vincent Adolphe 3 years ago
parent
commit
58041a1c9e
  1. 10
      scripts/psql_reset.sql
  2. 26
      scripts/reset_bdd.sh

10
scripts/psql_reset.sql

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
DO $$ DECLARE
r RECORD;
BEGIN
-- if the schema you operate on is not "current", you will want to
-- replace current_schema() in query with 'schematodeletetablesfrom'
-- *and* update the generate 'DROP...' accordingly.
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
END $$;

26
scripts/reset_bdd.sh

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
#! /bin/bash
PYTHON=../venv/bin/python
MANAGE=../manage.py
dump_help()
{
cat <<EOF
$0 <postgres_dump>
ecrase la base de donnee de l'application
avec le dump postgresql custom indiqué
EOF
}
[[ "x$1" = "x-h" ]] && { dump_help ; exit ; }
[[ $# -lt 1 ]] && { dump_help ; exit ; }
DUMP_FILE="$1"
read -p "Ecraser la base de donnee avec le contenu de [${DUMP_FILE}] (o/n)" ANS
[[ "$ANS" = "o" ]] || exit
echo "Suppression de la base de donnée"
cat psql_reset.sql | $PYTHON $MANAGE dbshell
echo "Restauration de la base depuis le dump"
pg_restore -xO "$DUMP_FILE" | $PYTHON $MANAGE dbshell
Loading…
Cancel
Save
Map all the world