Skip to content

Commit

Permalink
create a new user after creating a fresh db (#672)
Browse files Browse the repository at this point in the history
* crate a new user after creating a fresh db

Signed-off-by: Ahmad <ahmedwahid16101@gmail.com>

* made db_user as an optional param for make command

Signed-off-by: Ahmad <ahmedwahid16101@gmail.com>

* grant the required privileges to the new user

Signed-off-by: Ahmad <ahmedwahid16101@gmail.com>

* create user if doesn't exist, and assign permissions

Signed-off-by: Ahmad <ahmedwahid16101@gmail.com>

* removed unwanted line

Signed-off-by: Ahmad <ahmedwahid16101@gmail.com>

* checks for creating and dropping db

Signed-off-by: Ahmad <ahmedwahid16101@gmail.com>

* replace exit by return and then use exit in the main script

Signed-off-by: Ahmad <ahmedwahid16101@gmail.com>

* removed exit from internal functions

Signed-off-by: Ahmad <ahmedwahid16101@gmail.com>

---------

Signed-off-by: Ahmad <ahmedwahid16101@gmail.com>
Co-authored-by: Nicolas Höning <nicolas@seita.nl>
  • Loading branch information
Ahmad-Wahid and nhoening authored May 18, 2023
1 parent 4b963f5 commit aecb395
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 15 deletions.
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
92 changes: 80 additions & 12 deletions flexmeasures/data/scripts/clean_database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,71 @@ 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 "User $1 is already available."
return 0 # success (user exists)
else
echo "User $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..."
return 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
if sudo -i -u postgres createdb -U postgres $1; then
echo "$1 database is created"
else
echo "$1 database cannot be created"
return 1
fi

if [[ -n "$2" ]];
then
# check if the user already exists
if is_user $2
then
# give the required permissions to the user
grant_privileges $1 $2
else
# if a user is created, then grant the required privileges
if ! create_user $2
then
return 1
else
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 @@ -23,20 +79,26 @@ function create_database() {

# function for deleting the old database
function delete_database() {
echo "Deleting database ..."
sudo -i -u postgres dropdb -U postgres $1
echo "Dropping database ..."
if sudo -i -u postgres dropdb -U postgres $1; then
echo "$1 database is dropped"
return 0
else
echo "$1 database cannot be dropped"
return 1
fi
}

# 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

# Check if the database exists
if is_database $1
then
echo "$1 exists"
echo "$1 database exists"
read -r -p "Make a backup first? [y/N] " response
response=${response,,} # make lowercase
if [[ "$response" =~ ^(yes|y)$ ]]; then
Expand All @@ -47,12 +109,18 @@ then
read -r -p "This will drop your database and re-create a clean one. Continue?[y/N] " response
response=${response,,} # make lowercase
if [[ "$response" =~ ^(yes|y)$ ]]; then
delete_database $1
create_database $1
if ! delete_database $1; then
exit 1
fi
if ! create_database $1 $2; then
exit 1
fi
fi

# otherwise, create a fresh database
else
echo "$1 does not exist"
create_database $1
echo "$1 database does not exist"
if ! create_database $1 $2; then
exit 1
fi
fi

0 comments on commit aecb395

Please sign in to comment.