mirror of
https://github.com/jbranchaud/til
synced 2026-01-16 05:28:03 +00:00
Compare commits
2 Commits
b743dc2ac0
...
43ea7acd74
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
43ea7acd74 | ||
|
|
d7d331b688 |
@@ -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).
|
||||
|
||||
_1477 TILs and counting..._
|
||||
_1479 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -804,6 +804,7 @@ _1477 TILs and counting..._
|
||||
- [Pretty Printing JSONB Rows](postgres/pretty-printing-jsonb-rows.md)
|
||||
- [Prevent A Query From Running Too Long](postgres/prevent-a-query-from-running-too-long.md)
|
||||
- [Print The Query Buffer In psql](postgres/print-the-query-buffer-in-psql.md)
|
||||
- [Put Unique Constraint On Generated Column](postgres/put-unique-constraint-on-generated-column.md)
|
||||
- [Remove Not Null Constraint From A Column](postgres/remove-not-null-constraint-from-a-column.md)
|
||||
- [Renaming A Sequence](postgres/renaming-a-sequence.md)
|
||||
- [Renaming A Table](postgres/renaming-a-table.md)
|
||||
@@ -1757,6 +1758,7 @@ _1477 TILs and counting..._
|
||||
|
||||
- [Add To The Path Via Path Array](zsh/add-to-the-path-via-path-array.md)
|
||||
- [Link A Scalar To An Array](zsh/link-a-scalar-to-an-array.md)
|
||||
- [Use A Space To Exclude Command From History](zsh/use-a-space-to-exclude-command-from-history.md)
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
46
postgres/put-unique-constraint-on-generated-column.md
Normal file
46
postgres/put-unique-constraint-on-generated-column.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# Put Unique Constraint On Generated Column
|
||||
|
||||
You cannot apply a _unique constraint_ to an expression over a column, e.g.
|
||||
`lower(email)`. You can, however, create a [generated
|
||||
column](https://www.postgresql.org/docs/current/ddl-generated-columns.html) for
|
||||
that expression and then apply the unique constraint to that generated column.
|
||||
|
||||
Here is what that could look like:
|
||||
|
||||
```sql
|
||||
> create table users (
|
||||
id integer generated always as identity primary key,
|
||||
name text not null,
|
||||
email text not null,
|
||||
email_lower text generated always as (lower(email)) stored,
|
||||
unique ( email_lower )
|
||||
);
|
||||
|
||||
> \d users
|
||||
+-------------+---------+-----------------------------------------------------------------+
|
||||
| Column | Type | Modifiers |
|
||||
|-------------+---------+-----------------------------------------------------------------|
|
||||
| id | integer | not null generated always as identity |
|
||||
| name | text | not null |
|
||||
| email | text | not null |
|
||||
| email_lower | text | default lower(email) generated always as (lower(email)) stored |
|
||||
+-------------+---------+-----------------------------------------------------------------+
|
||||
Indexes:
|
||||
"users_pkey" PRIMARY KEY, btree (id)
|
||||
"users_email_lower_key" UNIQUE CONSTRAINT, btree (email_lower)
|
||||
```
|
||||
|
||||
And then an demonstration of violating that constraint:
|
||||
|
||||
```sql
|
||||
|
||||
> insert into users (name, email) values ('Bob', 'bob@email.com');
|
||||
INSERT 0 1
|
||||
|
||||
> insert into users (name, email) values ('Bobby', 'BOB@email.com');
|
||||
duplicate key value violates unique constraint "users_email_lower_key"
|
||||
DETAIL: Key (email_lower)=(bob@email.com) already exists.
|
||||
```
|
||||
|
||||
The main tradeoff here is that you are doubling the amount of storage you need
|
||||
for that column. Unless it is a massive table, that is likely not an issue.
|
||||
37
zsh/use-a-space-to-exclude-command-from-history.md
Normal file
37
zsh/use-a-space-to-exclude-command-from-history.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Use A Space To Exclude Command From History
|
||||
|
||||
When using a shell like `zsh`, you get the benefit of it keeping track of the
|
||||
history of the commands you've entered into the shell. This means you can
|
||||
quickly traverse pack to a previous command that you want to run again. It also
|
||||
means [a tool like `fzf` can hook into your history
|
||||
file](https://github.com/junegunn/fzf?tab=readme-ov-file#key-bindings-for-command-line)
|
||||
so that you can fuzzy-search for a command you may have executed weeks ago.
|
||||
|
||||
The history is stored on your machine in a plaintext file. Not every command
|
||||
should be stored in a plaintext file. For instance, you don't want `zsh` to
|
||||
persist a command that includes a password.
|
||||
|
||||
With the `histignorespace` option enabled in `zsh`, we can put a leading space
|
||||
in front of our command and it will be excluded from the history file.
|
||||
|
||||
Try it yourself:
|
||||
|
||||
```bash
|
||||
$ echo 'this command will be remembered'
|
||||
this command will be remembered
|
||||
|
||||
$ echo 'this command will be forgotten'
|
||||
this command will be forgotten
|
||||
```
|
||||
|
||||
Notice the leading space in the second command. Trying pressing your _up_ arrow
|
||||
and notice only that first `echo` is remembered.
|
||||
|
||||
Make sure `histignorespace` is included in the list when you run `setopt`. If
|
||||
it isn't, then add it:
|
||||
|
||||
```bash
|
||||
$ setopt histignorespace
|
||||
```
|
||||
|
||||
[source](https://stackoverflow.com/questions/8473121/execute-a-command-without-keeping-it-in-history/49643320#49643320)
|
||||
Reference in New Issue
Block a user