1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-18 06:28:02 +00:00

Compare commits

...

9 Commits

Author SHA1 Message Date
Mohammad Alyetama
76317a971c Merge bc767a0ad3 into a07a19e6d9 2024-09-30 15:26:39 +08:00
jbranchaud
a07a19e6d9 Add Generate Modern Primary Key Columns as a Postgres TIL 2024-09-27 09:57:33 -05:00
jbranchaud
951a2f04ad Add Find Executables Installed By Go as a Go TIL 2024-09-26 20:54:41 -05:00
jbranchaud
6b9ec224b8 Add Connect To A Database By Color as a Heroku TIL 2024-09-26 08:47:31 -05:00
jbranchaud
c33be14ec2 Add Skip The Front Of An Array With Drop as a Ruby TIL 2024-09-25 22:43:04 -05:00
jbranchaud
2922bbdd6a Add Check Network Quality Stats From The Command Line as a Mac TIL 2024-09-24 11:08:38 -05:00
jbranchaud
2e72ec1160 Add Generate A Model as a Rails TIL 2024-09-20 20:54:52 -05:00
jbranchaud
baab5738e7 Add Convert SVG To Favicon as a Unix TIL 2024-09-15 12:24:18 -05:00
Mohammad Alyetama
bc767a0ad3 Update bew cask command 2022-11-24 17:49:13 -05:00
9 changed files with 249 additions and 2 deletions

View File

