Skip to main content

appstruct_migrate/
lint.rs

1use crate::{ExecutionRisk, MigrationPlan, SchemaChange, SchemaRisk};
2use serde::Serialize;
3
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
5#[serde(rename_all = "snake_case")]
6pub enum LintSeverity {
7    Warning,
8    Error,
9}
10
11#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
12pub struct MigrationLint {
13    pub code: &'static str,
14    pub severity: LintSeverity,
15    pub message: String,
16}
17
18#[must_use]
19pub fn lint_plan(plan: &MigrationPlan) -> Vec<MigrationLint> {
20    let mut issues = Vec::new();
21    for planned in &plan.changes {
22        let label = change_label(&planned.change);
23        if planned.risk.schema == SchemaRisk::Destructive {
24            issues.push(MigrationLint {
25                code: "AS4201",
26                severity: LintSeverity::Error,
27                message: format!("{label} is destructive; write and review a manual migration"),
28            });
29        } else if planned.risk.execution == ExecutionRisk::MayLock {
30            issues.push(MigrationLint {
31                code: "AS4202",
32                severity: LintSeverity::Warning,
33                message: format!(
34                    "{label} may lock existing rows; schedule it outside peak traffic"
35                ),
36            });
37        } else if planned.risk.execution == ExecutionRisk::ManualReview {
38            issues.push(MigrationLint {
39                code: "AS4203",
40                severity: LintSeverity::Error,
41                message: format!("{label} requires manual SQL review before applying"),
42            });
43        }
44        if let SchemaChange::AddColumn { table, column } = &planned.change
45            && !column.nullable
46            && column.default.is_none()
47            && column.generated.is_none()
48        {
49            issues.push(MigrationLint {
50                code: "AS4204",
51                severity: LintSeverity::Error,
52                message: format!(
53                    "adding non-null column `{}.{}` without a default can fail on existing rows",
54                    table, column.name
55                ),
56            });
57        }
58    }
59    issues.sort_by(|left, right| {
60        left.code
61            .cmp(right.code)
62            .then(left.message.cmp(&right.message))
63    });
64    issues
65}
66
67fn change_label(change: &SchemaChange) -> String {
68    match change {
69        SchemaChange::AddTable { table } => format!("add table `{}`", table.name),
70        SchemaChange::RemoveTable { table } => format!("remove table `{}`", table.name),
71        SchemaChange::RenameTable { before, after } => {
72            format!("rename table `{}` to `{}`", before.name, after.name)
73        }
74        SchemaChange::AddColumn { table, column } => {
75            format!("add column `{table}.{}`", column.name)
76        }
77        SchemaChange::RemoveColumn { table, column } => {
78            format!("remove column `{table}.{}`", column.name)
79        }
80        SchemaChange::AlterColumn { table, after, .. } => {
81            format!("alter column `{table}.{}`", after.name)
82        }
83        SchemaChange::AddUniqueConstraint { constraint } => {
84            format!("add unique constraint `{}`", constraint.id)
85        }
86        SchemaChange::RemoveUniqueConstraint { constraint } => {
87            format!("remove unique constraint `{}`", constraint.id)
88        }
89        SchemaChange::AddIndex { index } => format!("add index `{}`", index.id),
90        SchemaChange::RemoveIndex { index } => format!("remove index `{}`", index.id),
91        SchemaChange::AddSeed { seed } => format!("add seed `{}`", seed.id),
92        SchemaChange::RemoveSeed { seed } => format!("remove seed `{}`", seed.id),
93        SchemaChange::AddForeignKey { foreign_key } => {
94            format!("add foreign key `{}`", foreign_key.id)
95        }
96        SchemaChange::RemoveForeignKey { foreign_key } => {
97            format!("remove foreign key `{}`", foreign_key.id)
98        }
99    }
100}