#!/bin/bash

# Clears all content from the database. Use with care.

set -e
cd "$(dirname $0)"
source helpers/use-db.sh
source helpers/confirm.sh
cd ..

if [[ ! "$DATABASE_URL" =~ "//localhost/" ]]; then
  echo "Clearing a non-local database is not supported (for safety reasons, modify this script or do it manually if you are absolutely sure)!"
  exit 1
fi

echo "You are about to DELETE ALL CONTENT in the database. This is IRREVERSIBLE."
confirm "Are you sure?"

echo "==> Clearing db..."
psql "$DATABASE_URL" -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public; CREATE EXTENSION pgcrypto"

echo "==> Rerunning migrations..."
diesel migration run

echo "==> Note: Make sure to delete local files referenced by file_infos (e.g. under 'local') manually, since the DB might otherwise try to overwrite them in the future, causing potential inconsistencies."
