From e154cf7fe9e6861dbd698a62c3f6a35fde14126b Mon Sep 17 00:00:00 2001 From: "Simon V. Lejel" Date: Fri, 11 Mar 2022 20:13:18 +0100 Subject: [PATCH] Restructure database code --- db/connector.py | 17 ----------------- db/database.py | 41 +++++++++++++++++++++++++++++++++++++++++ db/model/image_table.py | 18 ------------------ 3 files changed, 41 insertions(+), 35 deletions(-) delete mode 100644 db/connector.py create mode 100644 db/database.py delete mode 100644 db/model/image_table.py diff --git a/db/connector.py b/db/connector.py deleted file mode 100644 index e262970..0000000 --- a/db/connector.py +++ /dev/null @@ -1,17 +0,0 @@ -import os - -from sqlalchemy import create_engine - -user = os.environ['DB_USER'] -password = os.environ['DB_PASSWORD'] -host = os.environ['DB_HOST'] -port = os.environ['DB_PORT'] -database = os.environ['DB_DATABASE'] - - -def get_connection(): - return create_engine( - url="mysql+pymysql://{0}:{1}@{2}:{3}/{4}".format( - user, password, host, port, database - ) - ) diff --git a/db/database.py b/db/database.py new file mode 100644 index 0000000..0353118 --- /dev/null +++ b/db/database.py @@ -0,0 +1,41 @@ +import os +from datetime import datetime + +from sqlalchemy import create_engine, Column, Integer, String, DateTime +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import scoped_session, sessionmaker + +user = os.environ['DB_USER'] +password = os.environ['DB_PASSWORD'] +host = os.environ['DB_HOST'] +port = os.environ['DB_PORT'] +database = os.environ['DB_DATABASE'] + +Base = declarative_base() + + +class Image(Base): + __tablename__ = "images" + + id = Column('id', Integer(), primary_key=True, autoincrement=True) + location = Column('location', String(25), nullable=False) + description = Column('copyright', String(), nullable=False) + timestamp = Column('date_added', DateTime, default=datetime.now()) + + def __init__(self, location, description): + self.location = location + self.description = description + + +def get_connection(): + return create_engine( + url="mysql+pymysql://{0}:{1}@{2}:{3}/{4}".format( + user, password, host, port, database + ) + ) + + +engine = create_engine +Base.metadata.create_all(engine) +Session = scoped_session(sessionmaker(bind=engine, + autocommit=False, autoflush=False)) diff --git a/db/model/image_table.py b/db/model/image_table.py deleted file mode 100644 index a6af713..0000000 --- a/db/model/image_table.py +++ /dev/null @@ -1,18 +0,0 @@ -import sqlalchemy as db - -from db import connector - -engine = connector.create_engine - -metadata_obj = db.MetaData() - -profile = db.Table( - 'images', - metadata_obj, - db.Column('id', db.Integer, primary_key=True), - db.Column('location', db.String), - db.Column('copyright', db.String), - db.Column('date_added', db.TIMESTAMP), -) - -metadata_obj.create_all(engine)