outline/abra.sh
2023-06-20 12:58:15 +02:00

84 lines
3.2 KiB
Bash

export APP_ENTRYPOINT_VERSION=v7
export DB_ENTRYPOINT_VERSION=v1
migrate() {
export DATABASE_PASSWORD=$(cat /run/secrets/db_password)
export DATABASE_URL="postgres://outline:${DATABASE_PASSWORD}@${STACK_NAME}_db:5432/outline"
yarn db:migrate --env=production-ssl-disabled
}
delete_user_by_id() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: ... delete_user_by_id <userid-to-delete> <userid-to-replace>"
exit 1
fi
USERID_REPLACE="$2"
USERID_REMOVE="$1"
psql -U outline outline <<- SQL
UPDATE documents SET "userId" = '$USERID_REPLACE' WHERE "userId" = '$USERID_REMOVE';
UPDATE groups SET "createdById" = '$USERID_REPLACE' WHERE "createdById" = '$USERID_REMOVE';
UPDATE pins SET "createdById" = '$USERID_REPLACE' WHERE "createdById" = '$USERID_REMOVE';
UPDATE group_users SET "createdById" = '$USERID_REPLACE' WHERE "createdById" = '$USERID_REMOVE';
UPDATE collections SET "createdById" = '$USERID_REPLACE' WHERE "createdById" = '$USERID_REMOVE';
UPDATE collection_users SET "createdById" = '$USERID_REPLACE' WHERE "createdById" = '$USERID_REMOVE';
UPDATE collection_groups SET "createdById" = '$USERID_REPLACE' WHERE "createdById" = '$USERID_REMOVE';
UPDATE documents SET "lastModifiedById" = '$USERID_REPLACE' WHERE "lastModifiedById" = '$USERID_REMOVE';
UPDATE documents SET "createdById" = '$USERID_REPLACE' WHERE "createdById" = '$USERID_REMOVE';
UPDATE revisions SET "userId" = '$USERID_REPLACE' WHERE "userId" = '$USERID_REMOVE';
UPDATE attachments SET "userId" = '$USERID_REPLACE' WHERE "userId" = '$USERID_REMOVE';
UPDATE backlinks SET "userId" = '$USERID_REPLACE' WHERE "userId" = '$USERID_REMOVE';
UPDATE file_operations SET "userId" = '$USERID_REPLACE' WHERE "userId" = '$USERID_REMOVE';
UPDATE users SET "suspendedById" = '$USERID_REPLACE' WHERE "suspendedById" = '$USERID_REMOVE';
DELETE FROM search_queries WHERE "userId" = '$USERID_REMOVE';
DELETE FROM shares WHERE "userId" = '$USERID_REMOVE';
DELETE FROM notification_settings WHERE "userId" = '$USERID_REMOVE';
DELETE FROM events WHERE "actorId" = '$USERID_REMOVE';
DELETE FROM events WHERE "userId" = '$USERID_REMOVE';
DELETE FROM users WHERE "id" = '$USERID_REMOVE';
SQL
}
delete_user() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: ... delete_user <userid-to-delete> <userid-to-replace>"
exit 1
fi
USERID_REMOVE=$(echo "SELECT id FROM users WHERE username = '$1'" | psql -t -A -U outline outline)
USERID_REPLACE=$(echo "SELECT id FROM users WHERE username = '$2'" | psql -t -A -U outline outline)
if [ -z "$USERID_REMOVE" ]; then
echo "Can't find ID of '$1'"
exit 1
fi
if [ -z "$USERID_REPLACE" ]; then
echo "Can't find ID of '$2'"
exit 1
fi
delete_user_by_id "$USERID_REMOVE" "$USERID_REPLACE"
}
delete_duplicate_users() {
if [ -z "$1" ]; then
echo "Usage: ... delete_duplicate_users <username>"
exit 1
fi
USERIDS=$(echo "SELECT id FROM users WHERE username = '$1' ORDER BY users.\"createdAt\" DESC" | psql -t -A -U outline outline)
if [ ! "$(echo "$USERIDS" | wc -l)" -gt 1 ]; then
echo "Only one user exists, bailing"
exit 1
fi
USERID_NEW=$(echo "$USERIDS" | head -n1)
for USERID_OLD in $(echo "$USERIDS" | tail -n+2); do
delete_user_by_id "$USERID_OLD" "$USERID_NEW"
done
}