Restructure database code
This commit is contained in:
parent
9f4e06d7f9
commit
e154cf7fe9
3 changed files with 41 additions and 35 deletions
|
@ -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
|
|
||||||
)
|
|
||||||
)
|
|
41
db/database.py
Normal file
41
db/database.py
Normal file
|
@ -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))
|
|
@ -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)
|
|
Loading…
Reference in a new issue