forked from oscar.krause/fastapi-dls
implemented db_init
This commit is contained in:
parent
6049048bbf
commit
f539db5933
@ -22,7 +22,7 @@ from sqlalchemy.orm import sessionmaker
|
|||||||
from Crypto.PublicKey import RSA
|
from Crypto.PublicKey import RSA
|
||||||
from Crypto.PublicKey.RSA import RsaKey
|
from Crypto.PublicKey.RSA import RsaKey
|
||||||
|
|
||||||
from orm import Origin, Lease
|
from orm import Origin, Lease, init as db_init
|
||||||
|
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
load_dotenv('../version.env')
|
load_dotenv('../version.env')
|
||||||
@ -48,6 +48,7 @@ __details = dict(
|
|||||||
)
|
)
|
||||||
|
|
||||||
app, db = FastAPI(**__details), create_engine(url=str(getenv('DATABASE', 'sqlite:///db.sqlite')))
|
app, db = FastAPI(**__details), create_engine(url=str(getenv('DATABASE', 'sqlite:///db.sqlite')))
|
||||||
|
db_init(db)
|
||||||
|
|
||||||
TOKEN_EXPIRE_DELTA = relativedelta(hours=1) # days=1
|
TOKEN_EXPIRE_DELTA = relativedelta(hours=1) # days=1
|
||||||
LEASE_EXPIRE_DELTA = relativedelta(days=int(getenv('LEASE_EXPIRE_DAYS', 90)))
|
LEASE_EXPIRE_DELTA = relativedelta(days=int(getenv('LEASE_EXPIRE_DAYS', 90)))
|
||||||
|
12
app/orm.py
12
app/orm.py
@ -1,6 +1,6 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from sqlalchemy import Column, VARCHAR, CHAR, ForeignKey, DATETIME, UniqueConstraint, update, and_, delete
|
from sqlalchemy import Column, VARCHAR, CHAR, ForeignKey, DATETIME, UniqueConstraint, update, and_, delete, inspect
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.future import Engine
|
from sqlalchemy.future import Engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
@ -102,3 +102,13 @@ class Lease(Base):
|
|||||||
deletions = session.query(Lease).delete(Lease.origin_ref == origin_ref)
|
deletions = session.query(Lease).delete(Lease.origin_ref == origin_ref)
|
||||||
session.close()
|
session.close()
|
||||||
return deletions
|
return deletions
|
||||||
|
|
||||||
|
|
||||||
|
def init(engine: Engine):
|
||||||
|
tables = [Origin, Lease]
|
||||||
|
db = inspect(engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
for table in tables:
|
||||||
|
if not db.dialect.has_table(engine.connect(), table.__tablename__):
|
||||||
|
session.execute(str(table.create_statement(engine)))
|
||||||
|
session.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user