Skip to content

Commit 7c5b511

Browse files
James Stone TIL sync botjamesmstone
James Stone TIL sync bot
authored andcommitted
Wed 29 Jan 2025 15:04:31 +07
1 parent ff5516b commit 7c5b511

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

IKEA/IKEAs-restaurant.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# IKEAs restaurant
2+
3+
IKEA is the 6th largest restaurant in the world by number of customers.
4+
In 2017 there were 700 million people who ate an IKEA. Roughly 10 percent of the world.
5+
6+
From [podcast](https://open.spotify.com/episode/6AdowJpGm8uBINgHAdDTKB?si=vGU_5gdDTriVUMJGN7nKTA%0A)

generate_screenshots.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import hashlib
2+
import pathlib
3+
import subprocess
4+
import sqlite_utils
5+
import tempfile
6+
7+
root = pathlib.Path(__file__).parent.resolve()
8+
TMP_PATH = pathlib.Path(tempfile.gettempdir())
9+
SHOT_HASH_PATHS = [
10+
(root / "templates" / "row.html"),
11+
(root / "templates" / "til_base.html"),
12+
]
13+
14+
15+
def png_for_path(path):
16+
page_html = str(TMP_PATH / "generate-screenshots-page.html")
17+
# Use datasette to generate HTML
18+
proc = subprocess.run(["datasette", ".", "--get", path], capture_output=True)
19+
open(page_html, "wb").write(proc.stdout)
20+
# Now use puppeteer screenshot to generate a PNG
21+
proc2 = subprocess.run(
22+
[
23+
"puppeteer",
24+
"screenshot",
25+
page_html,
26+
"--viewport",
27+
"800x400",
28+
"--full-page=false",
29+
],
30+
capture_output=True,
31+
)
32+
png_bytes = proc2.stdout
33+
return png_bytes
34+
35+
36+
def generate_screenshots(root):
37+
db = sqlite_utils.Database(root / "tils.db")
38+
39+
# The shot_hash incorporates a hash of all of row.html
40+
41+
shot_html_hash = hashlib.md5()
42+
for filepath in SHOT_HASH_PATHS:
43+
shot_html_hash.update(filepath.read_text().encode("utf-8"))
44+
shot_html_hash = shot_html_hash.hexdigest()
45+
46+
for row in db["til"].rows:
47+
path = row["path"]
48+
html = row["html"]
49+
shot_hash = hashlib.md5((shot_html_hash + html).encode("utf-8")).hexdigest()
50+
if shot_hash != row.get("shot_hash"):
51+
png = png_for_path("/{}/{}".format(row["topic"], row["slug"]))
52+
db["til"].update(path, {"shot": png, "shot_hash": shot_hash}, alter=True)
53+
print(
54+
"Got {} byte PNG for {} shot hash {}".format(len(png), path, shot_hash)
55+
)
56+
else:
57+
print("Skipped {} with shot hash {}".format(path, shot_hash))
58+
59+
60+
if __name__ == "__main__":
61+
generate_screenshots(root)

postgres/ddl-macros.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# DDL Postgres Macros
2+
3+
In postgres you may find yourself repeating common operations on all your tables. For example adding an audit log ,created times, modified user etc.
4+
5+
This can be tedious to add everywhere. Instead you can create a postgres "macro" to update your DDL for you
6+
7+
8+
# Example
9+
```postgresql
10+
CREATE OR REPLACE FUNCTION public.add_created_columns(
11+
table_type regclass
12+
) RETURNS void
13+
LANGUAGE plpgsql
14+
SET search_path TO ''
15+
AS $_$
16+
declare
17+
statement text = format($$
18+
alter table %1$s
19+
add column created_user int references public."user"(id) not null,
20+
add column created_at timestamptz not null default current_timestamp;
21+
$$,
22+
$1);
23+
begin
24+
execute statement;
25+
end;
26+
$_$;
27+
```
28+
29+
# Usage
30+
31+
To use the `add_created_columns` macro, you simply call it by passing the table name as a parameter. Here's an example
32+
of how to use it:
33+
34+
```postgresql
35+
SELECT public.add_created_columns('public.orders'),
36+
public.add_created_columns('public.products');
37+
```
38+
39+
This will modify the specified table by adding the `created_user` and `created_at` columns as defined in the macro.

0 commit comments

Comments
 (0)