-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Yarn blank state non destructive (#6416)
* Refactor filter * Add test script * Refactor code * Refactor code * Add tests * Remove unused code * WIP, proof of concept bash script test * Argument management first approach * Use cases tests added * fix blank_state tests and refactor * Tweak error message when db already exists * Bump version * Added --force to blank-state in cypress e2e --------- Co-authored-by: Daneryl <daneryl@gmail.com> Co-authored-by: A happy cat <acasadotorres@gmail.com>
- Loading branch information
1 parent
ad5398b
commit e460bb9
Showing
6 changed files
with
101 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,42 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
[[ -f ".env" ]] && source ".env" | ||
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd -P ) | ||
cd "$parent_path" || exit | ||
|
||
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) | ||
cd "$parent_path" | ||
FORCE_FLAG=false | ||
|
||
DB=${1:-${DATABASE_NAME:-uwazi_development}} | ||
HOST=${2:-${DBHOST:-127.0.0.1}} | ||
filtered=() | ||
args=("$@") | ||
for item in "${args[@]}"; do | ||
if [ "$item" == '--force' ]; then | ||
FORCE_FLAG=true | ||
else | ||
filtered+=("$item") | ||
fi | ||
done | ||
|
||
echo -e "\n\nDeleting $DB database on $HOST" | ||
mongosh --quiet -host $HOST $DB --eval "db.dropDatabase()" | ||
mongorestore -h $HOST blank_state/uwazi_development/ --db=$DB | ||
DB=${filtered[0]:-${DATABASE_NAME:-uwazi_development}} | ||
HOST=${filtered[1]:-${DBHOST:-127.0.0.1}} | ||
|
||
recreate_database() { | ||
mongosh --quiet -host "$HOST" "$DB" --eval "db.dropDatabase()" | ||
mongorestore -h "$HOST" blank_state/uwazi_development/ --db="$DB" | ||
|
||
INDEX_NAME=$DB DATABASE_NAME=$DB yarn migrate | ||
INDEX_NAME=$DB DATABASE_NAME=$DB yarn reindex | ||
|
||
exit 0 | ||
} | ||
|
||
mongo_indexof_db=$(mongosh --quiet -host "$HOST" --eval "db.getMongo().getDBNames().indexOf('$DB')") | ||
RED='\033[0;31m' | ||
NC='\033[0m' | ||
if [ "$mongo_indexof_db" -ne "-1" ]; then | ||
if [ "$FORCE_FLAG" = false ]; then | ||
echo -e "\nError!${RED} $DB ${NC}database already exists. It will not be deleted.\nPlease use --force flag if you want to override\n" | ||
exit 2 | ||
fi | ||
fi | ||
recreate_database | ||
|
||
INDEX_NAME=$DB DATABASE_NAME=$DB yarn migrate | ||
INDEX_NAME=$DB DATABASE_NAME=$DB yarn reindex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/bin/bash | ||
|
||
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd -P ) | ||
cd "$parent_path" || exit | ||
|
||
assert_equals() { | ||
E_PARAM_ERR=98 | ||
E_ASSERT_FAILED=99 | ||
|
||
if [ -z "$3" ] | ||
then | ||
return $E_PARAM_ERR | ||
fi | ||
|
||
message=$1 | ||
|
||
if [ ! "$2" == "$3" ] | ||
then | ||
echo "❌ $message" | ||
exit $E_ASSERT_FAILED | ||
else | ||
echo "✅ $message" | ||
return | ||
fi | ||
} | ||
|
||
mongosh --quiet new-db --eval "db.dropDatabase()" | ||
|
||
echo -e "\nTest blank state -> new_db" | ||
yarn blank-state new-db > /dev/null 2>&1; result=$? | ||
assert_equals "Creating new-db should be successful" $result 0 | ||
|
||
echo -e "\nTest blank state -> Database existing should exit with error" | ||
yarn blank-state new-db > /dev/null 2>&1; result=$? | ||
assert_equals "Creating new-db again should throw error" $result 2 | ||
|
||
echo -e "\nTest blank state -> Database existing with force flag" | ||
yarn blank-state --force new-db > /dev/null 2>&1; result=$? | ||
assert_equals "Creating new-db again should be successful" $result 0 | ||
|
||
echo -e "\nTest blank state -> Database existing with force flag in any order" | ||
yarn blank-state new-db --force > /dev/null 2>&1; result=$? | ||
assert_equals "Creating new-db again should be successful" $result 0 | ||
|
||
export DATABASE_NAME="uwazi_development_test"; | ||
mongosh --quiet uwazi_development_test --eval "db.dropDatabase()" | ||
|
||
echo -e "\nTest blank state -> Default params" | ||
yarn blank-state > /dev/null 2>&1; result=$? | ||
assert_equals "Creating default db should be successful" $result 0 | ||
|
||
echo -e "\nTest blank state -> Database existing with --force flag" | ||
yarn blank-state --force > /dev/null 2>&1; result=$? | ||
assert_equals "Creating default db with force should be successful" $result 0 | ||
|
||
echo -e "\nTest blank state -> Database deafult existing should exit with error" | ||
yarn blank-state > /dev/null 2>&1; result=$? | ||
assert_equals "Creating default db again should throw error" $result 2 | ||
|
||
mongosh --quiet uwazi_development_test --eval "db.dropDatabase()" | ||
mongosh --quiet new-db --eval "db.dropDatabase()" |