Skip to content

Commit

Permalink
Yarn blank state non destructive (#6416)
Browse files Browse the repository at this point in the history
* 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
3 people authored Feb 8, 2024
1 parent ad5398b commit e460bb9
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cypress/e2e/settings/account.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/settings/menu.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/settings/mobile-settings-menu.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
46 changes: 36 additions & 10 deletions database/blank_state.sh
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
61 changes: 61 additions & 0 deletions scripts/test_blank_state.sh
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()"

0 comments on commit e460bb9

Please sign in to comment.