Weeknotes 13

  • My colleague managed to develop a simple solution to our CTE performance issue from last week.

    By passing a relation to the scope, we can apply some preconditions to the CTE and drastically increase its performance. In our example, we want to operate on a subset of users like so:

    scope :include_previous_value, ->(relation) {
      with(cte: relation.select(<<~SQL))
         *,
         lag(value) over (partition by user_id order by created_at asc) as previous_value
       SQL
        .from('cte as events')
      }
    

    The downside here is that we’ve introduced a required, unnamed argument to the scope. Using it looks like this:

    Events
      .include_previous_value(Events.where(user_id: [1,2]))
      .where(previous_value: 'active')
    
  • I use Hugo, a static site generator written in Go, to publish these weeknotes. Until this weekend, they were separate from the tiny landing page on my primary site, michaelbaird.co.uk. I spent a few hours this weekend consolidating the two different approaches and simplifying the deployment setup.

    I love the ease and simplicity of GitHub Pages. Still, it can be a little too simple, especially if you’re not using Jekyll and therefore either need to maintain a separate ‘gh-pages’ branch or have a separate repository with the published content. Before this weekend, my approach was the latter. I used a custom deploy script to build the repository into a subfolder and pushed it to a repository containing the static HTML.

    In searching for an easy to use (and ideally free) static site host, I came across the newly-launched Cloudflare Pages. It boasted git push builds, Hugo configuration out the box and all the usual security and speed benefits provided by Cloudflare DNS. With a healthy scepticism of 1-click deploys and seemingly magic provisioning and content serving, I was pleasantly surprised that it did indeed seem to all just work.

    To round off a successful evening, I did take great joy in deleting the custom deploy script, git submodule and additional repository that were now surplus to requirements. You can see the resulting repository here. The last thing to do was push a quick redirect to the old GitHub Pages-hosted repository and wait for the DNS to fully propagate.