diff --git a/basedosdados_api/core/management/commands/loadfixture.py b/basedosdados_api/core/management/commands/loadfixture.py index 1cecbd15..d5874546 100644 --- a/basedosdados_api/core/management/commands/loadfixture.py +++ b/basedosdados_api/core/management/commands/loadfixture.py @@ -44,16 +44,16 @@ def handle(self, *args, **options) -> str | None: return None print("Purge previous database if exists") - DB_PATH.unlink(missing_ok=True) + call_command("flush", interactive=False) print("Migrate development database") call_command("migrate") print("Purge database restrictions") with connection.cursor() as cursor: - cursor.execute(f"{DB_STATEMENT} auth_permission") - cursor.execute(f"{DB_STATEMENT} django_admin_log") - cursor.execute(f"{DB_STATEMENT} django_content_type") + cursor.execute(f"{DB_STATEMENT} auth_permission CASCADE") + cursor.execute(f"{DB_STATEMENT} django_admin_log CASCADE") + cursor.execute(f"{DB_STATEMENT} django_content_type CASCADE") print("Load fixtures") response = super().handle(*args, **options) diff --git a/scripts/dump.sh b/scripts/dump.sh deleted file mode 100755 index 1cd76455..00000000 --- a/scripts/dump.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -set -euxo pipefail - -# Set kube context -# kubectl config use-context ,,, - -# Set kube context namespace -# kubectl config set-context --current --namespace=,,, - -# Set file -FILE=fixtures - -# Set source env -SRC_ENV=api-prod - -# Get source pod -SRC_POD=$(kubectl get po -l app.kubernetes.io/name=$SRC_ENV -o name | \ - sed "s/^.\{4\}//" | \ - head -n 1\ -) - -# Dump fixtures in source pod -kubectl exec $SRC_POD -- /bin/bash -c "\ - python manage.py dumpfixture --output $FILE.json && \ - tar -czvf $FILE.tar.gz $FILE.json\ -" - -# Copy fixtures from source pod -kubectl cp $SRC_POD:/app/$FILE.tar.gz ./$FILE.tar.gz - -# Remove fixtures from pod -kubectl exec $SRC_POD -- rm $FILE.json $FILE.tar.gz diff --git a/scripts/sync.sh b/scripts/sync.sh index 9a83c315..691b4177 100755 --- a/scripts/sync.sh +++ b/scripts/sync.sh @@ -9,42 +9,67 @@ set -euxo pipefail # kubectl config set-context --current --namespace=,,, # Set file -FILE=fixtures +FILE="${FILE:-fixtures}" -# Set destination environment -SRC_ENV=api-prod -DST_ENV=api-staging +# If dump doesn't exist +if [ ! -f "$FILE.tar.gz" ]; then + # Set source env + SRC_ENV="${SRC_ENV:-api-prod}" + + # Get source pod + SRC_POD=$(kubectl get po -l app.kubernetes.io/name=$SRC_ENV -o name | \ + sed "s/^.\{4\}//" | \ + head -n 1\ + ) + + # Dump fixtures in source pod + kubectl exec $SRC_POD -- /bin/bash -c "\ + python manage.py dumpfixture --output $FILE.json && \ + tar -czvf $FILE.tar.gz $FILE.json\ + " + + # Copy fixtures from source pod + kubectl cp --retries=2 $SRC_POD:/app/$FILE.tar.gz ./$FILE.tar.gz + + # Remove fixtures from source pod + kubectl exec $SRC_POD -- rm $FILE.json $FILE.tar.gz +fi + +# Set destination env +DST_ENV="" if [[ $# -eq 1 ]]; then + # Local + if [[ $1 = l* ]]; then + exit 0 + fi + # Staging + if [[ $1 = s* ]]; then + DST_ENV=api-staging + fi + # Development if [[ $1 = d* ]]; then DST_ENV=api-development fi fi +if [[ $DST_ENV = "" ]]; then + echo "Please specify environment" + exit 1 +fi -# Get source and destination pods -SRC_POD=$(kubectl get po -l app.kubernetes.io/name=$SRC_ENV -o name | \ - sed "s/^.\{4\}//" | \ - head -n 1\ -) +# Get destination pod DST_POD=$(kubectl get po -l app.kubernetes.io/name=$DST_ENV -o name | \ sed "s/^.\{4\}//" | \ head -n 1\ ) -# Dump fixtures in source pod -kubectl exec $SRC_POD -- /bin/bash -c "\ - python manage.py dumpfixture --output $FILE.json && \ - tar -czvf $FILE.tar.gz $FILE.json\ -" - -# Copy fixtures from source pod to destination pod -kubectl cp $SRC_POD:/app/$FILE.tar.gz $DST_POD:/app/$FILE.tar.gz +# Copy fixtures to destination pod +kubectl cp --retries=2 ./$FILE.tar.gz $DST_POD:/app/$FILE.tar.gz # Load fixtures in destination pod -kubectl exec $SRC_POD -- /bin/bash -c "\ +kubectl exec $DST_POD -- /bin/bash -c "\ tar -xzvf $FILE.tar.gz && \ - python manage.py loadfixture $FILE.json && \ + python manage.py loadfixture $FILE.json\ " -# Remove fixtures from pods -kubectl exec $SRC_POD -- rm $FILE.json $FILE.tar.gz +# Remove fixtures from destination kubectl exec $DST_POD -- rm $FILE.json $FILE.tar.gz