RSSAmplifier

Blog

Jaco Pretorius

A blog for programmers, by a programmer.

jacopretorius.netRSS feed ↗10 posts

Latest posts

How to Use Variables in PostgreSQL psql Scripts (with Examples)

The psql command-line interface for PostgreSQL lets you pass parameters into your SQL scripts using variables. The psql --help command explains how to accomplish this: psql is the PostgreSQL interactive terminal. Usage: psql [OPTION]... [DBNAME [USERNAME]] General options: -f, --file=FILENAME execute commands from file, then exit -v, --set=, --variable=NAME=VALUE set psql variable NAME to VALUE…

Avoiding N+1 Queries with Rails Strict Loading

Active Record makes it easy to introduce N+1 queries into your Rails applications. The convenience of easily adding associations to your models means that great care is needed to avoid N+1 queries. I was therefore excited to see that Rails 6.1 introduced strict_loading to help alleviate some of these problems. I had previously used the bullet gem which adds visibility around these issues but…

Understanding PostgreSQL LATERAL Joins with Practical Examples

PostgreSQL has a powerful feature called Lateral joins , which lets you reference columns from earlier tables in the FROM clause. This becomes especially useful when working with data that changes over time, like historical addresses or versioned records. For example, consider a domain where we keep track of customers and statements. Customers can change their addresses over time, but it’s…

rails new: Complete Guide to All Options

Every time I run the rails new command I try to remember what options I prefer - of course, you can change things later, but if you’re looking to get up and running quickly it’s nice to get it mostly correct on the first try. I did an audit of all the options as of rails 8.0.2 and grouped them in a way that made sense to me, since the default output provided by rails new --help can be difficult to…

Remove Nth Node From End of List

I occasionally solve algorithm questions on Leetcode as a fun exercise. I recently wrote about a dynamic programming question I solved, and I thought it would be fun to share another one. This time it’s a simple linked list question . Remove Nth Node From End of List Given the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: Input: head =…

Interacting with Shell Commands in Ruby

We often need to run shell commands from within Ruby code, and Ruby provides a number of ways to do that. It’s sligtly confusing at first - specifically because there are different ways of accomplishing the same thing. I found this useful flowchart on StackOverflow that helps you choose the appropriate option. If you’re looking for a detailed writeup of the different options I recommend reading 6…

Fingerprinting Jekyll SASS Assets

As I’ve been updating the stylesheets on my blog, I ran into an issue with browser caching — changes to my CSS weren’t showing up right away. Since I’m serving assets through AWS CloudFront with a 7-day cache for non-HTML files, this behavior makes sense. While I could disable caching altogether, that feels like a blunt and amateur solution. Instead, I’m implementing asset fingerprinting to keep…

Tweeting New Jekyll Posts From Github Actions - Part 2

I previously wrote about my experience attempting to use Github Actions to post a tweet every time I publish a new post on my self-hosted Jekyll/S3/Cloudfront blog. I managed to get to a working solution that was too complicated, so I’m trying another approach. I was following this post by Dave Brock where he described using the commit message as the entire tweet - so every commit message and git…

Tweeting New Jekyll Posts From Github Actions

This site is built with Jekyll and hosted on Amazon S3, with Cloudfront as the CDN. I recently did some work to make the deployments automated with Github Actions. I wrote about that experience already, but the current workflow is: Every push to main is deployed to a staging environment (I don’t use pull requests) Deployments to production is handled through a github action workflow that is…

Using Separate Database Users for Migrations in Rails

In the past year I have done a lot of work with PostgreSQL (PG) and managing DDL (Data Definition Language) changes. This work has been focused on Java applications which use Flyway to manage migrations, which has less abstractions than Rails migrations. I have been thinking about how to apply some of the lessons I’ve learnt to Rails applications. One of the first changes - or best practices - I…