Add database creation script

This commit is contained in:
coolneng 2020-01-02 21:36:35 +01:00
parent 8a858e6fc2
commit 6c26ce6588
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
5 changed files with 39 additions and 7 deletions

View File

@ -7,8 +7,8 @@ verify_ssl = true
[packages]
flask = "*"
sqlalchemy = "*"
pymysql = "*"
flask-sqlalchemy = "*"
[requires]
python_version = "3.8"

11
Code/Pipfile.lock generated
View File

@ -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": {

View File

@ -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)

View File

@ -0,0 +1,19 @@
#!/bin/sh
EXPECTED_ARGS=3
ERROR=1
MYSQL=$(command -v which)
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: $0 <database name> <database user> <database password>"
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"

View File

@ -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%]