Skip to main content

max / makenotwork

mt-db: compile-time checked SQL via sqlx macros (ultra-fuzz M3, A+) Convert all 116 static queries in queries.rs and mutations.rs from runtime sqlx::query*::<_,T>("...") to the query!/query_as!/query_scalar! macros, which verify SQL against the schema at build time. A migration that drops or renames a column now fails cargo check instead of erroring at runtime — closing the M3 type-safety gap (Storage axis). Five queries stay runtime-checked, each with a comment: two dynamic-ORDER-BY list queries and two format!-built multi-row INSERTs (macros require a literal), and create_community_ban (binds a chrono timestamp; with the session store forcing sqlx's `time` feature into the unified build, a bind parameter's type cannot be overridden the way an output column's can). TIMESTAMPTZ output columns carry explicit `chrono::DateTime<chrono::Utc>` overrides because tower-sessions-sqlx-store pulls in sqlx's `time` feature, so the full binary build has both `time` and `chrono` on and the macro otherwise infers OffsetDateTime. Commit the .sqlx/ offline cache and default SQLX_OFFLINE=true so every build machine checks against the cache, never a live DB. Regenerate with `cargo sqlx prepare --workspace` after changing any macro query. Gate: cargo check (full binary, both features) + offline build + clippy --workspace --all-targets clean; 254 integration + 141 unit tests green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Author: Max Johnson <me@maxj.phd> · 2026-06-22 21:06 UTC
Commit: 54589bd0e746529bd8ad77c51c1e208351aa6abf
Parent: 4d7b285
114 files changed, +3807 insertions, -510 deletions
M .gitignore +4
@@ -57,3 +57,7 @@ sando/daemon/sando.db-*
57 57 sando/daemon/work/
58 58 sando/daemon/releases/
59 59 sando/daemon/cargo-target/
60 +
61 + # mt-db uses sqlx compile-time macros; its offline query cache must be committed
62 + # so build hosts (and the Sando gate) compile without a live DB.
63 + !multithreaded/.sqlx/
@@ -0,0 +1,7 @@
1 + # sqlx compile-time query checking uses the committed `.sqlx/` cache, not a live
2 + # database. After changing or adding any `query!`/`query_as!`/`query_scalar!` in
3 + # mt-db, regenerate it with `DATABASE_URL=postgres:///multithreaded cargo sqlx prepare --workspace`
4 + # and commit the result. Override locally with `SQLX_OFFLINE=false` to verify
5 + # against a live DB instead.
6 + [env]
7 + SQLX_OFFLINE = "true"
@@ -0,0 +1,15 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "UPDATE users SET suspended_at = now(), suspension_reason = $2 WHERE mnw_account_id = $1",
4 + "describe": {
5 + "columns": [],
6 + "parameters": {
7 + "Left": [
8 + "Uuid",
9 + "Text"
10 + ]
11 + },
12 + "nullable": []
13 + },
14 + "hash": "03ade415463b367871d02a5b82d14b7f544ebfbeb40080d3194bcb72fa9a47ae"
15 + }
@@ -0,0 +1,16 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "UPDATE post_flags SET resolved_at = now(), resolved_by = $2, resolution = $3\n WHERE id = $1 AND resolved_at IS NULL",
4 + "describe": {
5 + "columns": [],
6 + "parameters": {
7 + "Left": [
8 + "Uuid",
9 + "Uuid",
10 + "Text"
11 + ]
12 + },
13 + "nullable": []
14 + },
15 + "hash": "0768d2c0c86038fc01f2b662ab65c9fe45979876679ce94ae6431ff1600ed4ba"
16 + }
@@ -0,0 +1,58 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "SELECT f.id, f.post_id, f.author_id,\n COALESCE(u.display_name, u.username) AS \"author_name!\",\n u.username AS author_username,\n f.body_html,\n f.created_at AS \"created_at: chrono::DateTime<chrono::Utc>\"\n FROM post_footnotes f\n JOIN users u ON u.mnw_account_id = f.author_id\n WHERE f.post_id = ANY($1)\n ORDER BY f.created_at",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "id",
9 + "type_info": "Uuid"
10 + },
11 + {
12 + "ordinal": 1,
13 + "name": "post_id",
14 + "type_info": "Uuid"
15 + },
16 + {
17 + "ordinal": 2,
18 + "name": "author_id",
19 + "type_info": "Uuid"
20 + },
21 + {
22 + "ordinal": 3,
23 + "name": "author_name!",
24 + "type_info": "Text"
25 + },
26 + {
27 + "ordinal": 4,
28 + "name": "author_username",
29 + "type_info": "Text"
30 + },
31 + {
32 + "ordinal": 5,
33 + "name": "body_html",
34 + "type_info": "Text"
35 + },
36 + {
37 + "ordinal": 6,
38 + "name": "created_at: chrono::DateTime<chrono::Utc>",
39 + "type_info": "Timestamptz"
40 + }
41 + ],
42 + "parameters": {
43 + "Left": [
44 + "UuidArray"
45 + ]
46 + },
47 + "nullable": [
48 + false,
49 + false,
50 + false,
51 + null,
52 + false,
53 + false,
54 + false
55 + ]
56 + },
57 + "hash": "0830906e69639e409caf641739ac5ccc16662f9800fdccea7c1c0eeef5193f9e"
58 + }
@@ -0,0 +1,29 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "SELECT c.name, c.slug\n FROM categories c\n JOIN communities co ON co.id = c.community_id\n WHERE co.slug = $1 AND c.slug = $2",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "name",
9 + "type_info": "Text"
10 + },
11 + {
12 + "ordinal": 1,
13 + "name": "slug",
14 + "type_info": "Text"
15 + }
16 + ],
17 + "parameters": {
18 + "Left": [
19 + "Text",
20 + "Text"
21 + ]
22 + },
23 + "nullable": [
24 + false,
25 + false
26 + ]
27 + },
28 + "hash": "0a8b8dc6d47eb404cf7fb308e9cfa9083797b7cc54a716b151532b3c6f0034dc"
29 + }
@@ -0,0 +1,71 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "SELECT cb.id, cb.user_id,\n u.username, u.display_name,\n cb.ban_type AS \"ban_type: BanType\", cb.reason,\n cb.expires_at AS \"expires_at: chrono::DateTime<chrono::Utc>\",\n cb.created_at AS \"created_at: chrono::DateTime<chrono::Utc>\",\n actor.username AS banned_by_username\n FROM community_bans cb\n JOIN users u ON u.mnw_account_id = cb.user_id\n JOIN users actor ON actor.mnw_account_id = cb.banned_by\n WHERE cb.community_id = $1\n AND (cb.expires_at IS NULL OR cb.expires_at > now())\n ORDER BY cb.created_at DESC\n LIMIT $2",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "id",
9 + "type_info": "Uuid"
10 + },
11 + {
12 + "ordinal": 1,
13 + "name": "user_id",
14 + "type_info": "Uuid"
15 + },
16 + {
17 + "ordinal": 2,
18 + "name": "username",
19 + "type_info": "Text"
20 + },
21 + {
22 + "ordinal": 3,
23 + "name": "display_name",
24 + "type_info": "Text"
25 + },
26 + {
27 + "ordinal": 4,
28 + "name": "ban_type: BanType",
29 + "type_info": "Text"
30 + },
31 + {
32 + "ordinal": 5,
33 + "name": "reason",
34 + "type_info": "Text"
35 + },
36 + {
37 + "ordinal": 6,
38 + "name": "expires_at: chrono::DateTime<chrono::Utc>",
39 + "type_info": "Timestamptz"
40 + },
41 + {
42 + "ordinal": 7,
43 + "name": "created_at: chrono::DateTime<chrono::Utc>",
44 + "type_info": "Timestamptz"
45 + },
46 + {
47 + "ordinal": 8,
48 + "name": "banned_by_username",
49 + "type_info": "Text"
50 + }
51 + ],
52 + "parameters": {
53 + "Left": [
54 + "Uuid",
55 + "Int8"
56 + ]
57 + },
58 + "nullable": [
59 + false,
60 + false,
61 + false,
62 + true,
63 + false,
64 + true,
65 + true,
66 + false,
67 + false
68 + ]
69 + },
70 + "hash": "0ba3bb2135932053e74fee53d32e33138819e31a184841df4b365f8e4c32b541"
71 + }
@@ -0,0 +1,15 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "UPDATE communities SET state = $2 WHERE id = $1",
4 + "describe": {
5 + "columns": [],
6 + "parameters": {
7 + "Left": [
8 + "Uuid",
9 + "Text"
10 + ]
11 + },
12 + "nullable": []
13 + },
14 + "hash": "0c8bd7af0ecbc3729357019179bdd62a54de1c15b51ccb7d10febf32886a3a5a"
15 + }
@@ -0,0 +1,22 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "SELECT id FROM posts WHERE external_ref = $1",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "id",
9 + "type_info": "Uuid"
10 + }
11 + ],
12 + "parameters": {
13 + "Left": [
14 + "Text"
15 + ]
16 + },
17 + "nullable": [
18 + false
19 + ]
20 + },
21 + "hash": "0d0bd14a16970a0c34e9e719b087616f5730863904aeed9caab237b8efb15e1f"
22 + }
@@ -0,0 +1,34 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "SELECT id, name, slug FROM tags WHERE community_id = $1 ORDER BY name",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "id",
9 + "type_info": "Uuid"
10 + },
11 + {
12 + "ordinal": 1,
13 + "name": "name",
14 + "type_info": "Text"
15 + },
16 + {
17 + "ordinal": 2,
18 + "name": "slug",
19 + "type_info": "Text"
20 + }
21 + ],
22 + "parameters": {
23 + "Left": [
24 + "Uuid"
25 + ]
26 + },
27 + "nullable": [
28 + false,
29 + false,
30 + false
31 + ]
32 + },
33 + "hash": "0feaed5a9d2365a4a0d18d12d3e52fadf77592fd7c31e1959787dffc87b830f4"
34 + }
@@ -0,0 +1,25 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "INSERT INTO post_footnotes (post_id, author_id, body_markdown, body_html)\n VALUES ($1, $2, $3, $4)\n RETURNING id",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "id",
9 + "type_info": "Uuid"
10 + }
11 + ],
12 + "parameters": {
13 + "Left": [
14 + "Uuid",
15 + "Uuid",
16 + "Text",
17 + "Text"
18 + ]
19 + },
20 + "nullable": [
21 + false
22 + ]
23 + },
24 + "hash": "11529b2b1e38fc99f7732f2080c172fd2691ce72ff8f231d462bf9d7a0d085d6"
25 + }
@@ -0,0 +1,54 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "SELECT t.id AS thread_id,\n t.title AS thread_title,\n c.name AS category_name,\n c.slug AS category_slug,\n p.created_at AS \"post_created_at: chrono::DateTime<chrono::Utc>\",\n (t.author_id = $2) AS \"is_thread_author!\"\n FROM posts p\n JOIN threads t ON t.id = p.thread_id\n JOIN categories c ON c.id = t.category_id\n WHERE c.community_id = $1\n AND p.author_id = $2\n AND p.removed_at IS NULL\n AND p.deleted_at IS NULL\n AND t.deleted_at IS NULL\n ORDER BY p.created_at DESC\n LIMIT $3",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "thread_id",
9 + "type_info": "Uuid"
10 + },
11 + {
12 + "ordinal": 1,
13 + "name": "thread_title",
14 + "type_info": "Text"
15 + },
16 + {
17 + "ordinal": 2,
18 + "name": "category_name",
19 + "type_info": "Text"
20 + },
21 + {
22 + "ordinal": 3,
23 + "name": "category_slug",
24 + "type_info": "Text"
25 + },
26 + {
27 + "ordinal": 4,
28 + "name": "post_created_at: chrono::DateTime<chrono::Utc>",
29 + "type_info": "Timestamptz"
30 + },
31 + {
32 + "ordinal": 5,
33 + "name": "is_thread_author!",
34 + "type_info": "Bool"
35 + }
36 + ],
37 + "parameters": {
38 + "Left": [
39 + "Uuid",
40 + "Uuid",
41 + "Int8"
42 + ]
43 + },
44 + "nullable": [
45 + false,
46 + false,
47 + false,
48 + false,
49 + false,
50 + null
51 + ]
52 + },
53 + "hash": "150877ff6d3dd6d1c8034f0f5f9af152b5759f9c0b7e0c3549b636be95f65f89"
54 + }
@@ -0,0 +1,42 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "SELECT u.username,\n u.display_name,\n m.role AS \"role: CommunityRole\",\n m.joined_at AS \"joined_at: chrono::DateTime<chrono::Utc>\"\n FROM memberships m\n JOIN users u ON u.mnw_account_id = m.user_id\n WHERE m.community_id = $1\n ORDER BY\n CASE m.role\n WHEN 'owner' THEN 0\n WHEN 'moderator' THEN 1\n WHEN 'member' THEN 2\n ELSE 3\n END,\n m.joined_at\n LIMIT $2 OFFSET $3",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "username",
9 + "type_info": "Text"
10 + },
11 + {
12 + "ordinal": 1,
13 + "name": "display_name",
14 + "type_info": "Text"
15 + },
16 + {
17 + "ordinal": 2,
18 + "name": "role: CommunityRole",
19 + "type_info": "Text"
20 + },
21 + {
22 + "ordinal": 3,
23 + "name": "joined_at: chrono::DateTime<chrono::Utc>",
24 + "type_info": "Timestamptz"
25 + }
26 + ],
27 + "parameters": {
28 + "Left": [
29 + "Uuid",
30 + "Int8",
31 + "Int8"
32 + ]
33 + },
34 + "nullable": [
35 + false,
36 + true,
37 + false,
38 + false
39 + ]
40 + },
41 + "hash": "15333e4a76a069b3279777bae4abbe6c1b57158c3c5a2e29350972d879bb177d"
42 + }
@@ -0,0 +1,14 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "DELETE FROM thread_tags WHERE thread_id = $1",
4 + "describe": {
5 + "columns": [],
6 + "parameters": {
7 + "Left": [
8 + "Uuid"
9 + ]
10 + },
11 + "nullable": []
12 + },
13 + "hash": "168b23858c656f6b8b7bfa432f557477b9e67b8ae0b7e6939de5d206f85239aa"
14 + }
@@ -0,0 +1,16 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "INSERT INTO memberships (user_id, community_id, role)\n VALUES ($1, $2, $3)\n ON CONFLICT (user_id, community_id) DO NOTHING",
4 + "describe": {
5 + "columns": [],
6 + "parameters": {
7 + "Left": [
8 + "Uuid",
9 + "Uuid",
10 + "Text"
11 + ]
12 + },
13 + "nullable": []
14 + },
15 + "hash": "189c6a75a198bd05e3e523d4d15631750308a613f03e000d7f63b482014551eb"
16 + }
@@ -0,0 +1,14 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "UPDATE users SET suspended_at = NULL, suspension_reason = NULL WHERE mnw_account_id = $1",
4 + "describe": {
5 + "columns": [],
6 + "parameters": {
7 + "Left": [
8 + "Uuid"
9 + ]
10 + },
11 + "nullable": []
12 + },
13 + "hash": "1b7868b5755c66f69d5154d7fea134ae688fa2fc31391841eae830d6f4adba03"
14 + }
@@ -0,0 +1,15 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "DELETE FROM tracked_threads WHERE user_id = $1 AND thread_id = $2",
4 + "describe": {
5 + "columns": [],
6 + "parameters": {
7 + "Left": [
8 + "Uuid",
9 + "Uuid"
10 + ]
11 + },
12 + "nullable": []
13 + },
14 + "hash": "1ba6771c92311adf1599e90b28fcbe9f3419635bba4d4c999f10db6644bdacca"
15 + }
@@ -0,0 +1,40 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "SELECT c.name, c.slug, c.description,\n COUNT(t.id) AS \"thread_count!\"\n FROM categories c\n JOIN communities co ON co.id = c.community_id\n LEFT JOIN threads t ON t.category_id = c.id AND t.deleted_at IS NULL\n WHERE co.slug = $1\n GROUP BY c.id, c.name, c.slug, c.description, c.sort_order\n ORDER BY c.sort_order",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "name",
9 + "type_info": "Text"
10 + },
11 + {
12 + "ordinal": 1,
13 + "name": "slug",
14 + "type_info": "Text"
15 + },
16 + {
17 + "ordinal": 2,
18 + "name": "description",
19 + "type_info": "Text"
20 + },
21 + {
22 + "ordinal": 3,
23 + "name": "thread_count!",
24 + "type_info": "Int8"
25 + }
26 + ],
27 + "parameters": {
28 + "Left": [
29 + "Text"
30 + ]
31 + },
32 + "nullable": [
33 + false,
34 + false,
35 + true,
36 + null
37 + ]
38 + },
39 + "hash": "1d6a0c577f686ee413600a7bc98e31d6e2daacb24ee79e6e7cc6317bd49d3fa4"
40 + }
@@ -0,0 +1,23 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "SELECT EXISTS(SELECT 1 FROM community_bans\n WHERE community_id = $1 AND user_id = $2 AND ban_type = 'ban'\n AND (expires_at IS NULL OR expires_at > now())) AS \"exists!\"",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "exists!",
9 + "type_info": "Bool"
10 + }
11 + ],
12 + "parameters": {
13 + "Left": [
14 + "Uuid",
15 + "Uuid"
16 + ]
17 + },
18 + "nullable": [
19 + null
20 + ]
21 + },
22 + "hash": "27b906a5bbec171f7ba6c04ff8d26929bbbc5c1294e17425a7cb6350e7f65781"
23 + }
@@ -0,0 +1,58 @@
1 + {
2 + "db_name": "PostgreSQL",
3 + "query": "SELECT id, name, slug, description,\n suspended_at AS \"suspended_at: chrono::DateTime<chrono::Utc>\",\n auto_hide_threshold,\n state AS \"state: CommunityState\"\n FROM communities WHERE id = $1",
4 + "describe": {
5 + "columns": [
6 + {
7 + "ordinal": 0,
8 + "name": "id",
9 + "type_info": "Uuid"
10 + },
11 + {
12 + "ordinal": 1,
13 + "name": "name",
14 + "type_info": "Text"
15 + },
16 + {
17 + "ordinal": 2,
18 + "name": "slug",
19 + "type_info": "Text"
20 + },
21 + {
22 + "ordinal": 3,
23 + "name": "description",
24 + "type_info": "Text"
25 + },
26 + {
27 + "ordinal": 4,
28 + "name": "suspended_at: chrono::DateTime<chrono::Utc>",
29 + "type_info": "Timestamptz"
30 + },
31 + {
32 + "ordinal": 5,
33 + "name": "auto_hide_threshold",
34 + "type_info": "Int4"
35 + },
36 + {
37 + "ordinal": 6,
38 + "name": "state: CommunityState",
39 + "type_info": "Text"
40 + }
41 + ],
42 + "parameters": {
43 + "Left": [
44 + "Uuid"
45 + ]
46 + },
47 + "nullable": [
48 + false,
49 + false,
50 + false,
51 + true,
52 + true,
53 + true,
54 + false
55 + ]
56 + },
57 + "hash": "2a06e1e3e30841ed6cdc6bca28aafaed2ebeffe25a7010043e92db90e9a62dab"
58 + }