Just How Easy Migrations Are With Redis

Published on 09/19/19 at 22:25 EST by Max Bridgland

I came to a conclusion that the home page of this blog had posts that were much too long. I needed to change it so that it would only display X characters and then a show more button linking to the blog post. Below is a migration script I wrote showing just how easy it was to convert my old db values to new ones.

def convert_home_html_to_new_format():
    home_post_body = """\
          <div class="card">
            <div class="card-content">
              <div class="media">
                <div class="media-content">
                  <p class="title is-4"><a href="https://blog.maxbridgland.com/blog/{direct_url}">{title}</a></p>
                  <p class="subtitle is-6">{upload_date} by {name}</p>
              <div class="content">
                <center><a href="https://blog.maxbridgland.com/blog/{direct_url}">Show More</a></center>
    all_keys = db.keys()
    keys = []
    for key in all_keys:
        if len(key) < 5:
    for key in keys:
        obj = json.loads(bytes(db.get(key)).decode('utf-8'))
        content = obj['content']
        title = obj['title']
        upload_date = obj['upload_date']
        new_key = str(key).replace('b', '').replace("'", '')
        obj['home_html'] = home_post_body.format(title=title, upload_date=upload_date, content=markdown(content[:120] + "......", extras=["fenced-code-blocks"]), name=app.config['name'], direct_url=new_key)
        db.set(key, json.dumps(obj))

Let's go through and analyze what this function is doing. First I have a formatted HTML string that I will use to generate the card on my site. This HTML is updated compared to what we used to have. After that I pull all my keys from the db and grab all blog keys. I use len(key) < 5 to check that the key is not a long string like the keys I don't want in my db (one of the flaws I've found with Redis is that you can't have "collections" like in MongoDB). I iterate over those keys and append them to a new list with just the blog keys. Then I iterate over that and grab the value from the db. They are all json strings so i load those string with the json module into a Python object. I then modify this python object and reset the key in the database. Ez Pz Migration :)