yang-base 0.6.0

个人使用构建后端服务器
Documentation
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! 存量 PostgreSQL schema 的接入报告。
//!
//! 只读扫描表、列、主键与索引,为迁移人员输出 Markdown 报告与字段声明建议。
//! 不执行任何 DDL,也不写 `_yang_schema_binding`。

use crate::error::BaseError;
use sqlx::postgres::PgPool;
use sqlx::FromRow;
use std::collections::BTreeMap;

/// 单列报告。
#[derive(Debug, Clone)]
pub struct PgAdoptionColumn {
    /// 列名。
    pub name: String,
    /// `format_type` 规范化类型。
    pub formatted_type: String,
    /// `pg_type.typname`。
    pub type_name: String,
    /// `pg_type.typtype`(`b` 基础类型、`e` 枚举、`d` domain…)。
    pub type_kind: String,
    /// 是否允许 NULL。
    pub nullable: bool,
    /// 是否主键列。
    pub primary_key: bool,
    /// identity / serial 列。
    pub auto_increment: bool,
    /// 原生枚举标签。
    pub enum_labels: Vec<String>,
}

/// 单索引报告。
#[derive(Debug, Clone)]
pub struct PgAdoptionIndex {
    /// 索引名。
    pub name: String,
    /// 是否唯一。
    pub unique: bool,
    /// 列键。
    pub columns: Vec<String>,
    /// 表达式键。
    pub expressions: Vec<String>,
    /// 部分索引谓词。
    pub predicate: Option<String>,
}

/// 单表报告。
#[derive(Debug, Clone)]
pub struct PgAdoptionTable {
    /// 表名。
    pub name: String,
    /// 列。
    pub columns: Vec<PgAdoptionColumn>,
    /// 主键列(按声明顺序)。
    pub primary_key: Vec<String>,
    /// 索引。
    pub indexes: Vec<PgAdoptionIndex>,
}

/// 整个 schema 的接入报告。
#[derive(Debug, Clone)]
pub struct PgAdoptionReport {
    /// 扫描的 schema。
    pub schema: String,
    /// 表,按名称排序。
    pub tables: Vec<PgAdoptionTable>,
}

impl PgAdoptionReport {
    /// 渲染为 Markdown。
    pub fn to_markdown(&self) -> String {
        let mut output = String::new();
        output.push_str(&format!(
            "# PostgreSQL schema 接入报告: `{}`\n\n",
            self.schema
        ));
        output.push_str(&format!("- 表数量: {}\n", self.tables.len()));
        output.push_str(&format!(
            "- 列数量: {}\n\n",
            self.tables
                .iter()
                .map(|table| table.columns.len())
                .sum::<usize>()
        ));
        for table in &self.tables {
            output.push_str(&format!("## `{}`\n\n", table.name));
            output.push_str("| 列 | 类型 | 可空 | 主键 | 建议声明 |\n|---|---|---|---|---|\n");
            for column in &table.columns {
                output.push_str(&format!(
                    "| `{}` | `{}` | {} | {} | {} |\n",
                    column.name,
                    column.formatted_type,
                    if column.nullable { "是" } else { "否" },
                    if column.primary_key { "是" } else { "" },
                    column.suggested_field().unwrap_or_else(|| {
                        format!(
                            "`opaque_column(\"{}\", \"{}\")`",
                            column.name, column.formatted_type
                        )
                    })
                ));
            }
            output.push('\n');
            if !table.primary_key.is_empty() {
                output.push_str(&format!(
                    "- 主键: {}\n",
                    table
                        .primary_key
                        .iter()
                        .map(|column| format!("`{column}`"))
                        .collect::<Vec<_>>()
                        .join(", ")
                ));
            }
            for index in &table.indexes {
                output.push_str(&format!("- 索引 `{}`: {}\n", index.name, index.describe()));
            }
            output.push_str("\n> 默认值、CHECK、外键与列注释需要人工核对后补到定义中;opaque 列不会进入通用 CRUD/UI。\n\n");
        }
        output
    }
}

