diff --git a/cypress/e2e/settings/account.cy.ts b/cypress/e2e/settings/account.cy.ts index 586f963c65..06a994c145 100644 --- a/cypress/e2e/settings/account.cy.ts +++ b/cypress/e2e/settings/account.cy.ts @@ -5,7 +5,7 @@ import { clearCookiesAndLogin } from '../helpers/login'; describe('Account', () => { before(() => { const env = { DATABASE_NAME: 'uwazi_e2e', INDEX_NAME: 'uwazi_e2e' }; - cy.exec('yarn blank-state', { env }); + cy.exec('yarn blank-state --force', { env }); clearCookiesAndLogin('admin', 'change this password now'); cy.get('.only-desktop a[aria-label="Settings"]').click(); cy.injectAxe(); diff --git a/cypress/e2e/settings/menu.cy.ts b/cypress/e2e/settings/menu.cy.ts index 9ca3e1759a..21f8e06cc5 100644 --- a/cypress/e2e/settings/menu.cy.ts +++ b/cypress/e2e/settings/menu.cy.ts @@ -5,7 +5,7 @@ import { clearCookiesAndLogin } from '../helpers/login'; describe('Menu configuration', () => { before(() => { const env = { DATABASE_NAME: 'uwazi_e2e', INDEX_NAME: 'uwazi_e2e' }; - cy.exec('yarn blank-state', { env }); + cy.exec('yarn blank-state --force', { env }); clearCookiesAndLogin('admin', 'change this password now'); cy.get('.only-desktop a[aria-label="Settings"]').click(); cy.injectAxe(); diff --git a/cypress/e2e/settings/mobile-settings-menu.cy.ts b/cypress/e2e/settings/mobile-settings-menu.cy.ts index 2c4dcb0229..576fbcdec9 100644 --- a/cypress/e2e/settings/mobile-settings-menu.cy.ts +++ b/cypress/e2e/settings/mobile-settings-menu.cy.ts @@ -4,7 +4,7 @@ import 'cypress-axe'; describe('Settings mobile menu', () => { before(() => { const env = { DATABASE_NAME: 'uwazi_e2e', INDEX_NAME: 'uwazi_e2e' }; - cy.exec('yarn blank-state', { env }); + cy.exec('yarn blank-state --force', { env }); }); beforeEach(() => { diff --git a/database/blank_state.sh b/database/blank_state.sh index 7a4f3e8f97..8524658088 100755 --- a/database/blank_state.sh +++ b/database/blank_state.sh @@ -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 diff --git a/package.json b/package.json index 32a348f306..ed8cad2661 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uwazi", - "version": "1.152.0", + "version": "1.152.1", "description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.", "keywords": [ "react" diff --git a/scripts/test_blank_state.sh b/scripts/test_blank_state.sh new file mode 100755 index 0000000000..6a81a432a8 --- /dev/null +++ b/scripts/test_blank_state.sh @@ -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()"