|
| 1 | +use squawk_syntax::{ |
| 2 | + Parse, SourceFile, |
| 3 | + ast::{self, AstNode}, |
| 4 | +}; |
| 5 | + |
| 6 | +use crate::{Edit, Fix, Linter, Rule, Violation}; |
| 7 | + |
| 8 | +pub(crate) fn ban_uncommitted_transaction(ctx: &mut Linter, parse: &Parse<SourceFile>) { |
| 9 | + let file = parse.tree(); |
| 10 | + let mut uncommitted_begin: Option<ast::Begin> = None; |
| 11 | + |
| 12 | + for stmt in file.stmts() { |
| 13 | + match stmt { |
| 14 | + ast::Stmt::Begin(begin) => { |
| 15 | + uncommitted_begin = Some(begin); |
| 16 | + } |
| 17 | + ast::Stmt::Commit(_) | ast::Stmt::Rollback(_) => { |
| 18 | + uncommitted_begin = None; |
| 19 | + } |
| 20 | + _ => (), |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + if let Some(begin) = uncommitted_begin { |
| 25 | + let end_pos = file.syntax().text_range().end(); |
| 26 | + let fix = Fix::new("Add COMMIT", vec![Edit::insert("\nCOMMIT;\n", end_pos)]); |
| 27 | + |
| 28 | + ctx.report( |
| 29 | + Violation::for_node( |
| 30 | + Rule::BanUncommittedTransaction, |
| 31 | + "Transaction never committed or rolled back.".to_string(), |
| 32 | + begin.syntax(), |
| 33 | + ) |
| 34 | + .help("Add a `COMMIT` or `ROLLBACK` statement to complete the transaction.") |
| 35 | + .fix(Some(fix)), |
| 36 | + ); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +#[cfg(test)] |
| 41 | +mod test { |
| 42 | + use insta::{assert_debug_snapshot, assert_snapshot}; |
| 43 | + |
| 44 | + use crate::{Linter, Rule, test_utils::fix_sql}; |
| 45 | + use squawk_syntax::SourceFile; |
| 46 | + |
| 47 | + #[test] |
| 48 | + fn uncommitted_transaction_err() { |
| 49 | + let sql = r#" |
| 50 | +BEGIN; |
| 51 | +CREATE TABLE users (id bigint); |
| 52 | + "#; |
| 53 | + let file = SourceFile::parse(sql); |
| 54 | + let mut linter = Linter::from([Rule::BanUncommittedTransaction]); |
| 55 | + let errors = linter.lint(&file, sql); |
| 56 | + assert_ne!(errors.len(), 0); |
| 57 | + assert_debug_snapshot!(errors, @r#" |
| 58 | + [ |
| 59 | + Violation { |
| 60 | + code: BanUncommittedTransaction, |
| 61 | + message: "Transaction never committed or rolled back.", |
| 62 | + text_range: 1..6, |
| 63 | + help: Some( |
| 64 | + "Add a `COMMIT` or `ROLLBACK` statement to complete the transaction.", |
| 65 | + ), |
| 66 | + fix: Some( |
| 67 | + Fix { |
| 68 | + title: "Add COMMIT", |
| 69 | + edits: [ |
| 70 | + Edit { |
| 71 | + text_range: 48..48, |
| 72 | + text: Some( |
| 73 | + "\nCOMMIT;\n", |
| 74 | + ), |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + ), |
| 79 | + }, |
| 80 | + ] |
| 81 | + "#); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn committed_transaction_ok() { |
| 86 | + let sql = r#" |
| 87 | +BEGIN; |
| 88 | +CREATE TABLE users (id bigint); |
| 89 | +COMMIT; |
| 90 | + "#; |
| 91 | + let file = SourceFile::parse(sql); |
| 92 | + let mut linter = Linter::from([Rule::BanUncommittedTransaction]); |
| 93 | + let errors = linter.lint(&file, sql); |
| 94 | + assert_eq!(errors.len(), 0); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn rolled_back_transaction_ok() { |
| 99 | + let sql = r#" |
| 100 | +BEGIN; |
| 101 | +CREATE TABLE users (id bigint); |
| 102 | +ROLLBACK; |
| 103 | + "#; |
| 104 | + let file = SourceFile::parse(sql); |
| 105 | + let mut linter = Linter::from([Rule::BanUncommittedTransaction]); |
| 106 | + let errors = linter.lint(&file, sql); |
| 107 | + assert_eq!(errors.len(), 0); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn no_transaction_ok() { |
| 112 | + let sql = r#" |
| 113 | +CREATE TABLE users (id bigint); |
| 114 | + "#; |
| 115 | + let file = SourceFile::parse(sql); |
| 116 | + let mut linter = Linter::from([Rule::BanUncommittedTransaction]); |
| 117 | + let errors = linter.lint(&file, sql); |
| 118 | + assert_eq!(errors.len(), 0); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn multiple_transactions_last_uncommitted_err() { |
| 123 | + let sql = r#" |
| 124 | +BEGIN; |
| 125 | +CREATE TABLE users (id bigint); |
| 126 | +COMMIT; |
| 127 | +
|
| 128 | +BEGIN; |
| 129 | +CREATE TABLE posts (id bigint); |
| 130 | + "#; |
| 131 | + let file = SourceFile::parse(sql); |
| 132 | + let mut linter = Linter::from([Rule::BanUncommittedTransaction]); |
| 133 | + let errors = linter.lint(&file, sql); |
| 134 | + assert_ne!(errors.len(), 0); |
| 135 | + assert_debug_snapshot!(errors, @r#" |
| 136 | + [ |
| 137 | + Violation { |
| 138 | + code: BanUncommittedTransaction, |
| 139 | + message: "Transaction never committed or rolled back.", |
| 140 | + text_range: 49..54, |
| 141 | + help: Some( |
| 142 | + "Add a `COMMIT` or `ROLLBACK` statement to complete the transaction.", |
| 143 | + ), |
| 144 | + fix: Some( |
| 145 | + Fix { |
| 146 | + title: "Add COMMIT", |
| 147 | + edits: [ |
| 148 | + Edit { |
| 149 | + text_range: 96..96, |
| 150 | + text: Some( |
| 151 | + "\nCOMMIT;\n", |
| 152 | + ), |
| 153 | + }, |
| 154 | + ], |
| 155 | + }, |
| 156 | + ), |
| 157 | + }, |
| 158 | + ] |
| 159 | + "#); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn start_transaction_uncommitted_err() { |
| 164 | + let sql = r#" |
| 165 | +START TRANSACTION; |
| 166 | +CREATE TABLE users (id bigint); |
| 167 | + "#; |
| 168 | + let file = SourceFile::parse(sql); |
| 169 | + let mut linter = Linter::from([Rule::BanUncommittedTransaction]); |
| 170 | + let errors = linter.lint(&file, sql); |
| 171 | + assert_ne!(errors.len(), 0); |
| 172 | + assert_debug_snapshot!(errors, @r#" |
| 173 | + [ |
| 174 | + Violation { |
| 175 | + code: BanUncommittedTransaction, |
| 176 | + message: "Transaction never committed or rolled back.", |
| 177 | + text_range: 1..18, |
| 178 | + help: Some( |
| 179 | + "Add a `COMMIT` or `ROLLBACK` statement to complete the transaction.", |
| 180 | + ), |
| 181 | + fix: Some( |
| 182 | + Fix { |
| 183 | + title: "Add COMMIT", |
| 184 | + edits: [ |
| 185 | + Edit { |
| 186 | + text_range: 60..60, |
| 187 | + text: Some( |
| 188 | + "\nCOMMIT;\n", |
| 189 | + ), |
| 190 | + }, |
| 191 | + ], |
| 192 | + }, |
| 193 | + ), |
| 194 | + }, |
| 195 | + ] |
| 196 | + "#); |
| 197 | + } |
| 198 | + |
| 199 | + #[test] |
| 200 | + fn nested_begin_only_last_uncommitted_err() { |
| 201 | + let sql = r#" |
| 202 | +BEGIN; |
| 203 | +BEGIN; |
| 204 | +COMMIT; |
| 205 | + "#; |
| 206 | + let file = SourceFile::parse(sql); |
| 207 | + let mut linter = Linter::from([Rule::BanUncommittedTransaction]); |
| 208 | + let errors = linter.lint(&file, sql); |
| 209 | + assert_eq!(errors.len(), 0); |
| 210 | + } |
| 211 | + |
| 212 | + #[test] |
| 213 | + fn begin_work_uncommitted_err() { |
| 214 | + let sql = r#" |
| 215 | +BEGIN WORK; |
| 216 | +CREATE TABLE users (id bigint); |
| 217 | + "#; |
| 218 | + let file = SourceFile::parse(sql); |
| 219 | + let mut linter = Linter::from([Rule::BanUncommittedTransaction]); |
| 220 | + let errors = linter.lint(&file, sql); |
| 221 | + assert_ne!(errors.len(), 0); |
| 222 | + assert_debug_snapshot!(errors, @r#" |
| 223 | + [ |
| 224 | + Violation { |
| 225 | + code: BanUncommittedTransaction, |
| 226 | + message: "Transaction never committed or rolled back.", |
| 227 | + text_range: 1..11, |
| 228 | + help: Some( |
| 229 | + "Add a `COMMIT` or `ROLLBACK` statement to complete the transaction.", |
| 230 | + ), |
| 231 | + fix: Some( |
| 232 | + Fix { |
| 233 | + title: "Add COMMIT", |
| 234 | + edits: [ |
| 235 | + Edit { |
| 236 | + text_range: 53..53, |
| 237 | + text: Some( |
| 238 | + "\nCOMMIT;\n", |
| 239 | + ), |
| 240 | + }, |
| 241 | + ], |
| 242 | + }, |
| 243 | + ), |
| 244 | + }, |
| 245 | + ] |
| 246 | + "#); |
| 247 | + } |
| 248 | + |
| 249 | + #[test] |
| 250 | + fn fix_adds_commit() { |
| 251 | + let sql = r#" |
| 252 | +BEGIN; |
| 253 | +CREATE TABLE users (id bigint); |
| 254 | + "#; |
| 255 | + let fixed = fix_sql(sql, Rule::BanUncommittedTransaction); |
| 256 | + assert_snapshot!(fixed, @r" |
| 257 | + BEGIN; |
| 258 | + CREATE TABLE users (id bigint); |
| 259 | + |
| 260 | + COMMIT; |
| 261 | + "); |
| 262 | + } |
| 263 | + |
| 264 | + #[test] |
| 265 | + fn fix_adds_commit_to_start_transaction() { |
| 266 | + let sql = r#"START TRANSACTION; |
| 267 | +CREATE TABLE posts (id bigint);"#; |
| 268 | + let fixed = fix_sql(sql, Rule::BanUncommittedTransaction); |
| 269 | + assert_snapshot!(fixed, @r"START TRANSACTION; |
| 270 | +CREATE TABLE posts (id bigint); |
| 271 | +COMMIT; |
| 272 | +"); |
| 273 | + } |
| 274 | +} |
0 commit comments