aboutsummaryrefslogtreecommitdiff
path: root/app/db.py
blob: 4d41dcce413ad7663502cf824b662fa734cb0cdc (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
import sqlalchemy

engine = sqlalchemy.create_engine('sqlite:///data.db')

def init_db():
    with engine.begin() as conn:
        conn.execute(sqlalchemy.text(
            '''
            CREATE TABLE IF NOT EXISTS accounts (
                account_name, PRIMARY KEY (account_name)
            )
            '''
        ))
        conn.execute(sqlalchemy.text(
            '''
            CREATE TABLE IF NOT EXISTS links (
                owner, link, redirect_link, expire_date,
                FOREIGN KEY (owner) REFERENCES accounts(account_name), PRIMARY KEY (link)
            )
            '''
        ))
        conn.execute(sqlalchemy.text(
            '''
            CREATE TABLE IF NOT EXISTS records (
                owner, link, timestamp, ip, location, browser, os, user_agent, isp,
                FOREIGN KEY (owner) REFERENCES links(owner),
                FOREIGN KEY (link) REFERENCES links(link))
            '''
        ))

        conn.commit()