impl PgAdoptionColumn {
    /// 生成 schema-first DSL 的字段声明建议;无法安全映射时返回 `None`。
    pub fn suggested_field(&self) -> Option<String> {
        let required = if self.nullable { "" } else { ".required()" };
        let primary = if self.primary_key {
            ".primary_key()"
        } else {
            ""
        };
        let auto = if self.auto_increment {
            ".auto_increment()"
        } else {
            ""
        };
        let base = match self.type_name.as_str() {
            "int2" | "int4" => format!("Field::integer(\"{}\")", self.name),
            "int8" => format!("Field::bigint(\"{}\")", self.name),
            "float4" => format!("Field::float(\"{}\")", self.name),
            "float8" => format!("Field::double(\"{}\")", self.name),
            "bool" => format!("Field::boolean(\"{}\")", self.name),
            "date" => format!("Field::date(\"{}\")", self.name),
            "timestamptz" => format!("Field::datetime(\"{}\")", self.name),
            // 无时区 timestamp 与框架 DateTime(TIMESTAMPTZ)语义不同,不自动建议。
            "timestamp" => return None,
            "json" | "jsonb" => format!("Field::json(\"{}\")", self.name),
            "text" => format!("Field::text(\"{}\")", self.name),
            "uuid" => format!("Field::uuid(\"{}\")", self.name),
            "bytea" => format!("Field::bytes(\"{}\")", self.name),
            "varchar" | "bpchar" => {
                let length = type_length(&self.formatted_type)?;
                format!("Field::string(\"{}\", {length})", self.name)
            }
            "numeric" => {
                let (precision, scale) = numeric_precision_scale(&self.formatted_type)?;
                format!("Field::decimal(\"{}\", {precision}, {scale})", self.name)
            }
            "_int2" | "_int4" => format!("Field::array(\"{}\", FieldType::Integer)", self.name),
            "_int8" => format!("Field::array(\"{}\", FieldType::BigInt)", self.name),
            "_float4" => format!("Field::array(\"{}\", FieldType::Float)", self.name),
            "_float8" => format!("Field::array(\"{}\", FieldType::Double)", self.name),
            "_bool" => format!("Field::array(\"{}\", FieldType::Boolean)", self.name),
            "_text" => format!("Field::array(\"{}\", FieldType::Text)", self.name),
            "_uuid" => format!("Field::array(\"{}\", FieldType::Uuid)", self.name),
            "_bytea" => format!("Field::array(\"{}\", FieldType::Bytes)", self.name),
            _ if self.type_kind == "e" => {
                let values = self
                    .enum_labels
                    .iter()
                    .map(|value| format!("\"{value}\""))
                    .collect::<Vec<_>>()
                    .join(", ");
                format!(
                    "Field::enumeration(\"{}\", [{}]).native_enum(\"{}\")",
                    self.name, values, self.type_name
                )
            }
            _ => return None,
        };
        Some(format!("{base}{required}{primary}{auto}"))
    }
}

impl PgAdoptionIndex {
    fn describe(&self) -> String {
        let mut kind = if self.unique { "唯一" } else { "普通" };
        if self.predicate.is_some() {
            kind = if self.unique {
                "部分唯一"
            } else {
                "部分"
            };
        }
        if !self.expressions.is_empty() {
            kind = if self.unique {
                "表达式唯一"
            } else {
                "表达式"
            };
        }
        let keys = self
            .columns
            .iter()
            .map(|column| format!("`{column}`"))
            .chain(
                self.expressions
                    .iter()
                    .map(|expression| format!("`{expression}`")),
            )
            .collect::<Vec<_>>()
            .join(", ");
        let predicate = self
            .predicate
            .as_ref()
            .map(|predicate| format!(" WHERE {predicate}"))
            .unwrap_or_default();
        format!("{kind} ({keys}){predicate}")
    }
}

/// 只读扫描 PostgreSQL schema 并生成接入报告。
pub async fn describe_postgres_schema(
    database: &yang_db::postgres::Database,
    schema: Option<&str>,
) -> Result<PgAdoptionReport, BaseError> {
    fetch_report(database.pool(), schema).await
}

