Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create a new user after creating a fresh db #672

Merged
merged 9 commits into from
May 18, 2023
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ show-data-model:
./flexmeasures/data/scripts/visualize_data_model.py --uml

clean-db:
./flexmeasures/data/scripts/clean_database.sh ${db_name}
./flexmeasures/data/scripts/clean_database.sh ${db_name} ${db_user}
4 changes: 2 additions & 2 deletions documentation/tut/toy-example-from-scratch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ Install Flexmeasures and the database

.. note:: When installing with ``pip``, on some platforms problems might come up (e.g. macOS, Windows). One reason is that FlexMeasures requires some libraries with lots of C code support (e.g. Numpy). One way out is to use Docker, which uses a prepared Linux image, so it'll definitely work.

In case you want to re-run the tutorial, then it's recommended to delete the old database and create a fresh one. Run the following command to create a clean database.
In case you want to re-run the tutorial, then it's recommended to delete the old database and create a fresh one. Run the following command to create a clean database with a new user, where it is optional. If you don't provide the user, then the default `postgres` user will be used to create the database.

.. code-block:: bash

$ make clean-db db_name=flexmeasures-db
$ make clean-db db_name=flexmeasures-db [db_user=flexmeasures]

Add some structural data
---------------------------------------
Expand Down
63 changes: 56 additions & 7 deletions flexmeasures/data/scripts/clean_database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,64 @@ MAIN_DIR=$(pwd)

# function for checking database existence
function is_database() {
cd /tmp
sudo -u postgres psql -lqt | cut -d \| -f 1 | grep -wq $1
cd ${MAIN_DIR}
sudo -i -u postgres psql -lqt | cut -d \| -f 1 | grep -wq $1
}

# check if the user exists
function is_user() {
if sudo -i -u postgres psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='$1'" | grep -q 1; then
echo "$1 is already available."
Ahmad-Wahid marked this conversation as resolved.
Show resolved Hide resolved
return 0 # success (user exists)
else
echo "$1 is not created before."
return 1 # failure (user does not exist)
fi
}

# create a new user
function create_user() {
echo "Creating database user ..."
read -s -p "Enter password for new user: " password
echo ""
read -s -p "Confirm password for new user: " password_confirm
echo ""

if [ "$password" != "$password_confirm" ]; then
echo "Error: Passwords do not match. Exiting..."
exit 1
fi
sudo -i -u postgres psql -c "CREATE USER $1 WITH PASSWORD '$password'"
}

# function to give the required privileges to the newly created user
function grant_privileges(){
echo "Connect $2 to $1 "
sudo -i -u postgres psql -c "GRANT CONNECT ON DATABASE $1 TO $2"
echo "Grant required privileges"
sudo -i -u postgres psql -c "GRANT USAGE, SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO $2"
}

# function for creating a new database
function create_database() {
echo "Creating a new database ..."
sudo -i -u postgres createdb -U postgres $1
Ahmad-Wahid marked this conversation as resolved.
Show resolved Hide resolved

if [[ -n "$2" ]];
then
# check if the user exists
if is_user $2
then
# give the required permissions to the user
grant_privileges $1 $2
else
# if user doesn't exist, first create it and then give the permissions.
if create_user $2
then
grant_privileges $1 $2
fi
fi
fi

echo "Creating cube extension in $1 ..."
sudo -i -u postgres psql -c "\c $1" -c "CREATE EXTENSION cube;"
echo "Creating earthdistance extension in $1 ..."
Expand All @@ -29,7 +78,7 @@ function delete_database() {

# Check if the database name is provided
if [ -z "$1" ]; then
echo "Error: db_name is required. Please provide a value for db_name, e.g., make clean_db db_name=flexmeasures-db"
echo "Error: db_name is required. Please provide a value for db_name, e.g., make clean-db db_name=flexmeasures-db [db_user=flexmeasures]"
exit 1
fi

Expand All @@ -48,11 +97,11 @@ then
response=${response,,} # make lowercase
if [[ "$response" =~ ^(yes|y)$ ]]; then
delete_database $1
Ahmad-Wahid marked this conversation as resolved.
Show resolved Hide resolved
create_database $1
create_database $1 $2
fi

# otherwise, create a fresh database
else
echo "$1 does not exist"
create_database $1
fi
create_database $1 $2
fi
Loading