author | ymh <ymh.work@gmail.com> |
Tue, 14 Oct 2014 05:07:37 +0200 | |
changeset 22 | 986ee928a866 |
parent 0 | e1d4d7a8255a |
child 42 | 926f0426ce78 |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
# |
|
3 |
# See LICENCE for detail |
|
4 |
# Copyright (c) 2014 IRI |
|
5 |
# |
|
6 |
||
7 |
import datetime |
|
8 |
import json |
|
9 |
import uuid |
|
10 |
||
22
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
11 |
from sqlalchemy import Column, Integer, String, DateTime, Table, Index, text |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
12 |
from sqlalchemy.sql import func |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
13 |
from sqlalchemy.dialects.postgresql import UUID, JSON |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
14 |
|
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
15 |
from database import Base, engine |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
16 |
|
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
17 |
#def get_table_create_stmt(): |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
18 |
# return ( |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
19 |
# "CREATE TABLE IF NOT EXISTS annotations ( " |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
20 |
# "id serial PRIMARY KEY, " |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
21 |
# "uuid uuid UNIQUE, " |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
22 |
# "created timestamp default (now() at time zone 'utc') NOT NULL, " |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
23 |
# "ts timestamptz NOT NULL, " |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
24 |
# "event varchar(255) NOT NULL, " |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
25 |
# "channel varchar(255) NOT NULL, " |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
26 |
# "content json);" |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
27 |
# ) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
28 |
|
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
29 |
class Annotation(Base): |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
30 |
__tablename__ = 'annotations' |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
31 |
|
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
32 |
id = Column(Integer, primary_key=True, nullable=False) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
33 |
uuid = Column(UUID, unique=True, nullable=False) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
34 |
created = Column(DateTime, nullable=False, server_default=text("(now() at time zone 'utc')") ) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
35 |
ts = Column(DateTime(timezone=True), nullable=False) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
36 |
event = Column(String(255), nullable=False) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
37 |
channel = Column(String(255), nullable=False) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
38 |
content = Column(JSON) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
39 |
|
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
40 |
Index('idx_event', Annotation.event) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
41 |
Index('idx_channel', Annotation.channel) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
42 |
Index('idx_ts', Annotation.ts) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
43 |
|
0 | 44 |
|
45 |
def insert_annot_async(params, conn): |
|
46 |
||
47 |
content = params.get('content', {}) |
|
48 |
if not isinstance(content, basestring): |
|
49 |
params['content'] = json.dumps(content) |
|
50 |
if 'uuid' not in params: |
|
51 |
params['uuid'] = uuid.uuid4() |
|
52 |
||
53 |
if 'ts' not in params: |
|
54 |
params['ts'] = datetime.utcnow() |
|
55 |
||
22
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
56 |
stmt = Annotation.__table__.insert().values(**params).compile(engine) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
57 |
|
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
58 |
defer = conn.runOperation(stmt.string, stmt.params) |
986ee928a866
add sqlalchemy model + put create module for webapp
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
59 |
defer.addCallback(lambda _: stmt.params) |
0 | 60 |
|
61 |
return defer |