-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.py
61 lines (44 loc) · 1.66 KB
/
database.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
import enum
import os
import sys
from sqlalchemy import (Column, create_engine, Enum, ForeignKey,
Integer, JSON, LargeBinary, String)
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
try:
_DB_URI = os.environ['CLKHASH_SERVICE_DB_URI']
except KeyError as _e:
_msg = 'Unset environment variable CLKHASH_SERVICE_DB_URI.'
raise KeyError(_msg) from _e
engine = create_engine(_DB_URI)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
class ClkStatus(enum.Enum):
QUEUED = 'queued'
IN_PROGRESS = 'in-progress'
DONE = 'done'
INVALID_DATA = 'invalid-data'
ERROR = 'error'
class Project(Base):
__tablename__ = 'projects'
id = Column(String, primary_key=True)
schema = Column(JSON, nullable=False)
key = Column(String, nullable=False)
clk_count = Column(Integer, nullable=False, server_default='0')
class Clk(Base):
__tablename__ = 'clks'
project_id = Column(String, ForeignKey(Project.id, ondelete="CASCADE"), primary_key=True)
index = Column(Integer, primary_key=True)
status = Column(Enum(ClkStatus), nullable=False)
err_msg = Column(String)
pii = Column(JSON) # Future: make own table if having scale issues?
hash = Column(LargeBinary)
def init_db():
Base.metadata.create_all(bind=engine)
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == 'init':
init_db()
else:
'To create database: python database.py init'