From 6c26ce6588fc504be68d70c382418e94801d5178 Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 2 Jan 2020 21:36:35 +0100 Subject: [PATCH] Add database creation script --- Code/Pipfile | 2 +- Code/Pipfile.lock | 11 +++++++++-- Code/database/db_setup.py | 9 +++++++-- Code/database/mariadb_setup.sh | 19 +++++++++++++++++++ Design.org | 5 +++-- 5 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 Code/database/mariadb_setup.sh diff --git a/Code/Pipfile b/Code/Pipfile index f3cd808..ff3c98f 100644 --- a/Code/Pipfile +++ b/Code/Pipfile @@ -7,8 +7,8 @@ verify_ssl = true [packages] flask = "*" -sqlalchemy = "*" pymysql = "*" +flask-sqlalchemy = "*" [requires] python_version = "3.8" diff --git a/Code/Pipfile.lock b/Code/Pipfile.lock index 02f6263..fa60fe4 100644 --- a/Code/Pipfile.lock +++ b/Code/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "536bc13b89021a4575fe2c14d71636abf965ab896871f1503f3186f2f1c6a0d4" + "sha256": "77464f98cb5a9b5d12663323acdbbc44b448b5d9d2fee7c8e0e6302987ef8bfe" }, "pipfile-spec": 6, "requires": { @@ -31,6 +31,14 @@ "index": "pypi", "version": "==1.1.1" }, + "flask-sqlalchemy": { + "hashes": [ + "sha256:0078d8663330dc05a74bc72b3b6ddc441b9a744e2f56fe60af1a5bfc81334327", + "sha256:6974785d913666587949f7c2946f7001e4fa2cb2d19f4e69ead02e4b8f50b33d" + ], + "index": "pypi", + "version": "==2.4.1" + }, "itsdangerous": { "hashes": [ "sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19", @@ -90,7 +98,6 @@ "hashes": [ "sha256:bfb8f464a5000b567ac1d350b9090cf081180ec1ab4aa87e7bca12dab25320ec" ], - "index": "pypi", "version": "==1.3.12" }, "werkzeug": { diff --git a/Code/database/db_setup.py b/Code/database/db_setup.py index 1e25100..3eb7a43 100644 --- a/Code/database/db_setup.py +++ b/Code/database/db_setup.py @@ -1,3 +1,5 @@ +from os import environ + from sqlalchemy import create_engine @@ -9,12 +11,15 @@ def create_connection(): db_connection_uri = "mysql+pymysql://{user}:{pw}@{url}/{db}".format( user=db_user, pw=db_password, url=db_url, db=db_name ) - engine = sqlalchemy.create_engine(db_connection_uri, echo=True) + engine = create_engine(db_connection_uri, echo=True) + + +def create_tables(): def get_env_variable(name): try: - return os.environ[name] + return environ[name] except KeyError: message = "Expected environment variable '{}' not set.".format(name) raise Exception(message) diff --git a/Code/database/mariadb_setup.sh b/Code/database/mariadb_setup.sh new file mode 100644 index 0000000..ad2f5f2 --- /dev/null +++ b/Code/database/mariadb_setup.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +EXPECTED_ARGS=3 +ERROR=1 +MYSQL=$(command -v which) + +if [ $# -ne $EXPECTED_ARGS ] +then + echo "Usage: $0 " + exit $ERROR +fi + +Q1="CREATE DATABASE IF NOT EXISTS $1;" +Q2="GRANT USAGE ON *.* TO $2@localhost IDENTIFIED BY '$3';" +Q3="GRANT ALL PRIVILEGES ON $1.* TO $2@localhost;" +Q4="FLUSH PRIVILEGES;" +SQL="${Q1}${Q2}${Q3}${Q4}" + +$MYSQL -uroot -p -e "$SQL" diff --git a/Design.org b/Design.org index 20576ee..dc0dba6 100644 --- a/Design.org +++ b/Design.org @@ -46,9 +46,10 @@ CLOSED: [2019-11-01 Fri 00:34] - [X] Entity-Relationship ** Implementation *** TODO Backend [0/2] [0%] -**** TODO Database [1/2] [50%] +**** TODO Database [2/3] [66%] - [X] Connection -- [ ] Creation of fields via class +- [X] Creation from script +- [ ] Creation of tables via class **** NEXT Flask framework -[[https://flask.palletsprojects.com/en/1.1.x/patterns/#patterns][ Patterns]] *** NEXT Parsing script [0/2] [0%]