~sirn/fanboi2

ref: 9433842117dfb2879db1654fb993e9134a5c9199 fanboi2/fanboi2/scripts/topic_sync.py -rw-r--r-- 944 bytes
94338421Kridsada Thanabulpong Actually commit sync transaction and update CHANGELOGS. 4 years ago
                                                                                
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
import os
import sys
import transaction
import sqlalchemy as sa
from fanboi2.models import DBSession, TopicMeta, Post
from pyramid.paster import bootstrap


def main(argv=sys.argv):
    if not len(argv) >= 2:
        sys.stderr.write("Usage: %s config\n" % os.path.basename(argv[0]))
        sys.stderr.write("Configuration file not present.\n")
        sys.exit(1)

    bootstrap(argv[1])

    query = TopicMeta.__table__.update().\
            values(bumped_at=sa.select([Post.created_at]).\
                   where(Post.topic_id == TopicMeta.topic_id).\
                   where(Post.bumped).\
                   order_by(sa.desc(Post.created_at)).\
                   limit(1)).\
                returning(TopicMeta.topic_id,
                          TopicMeta.bumped_at)

    with transaction.manager:
        for topic_id, bumped_at in DBSession.execute(query):
            print("Topic %s set bumped_at to %s" % (topic_id, bumped_at))