-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
41 lines (33 loc) · 1.32 KB
/
Copy pathdatabase.py
File metadata and controls
41 lines (33 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
The file that holds the logic to
initialize and connect to the database.
"""
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import logging
# Setup logging out to a file
db_log_file_name = 'db.log'
db_handler_log_level = logging.INFO
db_logger_log_level = logging.DEBUG
db_handler = logging.FileHandler(db_log_file_name)
db_handler.setLevel(db_handler_log_level)
db_logger = logging.getLogger('sqlalchemy')
db_logger.addHandler(db_handler)
db_logger.setLevel(db_logger_log_level)
# Creates the engine to connect with our database
# TODO: Change the name from "vet" to something relevant
engine = create_engine("sqlite:///wave.db", echo=False)
# A Scoped Session helps us when creating our apps
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
# Base is the class we will extend
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
import models
Base.metadata.create_all(bind=engine)