Skip to content

Commit

Permalink
tech: ajout script dump db pour app de test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzem committed Feb 6, 2025
1 parent b8da991 commit 2e93f56
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .scalingo/first-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ echo "First deploy"

if [[ $APP =~ "pa-back-staging-pr" ]] ; then
echo "Dump de la DB de staging"

if [[ $DATABASE_URL == *"pa_back_pro_817"* ]]; then
echo "Error: Target must not be production"
exit 1
fi

export PATH=$HOME/bin:$PATH
dbclient-fetcher psql 13
psql --dbname $DATABASE_URL -c "CREATE extension postgis;"
Expand All @@ -14,4 +20,22 @@ if [[ $APP =~ "pa-back-staging-pr" ]] ; then
pg_restore --clean --if-exists --no-owner --no-privileges --no-comments --dbname $DATABASE_URL dump.pgsql
fi

if [[ $APP =~ "pa-back-prod-test" ]] ; then
echo "Dump de la DB de prod"

if [[ $DATABASE_URL == *"pa_back_pro_817"* ]]; then
echo "Error: Target must not be production"
exit 1
fi

export PATH=$HOME/bin:$PATH
dbclient-fetcher psql 13
psql --dbname $DATABASE_URL -c "CREATE extension postgis;"
psql --dbname $DATABASE_URL -c "CREATE extension postgis_tiger_geocoder CASCADE;"
psql --dbname $DATABASE_URL -c "CREATE extension postgis_topology;"
psql --dbname $DATABASE_URL -c "CREATE extension pg_trgm;"
pg_dump --clean --if-exists --format c --no-owner --no-privileges --no-comments -n 'public' -n 'sequelize' --exclude-schema 'information_schema' --exclude-schema '^pg_*' --exclude-schema 'tiger' --exclude-schema 'tiger_data' --exclude-schema 'topology' --dbname $PROD_DATABASE_URL --file dump.pgsql
pg_restore --clean --if-exists --no-owner --no-privileges --no-comments --dbname $DATABASE_URL dump.pgsql
fi

yarn migration
29 changes: 29 additions & 0 deletions .scalingo/post-deploy.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
#!/bin/bash

echo "Post deploy"

if [[ $APP =~ "pa-back-staging-pr" ]] ; then
echo "Dump de la DB de staging"

if [[ $DATABASE_URL == *"pa_back_pro_817"* ]]; then
echo "Error: Target must not be production"
exit 1
fi

export PATH=$HOME/bin:$PATH
dbclient-fetcher psql 13
pg_dump --clean --if-exists --format c --no-owner --no-privileges --no-comments -n 'public' -n 'sequelize' --exclude-schema 'information_schema' --exclude-schema '^pg_*' --exclude-schema 'tiger' --exclude-schema 'tiger_data' --exclude-schema 'topology' --dbname $STAGING_DATABASE_URL --file dump.pgsql
psql --dbname $DATABASE_URL -c "CREATE EXTENSION IF NOT EXISTS postgis; CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder; CREATE EXTENSION IF NOT EXISTS pg_trgm;"
pg_restore --clean --if-exists --no-owner --no-privileges --no-comments --dbname $DATABASE_URL dump.pgsql
fi

if [[ $APP =~ "pa-back-prod-test" ]] ; then
echo "Dump de la DB de prod"

if [[ $DATABASE_URL == *"pa_back_pro_817"* ]]; then
echo "Error: Target must not be production"
exit 1
fi

export PATH=$HOME/bin:$PATH
dbclient-fetcher psql 13

psql --dbname $DATABASE_URL -c "CREATE extension postgis;"
psql --dbname $DATABASE_URL -c "CREATE extension postgis_tiger_geocoder CASCADE;"
psql --dbname $DATABASE_URL -c "CREATE extension postgis_topology;"
psql --dbname $DATABASE_URL -c "CREATE extension pg_trgm;"

pg_dump --clean --if-exists --format c --no-owner --no-privileges --no-comments -n 'public' -n 'sequelize' --exclude-schema 'information_schema' --exclude-schema '^pg_*' --exclude-schema 'tiger' --exclude-schema 'tiger_data' --exclude-schema 'topology' --dbname $PROD_DATABASE_URL --file dump.pgsql
psql --dbname $DATABASE_URL -c "CREATE EXTENSION IF NOT EXISTS postgis; CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder; CREATE EXTENSION IF NOT EXISTS pg_trgm;"
pg_restore --clean --if-exists --no-owner --no-privileges --no-comments --dbname $DATABASE_URL dump.pgsql
fi

yarn migration

59 changes: 59 additions & 0 deletions scripts/perf/db_dump_restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash

echo "dump cej db and restore to another"

#DUMP_RESTORE_DB_SOURCE=
#DUMP_RESTORE_DB_TARGET=

if [[ $DUMP_RESTORE_DB_TARGET == *"pa_back_pro_817"* ]]; then
echo "Error: Target must not be production"
exit 1
fi

if [[ $DUMP_RESTORE_DB_SOURCE = $DUMP_RESTORE_DB_TARGET ]]; then
echo "error: source must be different from target"
exit 1
fi

if [ -z "$DUMP_RESTORE_DB_SOURCE" ]; then
echo "error: env var DUMP_RESTORE_DB_SOURCE must be set"
exit 1
fi

if [ -z "$DUMP_RESTORE_DB_TARGET" ]; then
echo "error: env var DUMP_RESTORE_DB_TARGET must be set"
exit 1
fi

export PATH=$HOME/bin:$PATH
dbclient-fetcher psql 13

pg_dump --clean --if-exists --format c --dbname "$DUMP_RESTORE_DB_SOURCE" --no-owner --no-privileges --no-comments --schema 'public' --file dump.pgsql \
--exclude-table 'spatial_ref_sys' \
--exclude-table 'log_api_partenaire' \
--exclude-table 'suivi_job' \
--exclude-table 'evenement_engagement' \
--exclude-table 'evenement_engagement_hebdo'

if [ $? -ne 0 ]; then
echo "Error: pg_dump command failed"
exit 1
fi
echo "pg_dump OK"

psql -d ${DUMP_RESTORE_DB_TARGET} \
-c "CREATE EXTENSION IF NOT EXISTS postgis SCHEMA public;" \
-c "CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder CASCADE SCHEMA public;" \
-c "CREATE EXTENSION IF NOT EXISTS postgis_topology SCHEMA topology;" \
-c "CREATE EXTENSION IF NOT EXISTS pg_trgm SCHEMA public;"

echo "extensions crées"

pg_restore --clean --if-exists --no-owner --no-privileges --no-comments --dbname "${DUMP_RESTORE_DB_TARGET}" dump.pgsql

if [ $? -ne 0 ]; then
echo "Error: pg_restore command failed gracefully"
fi
echo "pg_restore OK"

rm -f dump.pgsql

0 comments on commit 2e93f56

Please sign in to comment.