Weeknotes 161
28th July, 2024
“Passport shenanigans”
-
I upgraded to macOS Sonama and it seems to have happened without incident. I’m always scared my development environment will be broken is some hard to debug way (which would be bad because it would stop me earning) but all good so far.
-
Playing around with a LiveView app this week I wondered why my LiveView kept falling back to long polling instead of using a web socket. Long polling is an alternative transport for LiveViews for when websockets are blocked by corporate networks and the like, and are far less efficient, but a necessary backup.
Apparently, this can happen in development mode, for reasons. There is a session cookie called
phx:longpoll
. The solution is to clear that session cookie and the websocket will re-connect. It does happen with annoying regularity though. -
Counting values stored in a Postgres text array (
text[]
) column can be achieved by “unnesting” the values. This creates a row for each value stored in the text array column making counting easy.Given values like this:
| locales | |------------------------| | {en-US,en-GB,de,sv,nl} | | {en-GB} | | {en-GB,nb} | | {en-US} | | {en-US} | | {en-US} |
You write this query:
SELECT locale, COUNT(*) as count FROM <table>, unnest(locales) as locale GROUP BY locale ORDER BY count DESC;
Which yields this result:
| locale | count | |--------|-------| | en-US | 4 | | en-GB | 3 | | sv | 1 | | de | 1 | | nl | 1 | | nb | 1 |
Lovely stuff.
-
As you’ll know reader, I’m supposed to be travelling to Canada in September for RailsWorld and a short holiday.
It seems that if you are a dual citizen of the UK and Canada (as I am) then you need a Canadian passport in order to travel to Canada. I know this because I applied for an Electronic travel authorization and I was denied me for “being a Canadian citizen” 🙄
This is madness, right? I’ve been to Canada maybe 4 or 5 times and never had this problem before, but it looks like the rules were changed in 2016 and now here I am.
So now there is a mad scramble to get a Canadian passport before September!
-
When Rails is in development mode it will tell show a warning error page if you have pending migrations aka migrations that need to be run. Normally this is what you want because the code you are running likely depends on that database state being up-to-date.
However, this week I was fixing some migrations that weren’t inserting data correctly and I didn’t want all the other migrations to run because I was working through them one-by-one.
You can turn that pending migration check off with this in
config/environments/development.rb
.config.active_record.migration_error = false
-
Speaking of which, I’ve been writing way more SQL than usual this week. I’m sure I’ve mentioned this before but I don’t write a great deal of SQL, ActiveRecord (the ORM) shields you from it most of the time. When I do, I struggle a bit if I’m being honest.
This is where AI comes in. I’ve had really great success with using Claude 3.5 Sonnet this week to help me get started with some fairly involved queries using database features I have never used before. It’s not that it’s correct all the time, because it isn’t. But I struggle with starting things. Using AI gets me started so that I can iterate on what it produces until I have a working solution is a real life saver.
-
I finally bought a licence to TablePlus because I’ve been free-loading for longer than I’d care to admit.