@@ -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).
_1440 TILs and counting..._
_1447 TILs and counting..._
---
@@ -376,6 +376,7 @@ _1440 TILs and counting..._
- [Access Go Docs Offline](go/access-go-docs-offline.md)
- [Build For A Specific OS And Architecture](go/build-for-a-specific-os-and-architecture.md)
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
- [Not So Random](go/not-so-random.md)
- [Replace The Current Process With An External Command](go/replace-the-current-process-with-an-external-command.md)
- [Sleep For A Duration](go/sleep-for-a-duration.md)
@@ -390,6 +391,7 @@ _1440 TILs and counting..._
### Heroku
- [Connect To A Database By Color](heroku/connect-to-a-database-by-color.md)
- [Deploy A Review App To A Different Stack](heroku/deploy-a-review-app-to-a-different-stack.md)
- [Diagnose Problems In A Heroku Postgres Database](heroku/diagnose-problems-in-a-heroku-postgres-database.md)
- [Open Dashboard For Specific Add-On](heroku/open-dashboard-for-specific-add-on.md)
@@ -571,6 +573,7 @@ _1440 TILs and counting..._
- [Access All Screen And Video Capture Options](mac/access-all-screen-and-video-capture-options.md)
- [Access System Information On OS X](mac/access-system-information-on-osx.md)
- [Access Unsupported Screen Resolutions With RDM](mac/access-unsupported-screen-resolutions-with-rdm.md)
- [Check Network Quality Stats From The Command Line](mac/check-network-quality-stats-from-the-command-line.md)
- [Clean Up Old Homebrew Files](mac/clean-up-old-homebrew-files.md)
- [Convert An HEIC Image File To JPG](mac/convert-an-heic-image-file-to-jpg.md)
- [Default Screenshot Location](mac/default-screenshot-location.md)
@@ -733,6 +736,7 @@ _1440 TILs and counting..._
- [Fizzbuzz With Common Table Expressions](postgres/fizzbuzz-with-common-table-expressions.md)
- [Force SSL When Making A psql Connection](postgres/force-ssl-when-making-a-psql-connection.md)
- [Generate A UUID](postgres/generate-a-uuid.md)
- [Generate Modern Primary Key Columns](postgres/generate-modern-primary-key-columns.md)
- [Generate Random UUIDs Without An Extension](postgres/generate-random-uuids-without-an-extension.md)
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
- [Generating UUIDs With pgcrypto](postgres/generating-uuids-with-pgcrypto.md)
@@ -897,6 +901,7 @@ _1440 TILs and counting..._
- [Find Or Create A Record With FactoryBot](rails/find-or-create-a-record-with-factory-bot.md)
- [Find Records With Multiple Associated Records](rails/find-records-with-multiple-associated-records.md)
- [Force All Users To Sign Out](rails/force-all-users-to-sign-out.md)
- [Generate A Model](rails/generate-a-model.md)
- [Generate A Rails App From The Main Branch](rails/generate-a-rails-app-from-the-main-branch.md)
- [Generating And Executing SQL](rails/generating-and-executing-sql.md)
- [Get A Quick Approximate Count Of A Large Table](rails/get-a-quick-approximate-count-of-a-large-table.md)
@@ -1222,6 +1227,7 @@ _1440 TILs and counting..._
- [Silence The Output Of A Ruby Statement In Pry](ruby/silence-the-output-of-a-ruby-statement-in-pry.md)
- [Single And Double Quoted String Notation](ruby/single-and-double-quoted-string-notation.md)
- [Skip Specific CVEs When Auditing Your Bundle](ruby/skip-specific-cves-when-auditing-your-bundle.md)
- [Skip The Front Of An Array With Drop](ruby/skip-the-front-of-an-array-with-drop.md)
- [Specify Dependencies For A Rake Task](ruby/specify-dependencies-for-a-rake-task.md)
- [Specify How Random Array#sample Is](ruby/specify-how-random-array-sample-is.md)
- [Split A Float Into Its Integer And Decimal](ruby/split-a-float-into-its-integer-and-decimal.md)
@@ -1356,6 +1362,7 @@ _1440 TILs and counting..._
- [Command Line Length Limitations](unix/command-line-length-limitations.md)
- [Compare Two Variables In A Bash Script](unix/compare-two-variables-in-a-bash-script.md)
- [Configure cd To Behave Like pushd In Zsh](unix/configure-cd-to-behave-like-pushd-in-zsh.md)
- [Convert SVG To Favicon](unix/convert-svg-to-favicon.md)
- [Copying File Contents To System Paste Buffer](unix/copying-file-contents-to-system-paste-buffer.md)
- [Copying Nested Directories With Ditto](unix/copying-nested-directories-with-ditto.md)
- [Count The Lines In A CSV Where A Column Is Empty](unix/count-the-lines-in-a-csv-where-a-column-is-empty.md)

View File

@@ -0,0 +1,26 @@
# Find Executables Installed By Go
When you install an executable using `go install`, it puts that executable in
the `bin` directory designated by the `GOBIN` environment variable. If that env
var isn't set, then it falls back to one of `$GOPATH/bin` or `$HOME/go/bin`.
When I run `go help install`, it tells me as much:
```
Executables are installed in the directory named by the GOBIN environment
variable, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH
environment variable is not set.
```
So, if I am to install something like [`tern`](https://github.com/jackc/tern),
```bash
$ go install github.com/jackc/tern/v2@latest
```
it is going to place that binary in `~/go/bin` for me.
```bash
$ which tern
/Users/jbranchaud/go/bin/tern
```

View File

@@ -0,0 +1,29 @@
# Connect To A Database By Color
All of your PostgreSQL databases in Heroku are given attachment names that use
a random color. This might be _pink_, _brown_, _cobalt_, etc. And the
attachment names then look like `HEROKU_POSTGRESQL_PINK`,
`HEROKU_POSTGRESQL_BROWN`, `HEROKU_POSTGRESQL_COBALT`, etc.
We can connect to a Heroku-managed PostgreSQL instance from the command-line
like so:
```bash
$ heroku pg:psql --app my-app
```
This is going to connect to the _default_ database which is the one with the
`DATABASE_URL` attachment.
There are lots of instances where we may have other databases besides the
primary (e.g. let's say we have a read replica follower). If we want to connect
to that one, we can do so by _color_.
If that database's attachment is `HEROKU_POSTGRESQL_IVORY`, then we'd connect
to it like so:
```bash
$ heroku pg:psql ivory --app my-app
```
[source](https://devcenter.heroku.com/articles/managing-heroku-postgres-using-cli#pg-psql)

View File

@@ -0,0 +1,44 @@
# Check Network Quality Stats From The Command Line
MacOS comes with a little known CLI utility for checking your current network
quality statistics. It is aptly named `networkQuality`.
Here is what a basic run of the tool might output:
```bash
$ networkQuality
...
Downlink: capacity 281.430 Mbps, responsiveness 101 RPM - Uplink: capacity 16.629 Mbps, responsiveness 101 R
Downlink: capacity 285.534 Mbps, responsiveness 101 RPM - Uplink: capacity 16.028 Mbps, responsiveness 101 R
==== SUMMARY ====
Uplink capacity: 22.982 Mbps
Downlink capacity: 288.152 Mbps
Responsiveness: Low (93 RPM)
Idle Latency: 26.375 milliseconds
```
You can get an even more detailed summary with the `-v` option:
```bash
$ networkQuality -v
...
==== SUMMARY ====
Uplink capacity: 18.257 Mbps (Accuracy: High)
Downlink capacity: 469.355 Mbps (Accuracy: High)
Responsiveness: Medium (252 RPM) (Accuracy: High)
Idle Latency: 25.583 milliseconds (Accuracy: High)
Interface: en0
Uplink bytes transferred: 19.750 MB
Downlink bytes transferred: 488.265 MB
Uplink Flow count: 8
Downlink Flow count: 12
Start: 9/24/24, 11:06:20 AM
End: 9/24/24, 11:06:30 AM
OS Version: Version 13.5.2 (Build 22G91)
```
See `man networkQuality` for more details.
[source](https://cyberhost.uk/the-hidden-macos-speedtest-tool-networkquality/)

View File

@@ -0,0 +1,43 @@
# Generate Modern Primary Key Columns
Chances are if you have looked at some examples, blog posts, or real-world
instances of a `create table` statement, it defined the primary key with
`serial` (or `bigserial`).
```sql
create table books (
id serial primary key,
title text not null,
author text not null,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
```
The `serial` syntax is everywhere, but for quite a while now it has not been
the recommended way to define a primary key column for the `int` or `bigint`
data types.
The ["Don't Do This" page of the PostgreSQL
wiki](https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_serial) says
"Don't use serial".
> For new applications, identity columns should be used instead. The serial
> types have some weird behaviors that make schema, dependency, and permission
> management unnecessarily cumbersome.
The modern way to define a primary key column for `int` or `bigint` is with a
generated identity column.
```sql
create table books (
id int primary key generated always as identity,
title text not null,
author text not null,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
```
Check out the PostgreSQL docs for more about [identity
columns](https://www.postgresql.org/docs/17/ddl-identity-columns.html).

26
rails/generate-a-model.md Normal file
View File

@@ -0,0 +1,26 @@
# Generate A Model
The `rails` CLI comes with a variety of generators. Perhaps the mostly common
one to use is the _model_ generator.
The model generator will create a migration and a model file for the entity
that you name. In the following example `Book` will result in a
`app/models/book.rb` file as well as a migration file for a `books` table.
These generators know the singular and plural conventions.
At the end of the command is a series of field definitions containing the field
_name_ and field _type_. These are used in the migration file for defining
columns on the new table.
```bash
bin/rails generate model Book title:string publication_date:date author:string
invoke active_record
create db/migrate/20240920223447_create_books.rb
create app/models/book.rb
invoke rspec
create spec/models/book_spec.rb
```
You may also notice that an `rspec` action was invoked as part of this
generator. That is because I have the `rspec-rails` gem in my project. That gem
hooks into the model generator so that a model spec also gets generated. Handy!

View File

@@ -0,0 +1,44 @@
# Skip The Front Of An Array With Drop
I've long been familiar with Ruby's
[`Array#take`](https://apidock.com/ruby/Array/take) which is a handy way to
grab the first few elements of an array.
```ruby
> [:a, :b, :c, :d, :e, :f, :g].take(4)
=> [:a, :b, :c, :d]
```
But what if I want to skip those first four elements of the array and get what
remains. That is where [`Array#drop`](https://apidock.com/ruby/Array/drop)
comes in.
```ruby
> [:a, :b, :c, :d, :e, :f, :g].drop(4)
=> [:e, :f, :g]
```
How about a couple practical situations for use in a Rails app.
Here I want to segment out the first 6 books and whatever remains:
```
def index
@books = Book.order(created_at: :desc).limit(10)
@featured_books = @books.take(6)
@remaining_books = @books.drop(6)
end
```
Or, what about cleaning up all but the first of these duplicate records:
```
Book
.where(title: "The Murder of Roger Ackroyd")
.drop(1)
.each { |duplicate_book| duplicate_book.destroy }
```
The first record is ignored (dropped from the array) and the following records
are processed by the `#each` where they get destroyed.

View File

@@ -0,0 +1,28 @@
# Convert SVG To Favicon
The imagemagick `convert` CLI tool can convert an SVG file into a transparent
favicon (ICO) file with the different standard sizes.
Assuming the background that we want to make transparent is white, then include
`-transparent white` and then to resize the icon include `-define
icon:auto-resize ...`. Point to the `image.svg` to be converted and specify the
name of the output file (`favicon.ico`).
```bash
$ convert image.svg -transparent white -define icon:auto-resize=16,32,48,64,128 favicon.ico
```
We can then use the `identify` CLI to inspect the `favicon.ico` file to see
that the above worked.
```bash
$ identify favicon.ico
favicon.ico[0] ICO 16x16 16x16+0+0 8-bit sRGB 0.000u 0:00.002
favicon.ico[1] ICO 32x32 32x32+0+0 8-bit sRGB 0.000u 0:00.004
favicon.ico[2] ICO 48x48 48x48+0+0 8-bit sRGB 0.000u 0:00.004
favicon.ico[3] ICO 64x64 64x64+0+0 8-bit sRGB 0.000u 0:00.004
favicon.ico[4] ICO 128x128 128x128+0+0 8-bit sRGB 99678B 0.000u 0:00.003
```
[source](https://www.joshmcarthur.com/2024/06/19/Auto-resizing-images-for-.ico-files.html)

View File

@@ -6,7 +6,7 @@ convert it using the `ebook-convert` binary from `Calibre`.
First, install `Calibre`:
```bash
$ brew cask install calibre
$ brew install --cask calibre
```
Then convert your ePub using `ebook-convert`: