summaryrefslogtreecommitdiff
path: root/f1elo/db.py
blob: 1991e77d5dc53d3a26a84d906b5ca44dcc878a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import json
from os import path

import __main__
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker

config = json.load(
    open(path.join(path.dirname(__main__.__file__), 'config', 'db.json')))
if config['engine'] == 'mysql':
    engine = create_engine(
        "mysql://{0[user]}:{0[pass]}@{0[host]}/{0[db]}?charset=utf8".format(
            config))
elif config['engine'] == 'sqlite':
    engine = create_engine("sqlite:///{0[file]}".format(config))

    def fk_pragma(conn, record):
        conn.execute('PRAGMA FOREIGN_KEYS=ON')

    event.listen(engine, 'connect', fk_pragma)

Session = sessionmaker(bind=engine)