async fn fetch_report(pool: &PgPool, schema: Option<&str>) -> Result<PgAdoptionReport, BaseError> {
    #[derive(FromRow)]
    struct TableRow {
        table_name: String,
    }
    #[derive(FromRow)]
    struct ColumnRow {
        table_name: String,
        column_name: String,
        type_name: String,
        type_kind: String,
        formatted_type: String,
        nullable: bool,
        auto_increment: bool,
    }
    #[derive(FromRow)]
    struct EnumRow {
        type_name: String,
        label: String,
    }
    #[derive(FromRow)]
    struct PrimaryRow {
        table_name: String,
        column_name: String,
    }
    #[derive(FromRow)]
    struct IndexRow {
        table_name: String,
        index_name: String,
        is_unique: bool,
        predicate: Option<String>,
        column_name: Option<String>,
        expression: Option<String>,
    }

    let schema: String = sqlx::query_scalar("SELECT COALESCE($1::text, current_schema())")
        .bind(schema)
        .fetch_one(pool)
        .await
        .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;

    let table_rows: Vec<TableRow> = sqlx::query_as(
        "SELECT c.relname::text AS table_name \
         FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace \
         WHERE n.nspname = $1 AND c.relkind IN ('r', 'p') AND NOT c.relispartition \
         ORDER BY c.relname",
    )
    .bind(&schema)
    .fetch_all(pool)
    .await
    .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;

    let column_rows: Vec<ColumnRow> = sqlx::query_as(
        "SELECT c.relname::text AS table_name, a.attname::text AS column_name, \
                t.typname::text AS type_name, t.typtype::text AS type_kind, \
                format_type(a.atttypid, a.atttypmod) AS formatted_type, \
                NOT a.attnotnull AS nullable, \
                (a.attidentity <> '' OR COALESCE(pg_get_expr(d.adbin, d.adrelid) LIKE 'nextval(%', false)) \
                  AS auto_increment \
         FROM pg_attribute a \
         JOIN pg_class c ON c.oid = a.attrelid \
         JOIN pg_namespace n ON n.oid = c.relnamespace \
         JOIN pg_type t ON t.oid = a.atttypid \
         LEFT JOIN pg_attrdef d ON d.adrelid = a.attrelid AND d.adnum = a.attnum \
         WHERE n.nspname = $1 AND c.relkind IN ('r', 'p') \
           AND a.attnum > 0 AND NOT a.attisdropped \
         ORDER BY c.relname, a.attnum",
    )
    .bind(&schema)
    .fetch_all(pool)
    .await
    .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;

    let enum_rows: Vec<EnumRow> = sqlx::query_as(
        "SELECT t.typname::text AS type_name, e.enumlabel::text AS label \
         FROM pg_type t \
         JOIN pg_namespace n ON n.oid = t.typnamespace \
         JOIN pg_enum e ON e.enumtypid = t.oid \
         WHERE n.nspname = $1 ORDER BY t.typname, e.enumsortorder",
    )
    .bind(&schema)
    .fetch_all(pool)
    .await
    .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;
    let mut enum_labels = BTreeMap::<String, Vec<String>>::new();
    for row in enum_rows {
        enum_labels
            .entry(row.type_name)
            .or_default()
            .push(row.label);
    }

    let primary_rows: Vec<PrimaryRow> = sqlx::query_as(
        "SELECT c.relname::text AS table_name, a.attname::text AS column_name \
         FROM pg_constraint con \
         JOIN pg_class c ON c.oid = con.conrelid \
         JOIN pg_namespace n ON n.oid = c.relnamespace \
         JOIN unnest(con.conkey) WITH ORDINALITY AS key(attnum, ord) ON true \
         JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = key.attnum \
         WHERE con.contype = 'p' AND n.nspname = $1 \
         ORDER BY c.relname, key.ord",
    )
    .bind(&schema)
    .fetch_all(pool)
    .await
    .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;
    let mut primary_keys = BTreeMap::<String, Vec<String>>::new();
    for row in primary_rows {
        primary_keys
            .entry(row.table_name)
            .or_default()
            .push(row.column_name);
    }

    let index_rows: Vec<IndexRow> = sqlx::query_as(
        "SELECT c.relname::text AS table_name, i.relname::text AS index_name, \
                ix.indisunique AS is_unique, \
                pg_get_expr(ix.indpred, ix.indrelid) AS predicate, \
                a.attname::text AS column_name, \
                CASE WHEN key.attnum = 0 \
                     THEN pg_get_indexdef(ix.indexrelid, key.ord::integer, true) \
                     ELSE NULL END AS expression \
         FROM pg_index ix \
         JOIN pg_class c ON c.oid = ix.indrelid \
         JOIN pg_namespace n ON n.oid = c.relnamespace \
         JOIN pg_class i ON i.oid = ix.indexrelid \
         JOIN unnest(ix.indkey::smallint[]) WITH ORDINALITY AS key(attnum, ord) ON true \
         LEFT JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = key.attnum \
         WHERE n.nspname = $1 AND NOT ix.indisprimary \
           AND NOT EXISTS ( \
               SELECT 1 FROM pg_constraint con \
               WHERE con.conindid = ix.indexrelid AND con.contype IN ('u', 'x', 'p') \
           ) \
           AND key.ord <= ix.indnkeyatts::bigint \
         ORDER BY c.relname, i.relname, key.ord",
    )
    .bind(&schema)
    .fetch_all(pool)
    .await
    .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;
    let mut index_map =
        BTreeMap::<(String, String), (bool, Option<String>, Vec<String>, Vec<String>)>::new();
    for row in index_rows {
        let entry = index_map
            .entry((row.table_name, row.index_name))
            .or_insert_with(|| (row.is_unique, None, Vec::new(), Vec::new()));
        entry.0 = row.is_unique;
        if entry.1.is_none() {
            entry.1 = row.predicate;
        }
        if let Some(column) = row.column_name {
            entry.2.push(column);
        }
        if let Some(expression) = row.expression {
            entry.3.push(expression);
        }
    }

    let mut tables = table_rows
        .into_iter()
        // 框架自己的登记表不属于业务接入范围。
        .filter(|row| !row.table_name.starts_with("_yang_"))
        .map(|row| {
            let columns = column_rows
                .iter()
                .filter(|column| column.table_name == row.table_name)
                .map(|column| PgAdoptionColumn {
                    name: column.column_name.clone(),
                    formatted_type: column.formatted_type.clone(),
                    type_name: column.type_name.clone(),
                    type_kind: column.type_kind.clone(),
                    nullable: column.nullable,
                    primary_key: primary_keys
                        .get(&row.table_name)
                        .is_some_and(|keys| keys.contains(&column.column_name)),
                    auto_increment: column.auto_increment,
                    enum_labels: enum_labels
                        .get(&column.type_name)
                        .cloned()
                        .unwrap_or_default(),
                })
                .collect::<Vec<_>>();
            let indexes = index_map
                .iter()
                .filter(|((table_name, _), _)| table_name == &row.table_name)
                .map(
                    |((_, name), (unique, predicate, columns, expressions))| PgAdoptionIndex {
                        name: name.clone(),
                        unique: *unique,
                        columns: columns.clone(),
                        expressions: expressions.clone(),
                        predicate: predicate.clone(),
                    },
                )
                .collect();
            PgAdoptionTable {
                name: row.table_name.clone(),
                columns,
                primary_key: primary_keys
                    .get(&row.table_name)
                    .cloned()
                    .unwrap_or_default(),
                indexes,
            }
        })
        .collect::<Vec<_>>();
    tables.sort_by(|left, right| left.name.cmp(&right.name));
    Ok(PgAdoptionReport { schema, tables })
}

