|
| 1 | +use squawk_syntax::{ |
| 2 | + Parse, SourceFile, |
| 3 | + ast::{self, AstNode}, |
| 4 | +}; |
| 5 | + |
| 6 | +use crate::{Edit, Fix, Linter, Rule, Version, Violation}; |
| 7 | + |
| 8 | +fn vacuum_full_fix(vacuum: &ast::Vacuum) -> Option<Fix> { |
| 9 | + let tables = vacuum.table_and_columns_list()?; |
| 10 | + let tables = tables.syntax(); |
| 11 | + let replacement = format!("repack (concurrently) {tables}"); |
| 12 | + let edit = Edit::replace(vacuum.syntax().text_range(), replacement); |
| 13 | + Some(Fix::new("Replace with `repack (concurrently)`", vec![edit])) |
| 14 | +} |
| 15 | + |
| 16 | +fn cluster_fix(cluster: &ast::Cluster) -> Option<Fix> { |
| 17 | + let replacement = if let Some(on_path) = cluster.on_path() { |
| 18 | + let index = cluster.path()?; |
| 19 | + let table = on_path.path()?; |
| 20 | + format!( |
| 21 | + "repack (concurrently) {} using index {}", |
| 22 | + table.syntax(), |
| 23 | + index.syntax() |
| 24 | + ) |
| 25 | + } else if let Some(table) = cluster.path() { |
| 26 | + if let Some(index_name) = cluster.using_method().and_then(|method| method.name_ref()) { |
| 27 | + format!( |
| 28 | + "repack (concurrently) {} using index {}", |
| 29 | + table.syntax(), |
| 30 | + index_name.syntax() |
| 31 | + ) |
| 32 | + } else { |
| 33 | + format!("repack (concurrently) {}", table.syntax()) |
| 34 | + } |
| 35 | + } else { |
| 36 | + "repack (concurrently)".to_string() |
| 37 | + }; |
| 38 | + let edit = Edit::replace(cluster.syntax().text_range(), replacement); |
| 39 | + Some(Fix::new("Replace with `repack (concurrently)`", vec![edit])) |
| 40 | +} |
| 41 | + |
| 42 | +pub(crate) fn prefer_repack(ctx: &mut Linter, parse: &Parse<SourceFile>) { |
| 43 | + if ctx.settings.pg_version < Version::new(19, None, None) { |
| 44 | + return; |
| 45 | + } |
| 46 | + for stmt in parse.tree().stmts() { |
| 47 | + match stmt { |
| 48 | + ast::Stmt::Cluster(cluster) => { |
| 49 | + let fix = cluster_fix(&cluster); |
| 50 | + ctx.report( |
| 51 | + Violation::for_node( |
| 52 | + Rule::PreferRepack, |
| 53 | + "`cluster` requires an `ACCESS EXCLUSIVE` lock which blocks reads and writes to the table.".into(), |
| 54 | + cluster.syntax(), |
| 55 | + ) |
| 56 | + .help("Use `repack` to rewrite the table without blocking reads and writes.") |
| 57 | + .fix(fix), |
| 58 | + ); |
| 59 | + } |
| 60 | + ast::Stmt::Vacuum(vacuum) => { |
| 61 | + if vacuum.is_full() { |
| 62 | + let fix = vacuum_full_fix(&vacuum); |
| 63 | + ctx.report( |
| 64 | + Violation::for_node( |
| 65 | + Rule::PreferRepack, |
| 66 | + "`vacuum full` requires an `ACCESS EXCLUSIVE` lock which blocks reads and writes to the table.".into(), |
| 67 | + vacuum.syntax(), |
| 68 | + ) |
| 69 | + .help("Use `repack` to rewrite the table without blocking reads and writes.") |
| 70 | + .fix(fix), |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + _ => (), |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod test { |
| 81 | + use insta::assert_snapshot; |
| 82 | + |
| 83 | + use crate::{ |
| 84 | + LinterSettings, Rule, |
| 85 | + test_utils::{fix_sql_with, lint_errors_with, lint_ok, lint_ok_with}, |
| 86 | + }; |
| 87 | + |
| 88 | + fn pg19() -> LinterSettings { |
| 89 | + LinterSettings { |
| 90 | + pg_version: "19".parse().expect("Invalid PostgreSQL version"), |
| 91 | + ..Default::default() |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn cluster_err() { |
| 97 | + let sql = "CLUSTER foo;"; |
| 98 | + assert_snapshot!(lint_errors_with(sql, pg19(), Rule::PreferRepack), @" |
| 99 | + warning[prefer-repack]: `cluster` requires an `ACCESS EXCLUSIVE` lock which blocks reads and writes to the table. |
| 100 | + ╭▸ |
| 101 | + 1 │ CLUSTER foo; |
| 102 | + │ ━━━━━━━━━━━ |
| 103 | + │ |
| 104 | + ├ help: Use `repack` to rewrite the table without blocking reads and writes. |
| 105 | + ╭╴ |
| 106 | + 1 - CLUSTER foo; |
| 107 | + 1 + repack (concurrently) foo; |
| 108 | + ╰╴ |
| 109 | + "); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn cluster_no_path_err() { |
| 114 | + let sql = "CLUSTER;"; |
| 115 | + assert_snapshot!(lint_errors_with(sql, pg19(), Rule::PreferRepack), @" |
| 116 | + warning[prefer-repack]: `cluster` requires an `ACCESS EXCLUSIVE` lock which blocks reads and writes to the table. |
| 117 | + ╭▸ |
| 118 | + 1 │ CLUSTER; |
| 119 | + │ ━━━━━━━ |
| 120 | + │ |
| 121 | + ├ help: Use `repack` to rewrite the table without blocking reads and writes. |
| 122 | + ╭╴ |
| 123 | + 1 - CLUSTER; |
| 124 | + 1 + repack (concurrently); |
| 125 | + ╰╴ |
| 126 | + "); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn vacuum_full_err() { |
| 131 | + let sql = "VACUUM FULL foo;"; |
| 132 | + assert_snapshot!(lint_errors_with(sql, pg19(), Rule::PreferRepack), @" |
| 133 | + warning[prefer-repack]: `vacuum full` requires an `ACCESS EXCLUSIVE` lock which blocks reads and writes to the table. |
| 134 | + ╭▸ |
| 135 | + 1 │ VACUUM FULL foo; |
| 136 | + │ ━━━━━━━━━━━━━━━ |
| 137 | + │ |
| 138 | + ├ help: Use `repack` to rewrite the table without blocking reads and writes. |
| 139 | + ╭╴ |
| 140 | + 1 - VACUUM FULL foo; |
| 141 | + 1 + repack (concurrently) foo; |
| 142 | + ╰╴ |
| 143 | + "); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn vacuum_full_option_list_err() { |
| 148 | + let sql = "VACUUM (FULL) foo;"; |
| 149 | + assert_snapshot!(lint_errors_with(sql, pg19(), Rule::PreferRepack), @" |
| 150 | + warning[prefer-repack]: `vacuum full` requires an `ACCESS EXCLUSIVE` lock which blocks reads and writes to the table. |
| 151 | + ╭▸ |
| 152 | + 1 │ VACUUM (FULL) foo; |
| 153 | + │ ━━━━━━━━━━━━━━━━━ |
| 154 | + │ |
| 155 | + ├ help: Use `repack` to rewrite the table without blocking reads and writes. |
| 156 | + ╭╴ |
| 157 | + 1 - VACUUM (FULL) foo; |
| 158 | + 1 + repack (concurrently) foo; |
| 159 | + ╰╴ |
| 160 | + "); |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn vacuum_full_false_option_list_ok() { |
| 165 | + let sql = "VACUUM (FULL FALSE) foo;"; |
| 166 | + lint_ok_with(sql, pg19(), Rule::PreferRepack); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + fn cluster_below_pg19_ok() { |
| 171 | + let sql = "CLUSTER foo;"; |
| 172 | + lint_ok(sql, Rule::PreferRepack); |
| 173 | + } |
| 174 | + |
| 175 | + #[test] |
| 176 | + fn vacuum_full_below_pg19_ok() { |
| 177 | + let sql = "VACUUM FULL foo;"; |
| 178 | + lint_ok(sql, Rule::PreferRepack); |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn vacuum_no_full_ok() { |
| 183 | + let sql = "VACUUM foo;"; |
| 184 | + lint_ok_with(sql, pg19(), Rule::PreferRepack); |
| 185 | + } |
| 186 | + |
| 187 | + #[test] |
| 188 | + fn vacuum_analyze_ok() { |
| 189 | + let sql = "VACUUM ANALYZE foo;"; |
| 190 | + lint_ok_with(sql, pg19(), Rule::PreferRepack); |
| 191 | + } |
| 192 | + |
| 193 | + #[test] |
| 194 | + fn vacuum_freeze_ok() { |
| 195 | + let sql = "VACUUM FREEZE foo;"; |
| 196 | + lint_ok_with(sql, pg19(), Rule::PreferRepack); |
| 197 | + } |
| 198 | + |
| 199 | + fn fix(sql: &str) -> String { |
| 200 | + fix_sql_with(sql, pg19(), Rule::PreferRepack) |
| 201 | + } |
| 202 | + |
| 203 | + #[test] |
| 204 | + fn fix_vacuum_full() { |
| 205 | + assert_snapshot!(fix("VACUUM FULL foo;"), @"repack (concurrently) foo;"); |
| 206 | + } |
| 207 | + |
| 208 | + #[test] |
| 209 | + fn fix_vacuum_full_option_list() { |
| 210 | + assert_snapshot!(fix("VACUUM (FULL) foo;"), @"repack (concurrently) foo;"); |
| 211 | + } |
| 212 | + |
| 213 | + #[test] |
| 214 | + fn fix_vacuum_full_multiple_tables() { |
| 215 | + assert_snapshot!(fix("VACUUM FULL foo, bar;"), @"repack (concurrently) foo, bar;"); |
| 216 | + } |
| 217 | + |
| 218 | + #[test] |
| 219 | + fn fix_cluster_no_index() { |
| 220 | + assert_snapshot!(fix("CLUSTER foo;"), @"repack (concurrently) foo;"); |
| 221 | + } |
| 222 | + |
| 223 | + #[test] |
| 224 | + fn fix_cluster_with_index() { |
| 225 | + assert_snapshot!(fix("CLUSTER foo USING foo_pkey;"), @"repack (concurrently) foo using index foo_pkey;"); |
| 226 | + } |
| 227 | + |
| 228 | + #[test] |
| 229 | + fn fix_cluster_legacy_on_syntax() { |
| 230 | + assert_snapshot!(fix("CLUSTER verbose foo_pkey ON foo;"), @"repack (concurrently) foo using index foo_pkey;"); |
| 231 | + } |
| 232 | + |
| 233 | + #[test] |
| 234 | + fn fix_cluster_no_path() { |
| 235 | + assert_snapshot!(fix("CLUSTER;"), @"repack (concurrently);"); |
| 236 | + } |
| 237 | +} |
0 commit comments