mirror of
https://github.com/jbranchaud/til
synced 2026-01-21 07:58:02 +00:00
Compare commits
1 Commits
53e31ab734
...
5832cc06bc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5832cc06bc |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1620 TILs and counting..._
|
_1613 TILs and counting..._
|
||||||
|
|
||||||
See some of the other learning resources I work on:
|
See some of the other learning resources I work on:
|
||||||
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
|
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
|
||||||
@@ -224,7 +224,6 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
|
|
||||||
### Docker
|
### Docker
|
||||||
|
|
||||||
- [Check Postgres Version Running In Docker Container](docker/check-postgres-version-running-in-docker-container.md)
|
|
||||||
- [Configure Different Host And Container Ports](docker/configure-different-host-and-container-ports.md)
|
- [Configure Different Host And Container Ports](docker/configure-different-host-and-container-ports.md)
|
||||||
- [List Running Docker Containers](docker/list-running-docker-containers.md)
|
- [List Running Docker Containers](docker/list-running-docker-containers.md)
|
||||||
- [Prevent Containers From Running On Startup](docker/prevent-containers-from-running-on-startup.md)
|
- [Prevent Containers From Running On Startup](docker/prevent-containers-from-running-on-startup.md)
|
||||||
@@ -496,7 +495,6 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [Analyze Your Website Performance](internet/analyze-your-website-performance.md)
|
- [Analyze Your Website Performance](internet/analyze-your-website-performance.md)
|
||||||
- [Check Your Public IP Address](internet/check-your-public-ip-address.md)
|
- [Check Your Public IP Address](internet/check-your-public-ip-address.md)
|
||||||
- [Digraph Unicode Characters Have A Titlecase](internet/digraph-unicode-characters-have-a-titlecase.md)
|
- [Digraph Unicode Characters Have A Titlecase](internet/digraph-unicode-characters-have-a-titlecase.md)
|
||||||
- [Download A Google Doc As Specific Format](internet/download-a-google-doc-as-specific-format.md)
|
|
||||||
- [Enable Keyboard Shortcuts In Gmail](internet/enable-keyboard-shortcuts-in-gmail.md)
|
- [Enable Keyboard Shortcuts In Gmail](internet/enable-keyboard-shortcuts-in-gmail.md)
|
||||||
- [Exclude AI Overview From Google Search](internet/exclude-ai-overview-from-google-search.md)
|
- [Exclude AI Overview From Google Search](internet/exclude-ai-overview-from-google-search.md)
|
||||||
- [Exclude Whitespace Changes From GitHub Diffs](internet/exclude-whitespace-changes-from-github-diffs.md)
|
- [Exclude Whitespace Changes From GitHub Diffs](internet/exclude-whitespace-changes-from-github-diffs.md)
|
||||||
@@ -694,11 +692,9 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
|
|
||||||
### Mise
|
### Mise
|
||||||
|
|
||||||
- [Create Umbrella Task For All Test Tasks](mise/create-umbrella-task-for-all-test-tasks.md)
|
|
||||||
- [List The Files Being Loaded By Mise](mise/list-the-files-being-loaded-by-mise.md)
|
- [List The Files Being Loaded By Mise](mise/list-the-files-being-loaded-by-mise.md)
|
||||||
- [Preserve Color Output For Task Command](mise/preserve-color-output-for-task-command.md)
|
- [Preserve Color Output For Task Command](mise/preserve-color-output-for-task-command.md)
|
||||||
- [Read Existing Dot Env File Into Env Vars](mise/read-existing-dot-env-file-into-env-vars.md)
|
- [Read Existing Dot Env File Into Env Vars](mise/read-existing-dot-env-file-into-env-vars.md)
|
||||||
- [Run A Command With Specific Tool Version](mise/run-a-command-with-specific-tool-version.md)
|
|
||||||
|
|
||||||
### MongoDB
|
### MongoDB
|
||||||
|
|
||||||
@@ -838,7 +834,6 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [Escaping String Literals With Dollar Quoting](postgres/escaping-string-literals-with-dollar-quoting.md)
|
- [Escaping String Literals With Dollar Quoting](postgres/escaping-string-literals-with-dollar-quoting.md)
|
||||||
- [Export Query Results To A CSV](postgres/export-query-results-to-a-csv.md)
|
- [Export Query Results To A CSV](postgres/export-query-results-to-a-csv.md)
|
||||||
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
||||||
- [Fetch Data From An Endpoint In SQL](postgres/fetch-data-from-an-endpoint-in-sql.md)
|
|
||||||
- [Fetch Specific Number Of Results](postgres/fetch-specific-number-of-results.md)
|
- [Fetch Specific Number Of Results](postgres/fetch-specific-number-of-results.md)
|
||||||
- [Find Duplicate Records In Table Without Unique Id](postgres/find-duplicate-records-in-table-without-unique-id.md)
|
- [Find Duplicate Records In Table Without Unique Id](postgres/find-duplicate-records-in-table-without-unique-id.md)
|
||||||
- [Find Records That Contain Duplicate Values](postgres/find-records-that-contain-duplicate-values.md)
|
- [Find Records That Contain Duplicate Values](postgres/find-records-that-contain-duplicate-values.md)
|
||||||
@@ -962,7 +957,6 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
### Python
|
### Python
|
||||||
|
|
||||||
- [Access Instance Variables](python/access-instance-variables.md)
|
- [Access Instance Variables](python/access-instance-variables.md)
|
||||||
- [Break Debugger On First Line Of Program](python/break-debugger-on-first-line-of-program.md)
|
|
||||||
- [Create A Dummy DataFrame In Pandas](python/create-a-dummy-dataframe-in-pandas.md)
|
- [Create A Dummy DataFrame In Pandas](python/create-a-dummy-dataframe-in-pandas.md)
|
||||||
- [Dunder Methods](python/dunder-methods.md)
|
- [Dunder Methods](python/dunder-methods.md)
|
||||||
- [Override The Boolean Context Of A Class](python/override-the-boolean-context-of-a-class.md)
|
- [Override The Boolean Context Of A Class](python/override-the-boolean-context-of-a-class.md)
|
||||||
@@ -980,7 +974,6 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [Add A Generated Column To A PostgreSQL Table](rails/add-a-generated-column-to-a-postgresql-table.md)
|
- [Add A Generated Column To A PostgreSQL Table](rails/add-a-generated-column-to-a-postgresql-table.md)
|
||||||
- [Add A Reference Column With An Index](rails/add-a-reference-column-with-an-index.md)
|
- [Add A Reference Column With An Index](rails/add-a-reference-column-with-an-index.md)
|
||||||
- [Add ActiveRecord Error Not Tied To Any Attribute](rails/add-activerecord-error-not-tied-to-any-attribute.md)
|
- [Add ActiveRecord Error Not Tied To Any Attribute](rails/add-activerecord-error-not-tied-to-any-attribute.md)
|
||||||
- [Add Color To The IRB Console Prompt](rails/add-color-to-the-irb-console-prompt.md)
|
|
||||||
- [Add React With Webpacker To A New Rails App](rails/add-react-with-webpacker-to-a-new-rails-app.md)
|
- [Add React With Webpacker To A New Rails App](rails/add-react-with-webpacker-to-a-new-rails-app.md)
|
||||||
- [Add timestamptz Columns With The Migration DSL](rails/add-timestamptz-columns-with-the-migration-dsl.md)
|
- [Add timestamptz Columns With The Migration DSL](rails/add-timestamptz-columns-with-the-migration-dsl.md)
|
||||||
- [Adjust The Production Log Level](rails/adjust-the-production-log-level.md)
|
- [Adjust The Production Log Level](rails/adjust-the-production-log-level.md)
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
# Check Postgres Version Running In Docker Container
|
|
||||||
|
|
||||||
I have a docker container that I'm using to run a PostgreSQL development
|
|
||||||
database on my local machine. It was a while ago when I set it up, so I can't
|
|
||||||
remember specifically which major version of PostgreSQL I am using.
|
|
||||||
|
|
||||||
I use `docker ps` to list the names of each container.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ docker ps --format "{{.Names}}"
|
|
||||||
still-postgres-1
|
|
||||||
better_reads-postgres-1
|
|
||||||
```
|
|
||||||
|
|
||||||
I grab the one I am interested in. In this case, that is `still-postgres-1`.
|
|
||||||
|
|
||||||
Then I can execute a `select version()` statement with `psql` against the
|
|
||||||
container with that name like so:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ docker exec still-postgres-1 psql -U postgres -c "select version()";
|
|
||||||
version
|
|
||||||
---------------------------------------------------------------------------------------------------------------------
|
|
||||||
PostgreSQL 16.2 (Debian 16.2-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
|
|
||||||
(1 row)
|
|
||||||
```
|
|
||||||
|
|
||||||
And there I have it. I'm running Postgres v16 in this container.
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
# Download A Google Doc As Specific Format
|
|
||||||
|
|
||||||
I was recently given a public Google Doc URL and I was curious if I could
|
|
||||||
download it from the command line. I didn't want to have to install special CLI
|
|
||||||
though. I was hoping to use something like `curl`.
|
|
||||||
|
|
||||||
A brief chat with Claude and I learned that not only can I use `curl`, but I
|
|
||||||
can specify the format in the _export_ URL.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ export GOOGLE_DOC_URL="https://docs.google.com/document/d/157rMgHeBf76T9TZnUjtrUyyS2XPwG0tObr-OjYNfMaI"
|
|
||||||
|
|
||||||
$ echo $GOOGLE_DOC_URL
|
|
||||||
https://docs.google.com/document/d/157rMgHeBf76T9TZnUjtrUyyS2XPwG0tObr-OjYNfMaI
|
|
||||||
|
|
||||||
$ curl -L "$GOOGLE_DOC_URL/export?format=pdf" -o doc.pdf
|
|
||||||
% Total % Received % Xferd Average Speed Time Time Time Current
|
|
||||||
Dload Upload Total Spent Left Speed
|
|
||||||
100 414 0 414 0 0 2763 0 --:--:-- --:--:-- --:--:-- 2895
|
|
||||||
100 16588 0 16588 0 0 56214 0 --:--:-- --:--:-- --:--:-- 167k
|
|
||||||
|
|
||||||
$ ls doc.pdf
|
|
||||||
doc.pdf
|
|
||||||
```
|
|
||||||
|
|
||||||
I append `/export` and then include the `?format=pdf` query param to specify
|
|
||||||
that I want the document to be exported in PDF format. With the `-o` flag I can
|
|
||||||
specify the name and extension of the output file.
|
|
||||||
|
|
||||||
This is a handy on its own, but noticing that Google Docs supports other export
|
|
||||||
formats, I thought it would be useful to go back-and-forth with Claude to
|
|
||||||
sketch out a script that can do this and prompt me (with `fzf`) for the file
|
|
||||||
type -- [here is the gist for
|
|
||||||
`gdoc-download`](https://gist.github.com/jbranchaud/cf3d2028107a1bd8484eed7cca0fcdab).
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
# Create Umbrella Task For All Test Tasks
|
|
||||||
|
|
||||||
When I was first sketching out the [`mise`
|
|
||||||
tasks](https://mise.jdx.dev/tasks/running-tasks.html) for a Rails app, I added
|
|
||||||
the following two tasks. One is for running all the `rspec` tests. The Other is
|
|
||||||
for running all the `vitest` (JavaScript) tests.
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[tasks."test:rspec"]
|
|
||||||
run = "unbuffer bundle exec rspec"
|
|
||||||
description = "Run RSpec tests"
|
|
||||||
depends = ["bundle-install"]
|
|
||||||
|
|
||||||
[tasks."test:vitest"]
|
|
||||||
run = "unbuffer yarn test run"
|
|
||||||
description = "Run Vitest tests"
|
|
||||||
depends = ["node-install"]
|
|
||||||
```
|
|
||||||
|
|
||||||
I didn't want to have to invoked both of this individually every time I wanted
|
|
||||||
to run the full suite. So I added a `test:all` task to do it all.
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[tasks."test:all"]
|
|
||||||
description = "Run all tests (RSpec and Vitest)"
|
|
||||||
run = [
|
|
||||||
"unbuffer bundle exec rspec",
|
|
||||||
"unbuffer yarn test run",
|
|
||||||
]
|
|
||||||
description = "Run RSpec tests"
|
|
||||||
depends = ["bundle-install", "node-install"]
|
|
||||||
```
|
|
||||||
|
|
||||||
This worked (for now). But it ate at me, for a couple reasons. I had to
|
|
||||||
duplicate everything about the existing `test:rspec` and `test:vitest` tasks.
|
|
||||||
And this didn't account for a new kind of test task being added (e.g.
|
|
||||||
`test:e2e`).
|
|
||||||
|
|
||||||
Instead, I can rely on `depends` and wildcards to achieve this without the
|
|
||||||
duplication which makes it more future-proof.
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[tasks."test:all"]
|
|
||||||
description = "Run all tests (RSpec and Vitest)"
|
|
||||||
depends = ["test:*"]
|
|
||||||
```
|
|
||||||
|
|
||||||
Running `mise run test:all` won't execute its own command, but because it
|
|
||||||
depends on all other `test:*` tasks, the tests will get run through those
|
|
||||||
dependencies.
|
|
||||||
|
|
||||||
This task naming pattern also allows for calling all tests with `mise run
|
|
||||||
"test:**"`.
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
# Run A Command With Specific Tool Version
|
|
||||||
|
|
||||||
Because I'm using `mise` to manage the versions of tools like Node, I can
|
|
||||||
execute commands in the context of specific versions. Behind the scenes `mise`
|
|
||||||
makes sure I have the necessary tool(s) installed at the desired version(s).
|
|
||||||
|
|
||||||
So, [`mise exec` command](https://mise.jdx.dev/cli/exec.html) will default to
|
|
||||||
using the latest version of a tool if I haven't been more specific. At the time
|
|
||||||
of this writing, for Node, that is v23.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ mise exec node -- node --version
|
|
||||||
v23.9.0
|
|
||||||
```
|
|
||||||
|
|
||||||
To be specific I could specify the major version with `node@23` like so:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
mise exec node@23 -- npx repomix
|
|
||||||
Need to install the following packages:
|
|
||||||
repomix@0.2.39
|
|
||||||
Ok to proceed? (y) y
|
|
||||||
|
|
||||||
...
|
|
||||||
```
|
|
||||||
|
|
||||||
Or if I wanted to use a different, older version of Node, I could specify that
|
|
||||||
as well. We can see it will first install that and then execute the command:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ mise exec node@22 -- npx repomix
|
|
||||||
gpg: Signature made Tue Feb 11 04:44:53 2025 CST
|
|
||||||
gpg: using RSA key C0D6248439F1D5604AAFFB4021D900FFDB233756
|
|
||||||
gpg: Good signature from "Antoine du Hamel <duhamelantoine1995@gmail.com>" [unknown]
|
|
||||||
|
|
||||||
📦 Repomix v0.2.39
|
|
||||||
|
|
||||||
...
|
|
||||||
```
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
# Fetch Data From An Endpoint In SQL
|
|
||||||
|
|
||||||
The [`pgsql-http` extension](https://github.com/pramsey/pgsql-http) provides a
|
|
||||||
variety of functions for allowing PostgreSQL to act as an HTTP client. This is
|
|
||||||
a bit unorthodox and may not be a good idea in production systems. That said,
|
|
||||||
it is cool that it is possible. Let's look at an example of it.
|
|
||||||
|
|
||||||
First, I've installed the extension on the Docker container running my local
|
|
||||||
Postgres server.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ docker exec -it still-postgres-1 bash
|
|
||||||
|
|
||||||
$ apt-get update
|
|
||||||
|
|
||||||
$ apt-get install postgres-16-http # I'm running Postgres v16
|
|
||||||
|
|
||||||
$ exit
|
|
||||||
```
|
|
||||||
|
|
||||||
Then I'll connect to a `psql` session in that container for the `postgres` database.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ docker exec still-postgres-1 psql -U postgres -d postgres
|
|
||||||
```
|
|
||||||
|
|
||||||
Then I enable the extension.
|
|
||||||
|
|
||||||
```sql
|
|
||||||
> create extension if not exists http;
|
|
||||||
CREATE EXTENSION
|
|
||||||
```
|
|
||||||
|
|
||||||
Now I can point a PostgreSQL statement at a live endpoint like
|
|
||||||
[https://httpbun.com/ip](https://httpbun.com/ip) which will respond with a
|
|
||||||
chunk of JSON including the IP address for that project's server. I do this
|
|
||||||
using `http_get` which makes a `GET` request to the given endpoint. The body is
|
|
||||||
included in the result set.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
> select content from http_get('http://httpbun.com/ip');
|
|
||||||
content
|
|
||||||
-----------------------------
|
|
||||||
{ +
|
|
||||||
"origin": "73.75.236.101"+
|
|
||||||
} +
|
|
||||||
|
|
||||||
(1 row)
|
|
||||||
```
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
# Break Debugger On First Line Of Program
|
|
||||||
|
|
||||||
One of the things I appreciate about how
|
|
||||||
[Delve](https://github.com/go-delve/delve) (the debugger for Go) works by
|
|
||||||
default is that when you start it up, it immediately breaks on the first line
|
|
||||||
of the program. This is as good a starting point as any regardless of whether
|
|
||||||
you have other breakpoints set.
|
|
||||||
|
|
||||||
As I was reading through the VS Code Python Debugger configuration docs, I
|
|
||||||
noticed [an option called
|
|
||||||
`stopOnEntry`](https://code.visualstudio.com/docs/python/debugging#_stoponentry).
|
|
||||||
It is turned off by default, but if you turn it on, then you get the same
|
|
||||||
behavior as I described for Delve. Nice!
|
|
||||||
|
|
||||||
This can be configured in a `.vscode/launch.json` file:
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"version": "0.2.0",
|
|
||||||
"configurations": [
|
|
||||||
{
|
|
||||||
"name": "Python Debugger: Current File",
|
|
||||||
"type": "debugpy",
|
|
||||||
"request": "launch",
|
|
||||||
"program": "${file}",
|
|
||||||
"console": "integratedTerminal",
|
|
||||||
"stopOnEntry": true
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Now, when running this debugger configuration profile, you'll break the
|
|
||||||
debugger on the first line of the program. From there add more breakpoints,
|
|
||||||
start stepping through, etc.
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
# Add Color To The IRB Console Prompt
|
|
||||||
|
|
||||||
IRB has a little-known [`Color`
|
|
||||||
module](https://docs.ruby-lang.org/en/3.2/IRB/Color.html) with some helpers for
|
|
||||||
adding a splash of color to the IRB prompt. I like to clearly differentiate the
|
|
||||||
environment I'm in when connecting to the `rails console`, so I have a
|
|
||||||
customize the prompt to display and colorize the current environment.
|
|
||||||
|
|
||||||
I can wrap any string in ANSI escape codes that instruct the terminal to style
|
|
||||||
the text with color. For instance, here is how I can style the word `DEV` to be
|
|
||||||
inverted against a blue background.
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
IRB::Color.colorize("DEV", [:BLUE, :BOLD, :REVERSE])
|
|
||||||
```
|
|
||||||
|
|
||||||
which will clearly stand out from `PROD` against a red background:
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
IRB::Color.colorize("PROD", [:RED, :BOLD, :REVERSE])
|
|
||||||
```
|
|
||||||
|
|
||||||
Here is a full example of customizing the prompt from the
|
|
||||||
`config/application.rb` file.
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
module MyApp
|
|
||||||
class Application < Rails::Application
|
|
||||||
# ...
|
|
||||||
|
|
||||||
console do
|
|
||||||
# Get the application module name and convert to kebab-case
|
|
||||||
app_name = Rails.application.class.module_parent.name
|
|
||||||
kebab_name = app_name.underscore.dasherize
|
|
||||||
|
|
||||||
# Environment color coding
|
|
||||||
env_colors = {
|
|
||||||
"development" => IRB::Color.colorize("DEV", [:BLUE, :BOLD, :REVERSE]),
|
|
||||||
"production" => IRB::Color.colorize("PROD", [:RED, :BOLD, :REVERSE]),
|
|
||||||
"test" => IRB::Color.colorize("TEST", [:YELLOW, :BOLD, :REVERSE]),
|
|
||||||
}
|
|
||||||
|
|
||||||
colored_env = "(#{env_colors[Rails.env]})"
|
|
||||||
|
|
||||||
# Docs: https://docs.ruby-lang.org/en/3.2/IRB.html#module-IRB-label-Customizing+the+IRB+Prompt
|
|
||||||
IRB.conf[:PROMPT][:RAILS_APP] = {
|
|
||||||
PROMPT_I: "#{kebab_name}#{colored_env}> ",
|
|
||||||
PROMPT_N: "#{kebab_name}#{colored_env}* ",
|
|
||||||
PROMPT_S: "#{kebab_name}#{colored_env}% ",
|
|
||||||
PROMPT_C: "#{kebab_name}#{colored_env}? ",
|
|
||||||
RETURN: "=> %s\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Set it as the current prompt
|
|
||||||
IRB.conf[:PROMPT_MODE] = :RAILS_APP
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
The Ruby docs have more about [IRB Prompt
|
|
||||||
Customization](https://docs.ruby-lang.org/en/3.2/IRB.html#module-IRB-label-Customizing+the+IRB+Prompt).
|
|
||||||
Reference in New Issue
Block a user