Browse Source

changes to database creation and new script to perform database function

joel
Simon Bowie 2 years ago
parent
commit
4940aa83c6
4 changed files with 92 additions and 1 deletions
  1. +2
    -1
      .gitignore
  2. +4
    -0
      README.md
  3. +81
    -0
      database_functions.sh
  4. +5
    -0
      web/app/__init__.py

+ 2
- 1
.gitignore View File

@@ -1,5 +1,6 @@
web/app/__pycache__/
web/app/db.sqlite
.env.prod
.env.dev
.DS_Store
instance
database_functions_live.sh

+ 4
- 0
README.md View File

@@ -9,6 +9,10 @@ For creating database and user in production:
`CREATE USER 'flask'@'%' IDENTIFIED BY '[PASSWORD]';`
`GRANT CREATE, INSERT, UPDATE, SELECT, DELETE ON toolkit.* TO 'flask'@'%';`

## Legacy instructions:

The following is no longer required in Flask-SQLAlchemy 3. See https://stackoverflow.com/questions/73968584/flask-sqlalchemy-db-create-all-got-an-unexpected-keyword-argument-app

To build the database run:

`docker exec -it python python`

+ 81
- 0
database_functions.sh View File

@@ -0,0 +1,81 @@
#!/bin/bash
# @name: database_functions.sh
# @creation_date: 2022-11-02
# @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: Runs database functions for the ExPub Compendium
# @acknowledgements:
# https://www.redhat.com/sysadmin/arguments-options-bash-scripts

############################################################
# Subprograms #
############################################################
License()
{
echo 'Copyright 2022 Simon Bowie <ad7588@coventry.ac.uk>'
echo
echo 'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:'
echo
echo 'The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.'
echo
echo 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.'
}

Help()
{
# Display Help
echo "This script performs database functions for the ExPub Compendium"
echo
echo "Syntax: database_functions.sh [-l|h|e]"
echo "options:"
echo "l Print the MIT License notification."
echo "h Print this Help."
echo "e Export."
echo "i Import."
echo
}

Export()
{
docker exec -it $CONTAINER mysqldump --single-transaction -u $USERNAME -p$PASSWORD $DATABASE > $EXPORT_FILENAME`date +"%Y%m%d"`.sql
}

Import()
{
docker exec -i $CONTAINER mysql -u $USERNAME -p$PASSWORD $DATABASE < $DIRECTORY/$IMPORT_FILENAME
}
############################################################
############################################################
# Main program #
############################################################
############################################################

# Set variables
CONTAINER=mariadb
DATABASE=toolkit
USERNAME=xxxxxxxx
PASSWORD=xxxxxxxx
EXPORT_FILENAME=toolkit_db_
DIRECTORY="/Users/ad7588/Downloads"
IMPORT_FILENAME=toolkit_db.sql

# Get the options
while getopts ":hlimzaespxdw" option; do
case $option in
l) # display License
License
exit;;
h) # display Help
Help
exit;;
e) # export whole database
Export
exit;;
i) # import database from file
Import
exit;;
\?) # Invalid option
echo "Error: Invalid option"
exit;;
esac
done

+ 5
- 0
web/app/__init__.py View File

@@ -32,6 +32,11 @@ def create_app():

db.init_app(app)

from . import models

with app.app_context():
db.create_all()

login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)

Loading…
Cancel
Save