1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Helpers for migrations that involve [`Blob`](super::Blob) columns.
//!
//! A `Blob` lives on a Postgres `JSONB` column on a `#[model]`-derived
//! struct. Adding one to an existing table is a one-line `ALTER TABLE`,
//! but typing that one line by hand on every blob field invites typos
//! (wrong column type, missing `NULL`, no symmetric `down.sql`). The
//! [`add_blob_column!`] macro takes the table + column and returns a
//! `(up, down)` SQL pair you can paste into a Diesel migration or run
//! through any connection.
//!
//! ## Examples
//!
//! Generate the SQL pair for a Diesel `migrations/<name>/{up,down}.sql`:
//!
//! ```
//! use autumn_web::storage::migrations::add_blob_column;
//!
//! let (up, down) = add_blob_column!("users", "avatar");
//! assert_eq!(up, "ALTER TABLE users ADD COLUMN avatar JSONB NULL");
//! assert_eq!(down, "ALTER TABLE users DROP COLUMN avatar");
//! ```
//!
//! Run it through a runtime Diesel-async connection:
//!
//! ```rust,ignore
//! use autumn_web::storage::migrations::add_blob_column;
//! use diesel_async::RunQueryDsl;
//!
//! let (up, _down) = add_blob_column!("users", "avatar");
//! diesel::sql_query(up).execute(&mut conn).await?;
//! ```
/// Build the `(up, down)` SQL pair to add or drop a [`Blob`](super::Blob)
/// column on an existing Postgres table.
///
/// `up` produces:
///
/// ```sql
/// ALTER TABLE <table> ADD COLUMN <column> JSONB NULL
/// ```
///
/// `down` produces:
///
/// ```sql
/// ALTER TABLE <table> DROP COLUMN <column>
/// ```
///
/// Both arguments must be string literals — keys in `Diesel`/Postgres
/// identifiers can't be safely interpolated from runtime values without
/// quoting, so the macro deliberately requires compile-time strings to
/// avoid an injection footgun. For runtime-named columns, build the
/// SQL by hand.
pub use add_blob_column;