Example #1
0
def insert_bulk_data(topic_count=10, post_count=100):
    """Creates a specified number of topics in the first forum with
    each topic containing a specified amount of posts.
    Returns the number of created topics and posts.

    :param topics: The amount of topics in the forum.
    :param posts: The number of posts in each topic.
    """
    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()
    forum = Forum.query.filter_by(id=1).first()

    last_post = Post.query.order_by(Post.id.desc()).first()
    last_post_id = 1 if last_post is None else last_post.id

    created_posts = 0
    created_topics = 0
    posts = []

    if not (user1 or user2 or forum):
        return False

    with db.session.no_autoflush:
        for i in range(1, topic_count + 1):
            last_post_id += 1

            # create a topic
            topic = Topic(title="Test Title %s" % i)
            post = Post(content="First Post")
            topic.save(post=post, user=user1, forum=forum)
            created_topics += 1

            # create some posts in the topic
            for _ in range(1, post_count + 1):
                last_post_id += 1
                post = Post(content="Some other Post",
                            user=user2,
                            topic=topic.id)
                topic.last_updated = post.date_created
                topic.post_count += 1

                created_posts += 1
                posts.append(post)

        db.session.bulk_save_objects(posts)

    # and finally, lets update some stats
    forum.recalculate(last_post=True)
    user1.recalculate()
    user2.recalculate()

    return created_topics, created_posts
Example #2
0
def insert_bulk_data(topic_count=10, post_count=100):
    """Creates a specified number of topics in the first forum with
    each topic containing a specified amount of posts.
    Returns the number of created topics and posts.

    :param topics: The amount of topics in the forum.
    :param posts: The number of posts in each topic.
    """
    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()
    forum = Forum.query.filter_by(id=1).first()

    last_post = Post.query.order_by(Post.id.desc()).first()
    last_post_id = 1 if last_post is None else last_post.id

    created_posts = 0
    created_topics = 0
    posts = []

    if not (user1 or user2 or forum):
        return False

    db.session.begin(subtransactions=True)

    for i in range(1, topic_count + 1):
        last_post_id += 1

        # create a topic
        topic = Topic(title="Test Title %s" % i)
        post = Post(content="First Post")
        topic.save(post=post, user=user1, forum=forum)
        created_topics += 1

        # create some posts in the topic
        for j in range(1, post_count + 1):
            last_post_id += 1
            post = Post(content="Some other Post", user=user2, topic=topic.id)
            topic.last_updated = post.date_created
            topic.post_count += 1

            # FIXME: Is there a way to ignore IntegrityErrors?
            # At the moment, the first_post_id is also the last_post_id.
            # This does no harm, except that in the forums view, you see
            # the information for the first post instead of the last one.
            # I run a little benchmark:
            # 5.3643078804 seconds to create 100 topics and 10000 posts
            # Using another method (where data integrity is ok) I benchmarked
            # these stats:
            # 49.7832770348 seconds to create 100 topics and 10000 posts

            # Uncomment the line underneath and the other line to reduce
            # performance but fixes the above mentioned problem.
            #topic.last_post_id = last_post_id

            created_posts += 1
            posts.append(post)

        # uncomment this and delete the one below, also uncomment the
        # topic.last_post_id line above. This will greatly reduce the
        # performance.
        #db.session.bulk_save_objects(posts)
    db.session.bulk_save_objects(posts)

    # and finally, lets update some stats
    forum.recalculate(last_post=True)
    user1.recalculate()
    user2.recalculate()

    return created_topics, created_posts