blob: 5470d55b0fc770cb4b39178c390b6f1608596f3a (
plain)
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
42
43
44
45
46
47
48
|
"""Database for project."""
import logging
from sqlalchemy import Column, Integer, String, create_engine, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
handler = logging.FileHandler('sqlalchemy.engine.log')
handler.level = logging.DEBUG
logging.getLogger('sqlalchemy.engine.base.Engine').addHandler(handler)
DB_FILE = "sqlite:///db.sqlite"
engine = create_engine(DB_FILE, echo=False)
Base = declarative_base()
class Message(Base):
"""Message from slack."""
__tablename__ = "message"
id = Column(Integer, primary_key=True)
iid = Column(String)
type = Column(String)
username = Column(String)
text = Column(String)
timestamp = Column(DateTime)
# ----------------------------------------------------------------------
def __init__(self, iid, type, username, text, timestamp):
"""Create record."""
self.iid = iid
self.type = type
self.username = username
self.text = text
self.timestamp = timestamp
def create_tables():
"""Create tables."""
Base.metadata.create_all(engine)
def get_session():
"""Start a session."""
engine = create_engine(DB_FILE, echo=True)
Session = sessionmaker(bind=engine)
return Session()
|