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

Compare commits

...

12 Commits

Author SHA1 Message Date
Dylan Irlbeck
d040854204 Merge 5365e75267 into 84e2c9c6f4 2024-05-21 10:06:13 +05:30
jbranchaud
84e2c9c6f4 Add Override Table Name For Prisma Model as a Prisma TIL 2024-05-20 09:05:44 -05:00
jbranchaud
39614e975e Add Find Top-Level Directories Matching A Pattern as a Unix TIL 2024-05-19 10:08:45 -05:00
jbranchaud
44e626a086 Add source link for latest TIL 2024-05-16 14:50:34 -05:00
jbranchaud
48d2ecffa0 Add Get Row Count For Most Recent Query as a Postgres TIL 2024-05-16 14:49:37 -05:00
jbranchaud
6fb3b95ade Add Check If Database And Schema Are Not In Sync as a Prisma TIL 2024-05-15 12:21:38 -05:00
jbranchaud
51880975d4 Add Set Value On Null JSON Column as a MySQL TIL 2024-05-14 11:01:17 -05:00
jbranchaud
03980ab291 Add Format Time Zone Identifier as a JavaScript TIL 2024-05-13 12:59:15 -05:00
jbranchaud
bef492fe3a Add Generate Base64 Encoding Without Newlines as a Unix TIL 2024-05-05 11:39:10 -05:00
jbranchaud
36004e6d20 Add Remove Untracked Files From A Directory as a Git TIL 2024-05-03 10:11:00 -05:00
jbranchaud
18e52b9dbb Add Get A List Of Locales On Your System as a Unix TIL 2024-05-02 14:31:01 -05:00
Dylan Irlbeck
5365e75267 Update git show TIL 2020-04-29 22:17:44 -05:00
11 changed files with 358 additions and 4 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).
_1424 TILs and counting..._
_1433 TILs and counting..._
---
@@ -321,6 +321,7 @@ _1424 TILs and counting..._
- [Quicker Commit Fixes With The Fixup Flag](git/quicker-commit-fixes-with-the-fixup-flag.md)
- [Rebase Commits With An Arbitrary Command](git/rebase-commits-with-an-arbitrary-command.md)
- [Reference A Commit Via Commit Message Pattern Matching](git/reference-a-commit-via-commit-message-pattern-matching.md)
- [Remove Untracked Files From A Directory](git/remove-untracked-files-from-a-directory.md)
- [Rename A Remote](git/rename-a-remote.md)
- [Renaming A Branch](git/renaming-a-branch.md)
- [Resetting A Reset](git/resetting-a-reset.md)
@@ -461,6 +462,7 @@ _1424 TILs and counting..._
- [Find Where Yarn Is Installing Binaries](javascript/find-where-yarn-is-installing-binaries.md)
- [for...in Iterates Over Object Properties](javascript/for-in-iterates-over-object-properties.md)
- [Format A Decimal To A Fixed Number Of Digits](javascript/format-a-decimal-to-a-fixed-number-of-digits.md)
- [Format Time Zone Identifier](javascript/format-time-zone-identifier.md)
- [Formatting Values With Units For Display](javascript/formatting-values-with-units-for-display.md)
- [Freeze An Object, Sorta](javascript/freeze-an-object-sorta.md)
- [Generate A V4 UUID In The Browser](javascript/generate-a-v4-uuid-in-the-browser.md)
@@ -609,6 +611,7 @@ _1424 TILs and counting..._
- [List Databases And Tables](mysql/list-databases-and-tables.md)
- [Run Statements In A Transaction](mysql/run-statements-in-a-transaction.md)
- [Select Rows After An Offset](mysql/select-rows-after-an-offset.md)
- [Set Value On Null JSON Column](mysql/set-value-on-null-json-column.md)
- [Show Create Statement For A Table](mysql/show-create-statement-for-a-table.md)
- [Show Tables That Match A Pattern](mysql/show-tables-that-match-a-pattern.md)
- [Show Indexes For A Table](mysql/show-indexes-for-a-table.md)
@@ -731,6 +734,7 @@ _1424 TILs and counting..._
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
- [Generating UUIDs With pgcrypto](postgres/generating-uuids-with-pgcrypto.md)
- [Get A Quick Approximate Count Of A Table](postgres/get-a-quick-approximate-count-of-a-table.md)
- [Get Row Count For Most Recent Query](postgres/get-row-count-for-most-recent-query.md)
- [Get The Size On Disk of An Index](postgres/get-the-size-on-disk-of-an-index.md)
- [Get The Size Of A Database](postgres/get-the-size-of-a-database.md)
- [Get The Size Of A Table](postgres/get-the-size-of-a-table.md)
@@ -821,10 +825,12 @@ _1424 TILs and counting..._
- [Apply Separate Formatting With A Blank Line](prisma/apply-separate-formatting-with-a-blank-line.md)
- [Batch Insert Records With createMany](prisma/batch-insert-records-with-create-many.md)
- [Check If Database And Schema Are Not In Sync](prisma/check-if-database-and-schema-are-not-in-sync.md)
- [Configure Client To Log SQL Queries](prisma/configure-client-to-log-sql-queries.md)
- [Execute A Raw SQL Query](prisma/execute-a-raw-sql-query.md)
- [Grab A Limited Set Of Records](prisma/grab-a-limited-set-of-records.md)
- [Open Connections To Multiple Databases](prisma/open-connections-to-multiple-databases.md)
- [Override Table Name For Prisma Model](prisma/override-table-name-for-prisma-model.md)
- [Specify Alternate Location For Prisma Schema](prisma/specify-alternate-location-for-prisma-schema.md)
### Python
@@ -1372,10 +1378,13 @@ _1424 TILs and counting..._
- [Find Files With fd](unix/find-files-with-fd.md)
- [Find Newer Files](unix/find-newer-files.md)
- [Find Occurrences Of Multiple Values With Ripgrep](unix/find-occurrences-of-multiple-values-with-ripgrep.md)
- [Find Top-Level Directories Matching A Pattern](unix/find-top-level-directories-matching-a-pattern.md)
- [Fix Unlinked Node Binaries With asdf](unix/fix-unlinked-node-binaries-with-asdf.md)
- [Forward Multiple Ports Over SSH](unix/forward-multiple-ports-over-ssh.md)
- [Generate A SAML Key And Certificate Pair](unix/generate-a-saml-key-and-certificate-pair.md)
- [Generate Base64 Encoding Without Newlines](unix/generate-base64-encoding-without-newlines.md)
- [Generate Random 20-Character Hex String](unix/generate-random-20-character-hex-string.md)
- [Get A List Of Locales On Your System](unix/get-a-list-of-locales-on-your-system.md)
- [Get Matching Filenames As Output From Grep](unix/get-matching-filenames-as-output-from-grep.md)
- [Get The Unix Timestamp](unix/get-the-unix-timestamp.md)
- [Global Substitution On The Previous Command](unix/global-substitution-on-the-previous-command.md)