fn type_length(formatted_type: &str) -> Option<usize> {
    formatted_type
        .rsplit_once('(')?
        .1
        .trim_end_matches(')')
        .parse()
        .ok()
}

fn numeric_precision_scale(formatted_type: &str) -> Option<(u8, u8)> {
    let inner = formatted_type.rsplit_once('(')?.1.trim_end_matches(')');
    let (precision, scale) = inner.split_once(',')?;
    Some((precision.trim().parse().ok()?, scale.trim().parse().ok()?))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn column(type_name: &str, formatted_type: &str, type_kind: &str) -> PgAdoptionColumn {
        PgAdoptionColumn {
            name: "sample".to_string(),
            formatted_type: formatted_type.to_string(),
            type_name: type_name.to_string(),
            type_kind: type_kind.to_string(),
            nullable: false,
            primary_key: false,
            auto_increment: false,
            enum_labels: Vec::new(),
        }
    }

    #[test]
    fn common_types_map_to_schema_dsl() {
        assert_eq!(
            column("varchar", "character varying(32)", "b").suggested_field(),
            Some("Field::string(\"sample\", 32).required()".to_string())
        );
        assert_eq!(
            column("numeric", "numeric(12,3)", "b").suggested_field(),
            Some("Field::decimal(\"sample\", 12, 3).required()".to_string())
        );
        assert_eq!(
            column("_int8", "bigint[]", "b").suggested_field(),
            Some("Field::array(\"sample\", FieldType::BigInt).required()".to_string())
        );
    }

    #[test]
    fn enum_and_unsupported_types_are_distinguished() {
        let mut status = column("order_status", "order_status", "e");
        status.enum_labels = vec!["pending".to_string(), "paid".to_string()];
        assert_eq!(
            status.suggested_field(),
            Some(
                "Field::enumeration(\"sample\", [\"pending\", \"paid\"]).native_enum(\"order_status\").required()"
                    .to_string()
            )
        );
        assert_eq!(column("inet", "inet", "b").suggested_field(), None);
        // 无时区 timestamp 与框架 DateTime 语义不一致,必须留给人工决策。
        assert_eq!(
            column("timestamp", "timestamp without time zone", "b").suggested_field(),
            None
        );
    }
}