igdb/Code/database/db_setup.py

50 lines
1.2 KiB
Python
Raw Normal View History

from subprocess import run
from sys import argv
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from fields import Glacier, Annual_Data, Annual_Change, User
2020-01-02 21:36:35 +01:00
global db
2020-01-02 04:27:45 +01:00
def create_database(db_name, db_user, db_pw):
script = "mariadb_setup.sh"
output = run([script + db_name + db_user + db_pw])
if output.returncode != 0:
print("Error: couldn't create database")
exit()
2020-01-02 04:27:45 +01:00
def create_connection(db_name, db_user, db_pw):
host = "localhost:3306"
connection_uri = "mysql+pymysql://{user}:{pw}@{url}/{db}".format(
user=db_user, pw=db_pw, url=host, db=db_name
2020-01-02 04:27:45 +01:00
)
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = connection_uri
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
global db
db = SQLAlchemy(app)
def create_tables(db):
db.create_all()
def main():
if len(argv) != 3:
print("Usage: " + argv[0] + " <db name> <db user> <db password>")
exit()
2020-01-02 21:36:35 +01:00
db_name = str(argv[1])
db_user = str(argv[2])
db_password = str(argv[3])
2020-01-02 21:36:35 +01:00
create_database(db_name, db_user, db_password)
create_connection(db_name, db_user, db_password)
create_tables(db)
2020-01-02 04:27:45 +01:00
if __name__ == "__main__":
main()