View File

@@ -0,0 +1,26 @@
# Remove Untracked Files From A Directory
Let's say I have a directory (`spec/cassettes`) full of a ton of generated YAML
files. Most of these files are tracked by git. However, I just generated a
bunch of new ones that are untracked. For whatever reason, I don't want these
files. I need to delete them.
Running `rm` on each of them is going to be too tedious. And it is tricky to
target them for a bulk delete since there are a ton of other files in that
directory that I want to keep.
One way to approach this is have `git ls-files` help out with listing all files in the
directory that are untracked. The `--others` flag filters to untracked files.
```bash
git ls-files --others --exclude-standard spec/cassettes
```
From there, I can pipe it to `rm` (with `xargs` collapsing all the files into a
single line):
```bash
git ls-files --others --exclude-standard spec/cassettes | xargs rm
```
See `man git-ls-files` for more details.

View File

@@ -2,16 +2,16 @@
Sometimes you want to view a file on another branch (without switching
branches). That is, you want to view the version of that file as it exists
on that branch. `git show` can help. If your branch is named `my_feature` and
on that branch. `git show` can help. If the other branch is named `some_branch` and
the file you want to see is `app/models/users.rb`, then your command should
look like this:
```
$ git show my_feature:app/models/users.rb
$ git show some_branch:app/models/users.rb
```
You can even tab-complete the filename as you type it out.
See `man git-show` for more details.
[source](http://stackoverflow.com/questions/7856416/view-a-file-in-a-different-git-branch-without-changing-branches)
[source](https://stackoverflow.com/questions/7856416/view-a-file-in-a-different-git-branch-without-changing-branches)

View File

@@ -0,0 +1,34 @@
# Format Time Zone Identifier
Though there are surely libraries that can help with this task, we now have
full support in the [`Intl.DateTimeFormat`
API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat)
for formatting a date's time zone identifier in various ways.
To do this, we have to create a formatter specifying the locale, the `timeZone`
option, and any formatting options. For the formatting, I'll try the
`timeZoneName` with both `short` and `longGeneric`.
Then we `formatToParts` on any date object and extract the `timeZoneName`
value:
```javascript
const options = { timeZone: 'America/Chicago', timeZoneName: "short" }
const formatter = new Intl.DateTimeFormat("en-US", options)
formatter.formatToParts(new Date()).find((part) => part.type === "timeZoneName").value
//=> 'CDT'
```
Now, let's try this for `longGeneric`:
```javascript
const options = { timeZone: 'America/Chicago', timeZoneName: "longGeneric" }
const formatter = new Intl.DateTimeFormat("en-US", options)
formatter.formatToParts(new Date()).find((part) => part.type === "timeZoneName").value
//=> 'Central Time'
```
There are several more options for the `timeZoneName` as well as a bunch more
you can do with the `Intl.DateTimeFormat` API.

View File

@@ -0,0 +1,44 @@
# Set Value On Null JSON Column
To set a key-value pair on a JSON field, you can reach for
[MySQL's `json_set`](https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html#function_json-set)
or one of [the other JSON setter
functions](https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html).
However, if the JSON field you are updating is `null`, you might get an
unexpected result.
```sql
> update User
set metadata = json_set(metadata, '$.discord_id', 'discord_123')
where id = 123;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
```
We can see that the `where` clause matched on a single row as expected, but
right above that it says _0 rows affected_.
What happened?
The `json_set` function is not able to set a key-value pair on `null`. It needs
a JSON object to work on.
There are a number of ways to get around this. I find that
[`coalesce`](https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#function_coalesce)
is a natural way to handle this. If `metadata` happens to be `null`, then we
_coalesce_ it to `'{}'` (an empty object).
```sql
> update User
set metadata = json_set(coalesce(metadata, '{}'), '$.discord_id', 'discord_123')
where id = 123;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
```
It updates as expected. That same statement will work on a row where `metadata`
already contains a JSON object since the `coalesce` will resolve to that
instead of the empty object.

View File

@@ -0,0 +1,65 @@
# Get Row Count For Most Recent Query
Anytime you execute a query in `psql`, there is a _row count_ associated with
that query. This is most naturally understood with a `select` query where a
discreet number of rows are returned. We typically see the row count (e.g. `(19
rows)`) right below the result set.
You can always reference the row count of the most recent query with [the
`:ROW_COUNT`
variable](https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES-ROW-COUNT).
Here we use `\echo` to print it out.
```sql
> select generate_series(2,20);
generate_series
-----------------
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(19 rows)
Time: 12.338 ms
> \echo :ROW_COUNT
19
```
For some queries, like one that induces a pager (e.g. `less`) to be used,
you'll lose track of the row count once the pager closes. This is where being
able to reference the row count without rerunning the query is most useful.
```sql
> select generate_series(2,2000);
Time: 9.815 ms
> \echo :ROW_COUNT
1999
```
Notice, we can also get a row count from other kinds of queries like this
`insert` statement.
```sql
> insert into users (id) values (50001), (50002), (50003);
INSERT 0 3
Time: 2.804 ms
> \echo :ROW_COUNT
3
```
[source](https://postgresql.verite.pro/blog/2024/05/13/advanced-psql-coproc.html)

View File

@@ -0,0 +1,37 @@
# Check If Database And Schema Are Not In Sync
The [`prisma migrate
diff`](https://www.prisma.io/docs/orm/reference/prisma-cli-reference#migrate-diff)
command is a versatile tool that can be used to check if there is a difference
between two sources. In this case, we want to check if our database is in sync
with the `schema.prisma` file for our project.
If we have made changes to the schema file, but haven't yet migrated or pushed
those changes to our local database, then we want to be notified of that
mismatch.
We'll point at the schema file with `--to-schema-datamodel` and at our local
database with `--from-url`.
```bash
npx prisma migrate diff \
--to-schema-datamodel ./prisma/schema.prisma \
--from-url mysql://root@localhost:3309/kcd-products
[*] Changed the `User` table
[+] Added column `metadata`
```
In the case where there is a different, we see an output summary of the diff.
Let's say we've applied our changes (`prisma db push`) to our local database.
If we now run that same command again, we can see that no difference is
detected and our database is in sync with our schema.
```bash
npx prisma migrate diff \
--to-schema-datamodel ./prisma/schema.prisma \
--from-url mysql://root@localhost:3309/kcd-products
No difference detected.
```

View File

@@ -0,0 +1,42 @@
# Override Table Name For Prisma Model
When defining your Prisma schema, you'll add models to your
`prisma/schema.prisma` file that look something like this:
```
model Book {
id BigInt @id @default(autoincrement()) @db.BigInt
title String
author String
publication_year Int
created_at DateTime @default(now())
updated_at DateTime @updatedAt
}
```
The prisma client (ORM-layer) that gets generated will have a `Book` type and
you'll be able to reference the model to, for instance, create a record with
`prisma.book.create(...)`. Both of these things are derived from the model
name: `Book`.
The other thing that is derived from the model name is the name given to the
underlying database table. So you end up with a table called `Book`. You may,
however, prefer a table naming convention where this one would be named `books`
(snake_case and pluralized).
To achieve that, you have to manually override the table name with [the `@@map`
directive](https://www.prisma.io/docs/orm/reference/prisma-schema-reference#map-1).
Add it toward the bottom of the model like so:
```
model Book {
id BigInt @id @default(autoincrement()) @db.BigInt
title String
author String
publication_year Int
created_at DateTime @default(now())
updated_at DateTime @updatedAt
@@map("books")
}
```

View File

@@ -0,0 +1,36 @@
# Find Top-Level Directories Matching A Pattern
I like using [`fd`](https://github.com/sharkdp/fd) as an alternative to `find`.
In my experience it is more intuitive to use. For instance, I wanted to find
all the top-level directories in my current directory that contained the word
`next`. I was able to get the command mostly right by guessing the flags, only
checking the man page once.
On my first attempt, it prompted me with a suggestion for a flag that wasn't
quite right. I tried `--depth`, but it should have been `--maxdepth`.
```bash
$ fd --depth 0 next ./
error: Found argument '--depth' which wasn't expected, or isn't valid in this context
Did you mean --maxdepth?
```
Then I checked the man page for how to specify the file type as _directory_ --
using `-t` or `--type` with `d`.
And here is the command that gets me all top-level directories matching `next`
in my current directory:
```bash
$ fd --maxdepth 1 --type d next ./
bookshelf-nextjs-prisma-postgres
bookshelf-prisma-nextjs-planetscale
my-next-app
next-bookshelf
next-personal-site
next-sanity-v3-example
try-trpc-next
```
See `man fd` for more details.

View File

@@ -0,0 +1,25 @@
# Generate Base64 Encoding Without Newlines
There are a variety of tools that can generate a Base64 encoding of given text.
Most of them that I've encountered have a number of characters at which they
introduce a newline character. Here is `openssl` as an example:
```bash
echo "here is a long bit of text to base64 encode with openssl" | openssl base64
aGVyZSBpcyBhIGxvbmcgYml0IG9mIHRleHQgdG8gYmFzZTY0IGVuY29kZSB3aXRo
IG9wZW5zc2wK
```
[The theory I've seen](https://superuser.com/a/1225139) is that this is to
accommodate 80-character terminal screens when chunks of encoding were included
in emails.
With the `openssl base64` command, there is not an option to exclude the
newlines, but we can pipe it through `tr` to remove them.
```bash
echo "here is a long bit of text to base64 encode with openssl" | \
openssl base64 | \
tr -d '\n'
aGVyZSBpcyBhIGxvbmcgYml0IG9mIHRleHQgdG8gYmFzZTY0IGVuY29kZSB3aXRoIG9wZW5zc2wK
```

View File

@@ -0,0 +1,36 @@
# Get A List Of Locales On Your System
The `locale -a` command will list all the available locales on your system.
You'll see a giant list that probably includes these and many more values.
```bash
$ locale -a
en_NZ
nl_NL.UTF-8
pt_BR.UTF-8
fr_CH.ISO8859-15
eu_ES.ISO8859-15
en_US.US-ASCII
af_ZA
bg_BG
cs_CZ.UTF-8
fi_FI
zh_CN.UTF-8
eu_ES
sk_SK.ISO8859-2
nl_BE
fr_BE
sk_SK
en_US.UTF-8
...
```
Each of these locales identifies itself by the language and the manner in which
the language is used in a particular place. For example, `en_NZ` is _English as
it is spoken in New Zealand_. `fr_BE` is _French as it is spoken in Belgium_.
`en_US.UTF-8` is _English as it is spoken in the US, with a UTF-8 character set
encoding_.
[source](https://www.postgresql.org/docs/current/locale.html#LOCALE-OVERVIEW)