eugene 0.8.3

Careful with That Lock, Eugene
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
508
509
510
511
512
513
514
515
use crate::comments::filter_rules;
pub use crate::lints::ast::StatementSummary;
use crate::output::output_format::{LintReport, LintedStatement};
use crate::sqltext;
use itertools::Itertools;
use regex::Regex;

/// The `ast` module provides a way to describe a parsed SQL statement in a structured way,
/// using simpler trees than the ones provided by `pg_query`.
pub mod ast;
/// The `rules` module contains lint rules that can be matched to `LintedStatement`
pub mod rules;

/// Represents mutable state for linting through a single SQL script.
///
/// This struct is used to keep track of new objects, so the lint rules can check
/// visibility to other transactions, usage of lock timeouts and other properties
/// that require state to be kept between different statements.
#[derive(Debug, Default, Eq, PartialEq)]
pub struct TransactionState {
    locktimeout: bool,
    created_objects: Vec<(String, String)>,
    altered_tables: Vec<(String, String)>,
    has_access_exclusive: bool,
}

impl TransactionState {
    /// Query if the script under linting has previously created an object with the given schema and name.
    pub fn has_created_object(&self, schema: &str, name: &str) -> bool {
        self.created_objects
            .iter()
            .any(|(s, n)| schema.eq_ignore_ascii_case(s) && name.eq_ignore_ascii_case(n))
    }
    /// Query if the script under linting has previously set a lock timeout.
    pub fn has_locktimeout(&self) -> bool {
        self.locktimeout
    }
    /// Update the context with the information from a new statement, logging new objects and lock timeouts.
    pub fn update_from(&mut self, summary: &StatementSummary) {
        if let StatementSummary::LockTimeout = summary {
            self.locktimeout = true;
        }
        summary.created_objects().iter().for_each(|(schema, name)| {
            self.created_objects
                .push((schema.to_string(), name.to_string()))
        });
        match summary {
            StatementSummary::AlterTable { schema, name, .. }
                if !self.has_created_object(schema, name) =>
            {
                self.has_access_exclusive = true;
            }
            _ => {}
        }

        if let StatementSummary::AlterTable { schema, name, .. } = summary {
            let new_item = (schema.to_string(), name.to_string());
            if !self.altered_tables.contains(&new_item) {
                self.altered_tables.push(new_item);
            }
        }
    }
}

#[derive(Copy, Clone)]
pub struct LintContext<'a> {
    pub(crate) ctx: &'a TransactionState,
    pub(crate) statement: &'a StatementSummary,
}

impl<'a> LintContext<'a> {
    pub fn new(ctx: &'a TransactionState, statement: &'a StatementSummary) -> Self {
        LintContext { ctx, statement }
    }
    /// Locks taken by the statement that were not created in the same transaction.
    pub fn locks_visible_outside_tx(&self) -> Vec<(&str, &str)> {
        self.statement
            .lock_targets()
            .iter()
            .filter(|(schema, name)| !self.ctx.has_created_object(schema, name))
            .copied()
            .collect()
    }
    /// True if the statement takes a lock on the given schema and name.
    pub fn takes_lock(&self, target_schema: &str, target_name: &str) -> bool {
        self.statement
            .lock_targets()
            .iter()
            .contains(&(target_schema, target_name))
    }
    /// True if the transaction has set a lock timeout.
    pub fn has_lock_timeout(&self) -> bool {
        self.ctx.has_locktimeout()
    }
    /// True if the lock target was created in another transaction
    pub fn is_visible(&self, schema: &str, name: &str) -> bool {
        !self.ctx.has_created_object(schema, name)
    }
    pub fn holding_access_exclusive(&self) -> bool {
        self.ctx.has_access_exclusive
    }
    /// True if the transaction has previously altered this table
    pub fn has_altered_table(&self, schema: &str, name: &str) -> bool {
        self.ctx
            .altered_tables
            .iter()
            .any(|(s, n)| schema.eq_ignore_ascii_case(s) && name.eq_ignore_ascii_case(n))
    }
}

/// Lint a SQL script and return a report with all matched lints for each statement.
pub fn lint<S: AsRef<str>>(
    name: Option<String>,
    sql: S,
    ignored_lints: &[&str],
    skip_summary: bool,
    skip: &[Regex],
) -> crate::Result<LintReport> {
    let statements = sqltext::sql_statements_with_line_no(sql.as_ref())?;
    let mut ctx = TransactionState::default();
    let mut lints = Vec::new();
    let mut no: usize = 1;
    let mut passed_all = true;
    for (line, stmt) in statements {
        let skip_stmt = skip.iter().any(|r| r.is_match(stmt));
        let action = crate::comments::find_comment_action(sql.as_ref())?;
        let tree = pg_query::parse(stmt)?;
        for raw in tree.protobuf.stmts.iter() {
            if let Some(node) = &raw.stmt {
                if let Some(node_ref) = &node.node {
                    let summary = ast::describe(&node_ref.to_ref())?;
                    let lint_line = LintContext::new(&ctx, &summary);
                    let matched_lints: Vec<_> = if skip_stmt {
                        vec![]
                    } else {
                        filter_rules(&action, rules::all_rules())
                            .filter(|rule| !ignored_lints.contains(&rule.id()))
                            .filter_map(|rule| rule.check(lint_line))
                            .collect()
                    };
                    passed_all = passed_all && matched_lints.is_empty();

                    lints.push(LintedStatement {
                        statement_number: no,
                        line_number: line,
                        sql: stmt.trim().to_string(),
                        triggered_rules: matched_lints,
                    });
                    ctx.update_from(&summary);
                    no += 1;
                }
            }
        }
    }
    Ok(LintReport {
        name,
        statements: lints,
        passed_all_checks: passed_all,
        skip_summary,
    })
}

pub fn anon_lint<S: AsRef<str>>(sql: S) -> crate::Result<LintReport> {
    lint(None, sql, &[], false, &[])
}

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

    fn matched_lint_rule(report: &LintReport, rule_id: &str) -> bool {
        report
            .statements
            .iter()
            .any(|lint| lint.triggered_rules.iter().any(|hint| hint.id == rule_id))
    }

    #[test]
    fn test_no_locktimeout_create_index() {
        let report = anon_lint("create index books_title_idx on books(title);").unwrap();
        assert!(matched_lint_rule(&report, rules::LOCKTIMEOUT_WARNING.id()));
    }

    #[test]
    fn test_locktimeout_create_index_on_new_table() {
        let report = anon_lint(
            "create table books(id serial primary key, title text); \
            create index books_title_idx on books(title);",
        )
        .unwrap();
        assert!(!matched_lint_rule(&report, rules::LOCKTIMEOUT_WARNING.id()));
    }

    #[test]
    fn test_locktimeout_alter_table_without_timeout() {
        let report =
            anon_lint("alter table books add constraint check_price check (price > 0);").unwrap();
        assert!(matched_lint_rule(&report, rules::LOCKTIMEOUT_WARNING.id()));
    }

    #[test]
    fn test_locktimeout_alter_table_with_timeout() {
        let report =
            anon_lint("set lock_timeout = '2s'; create index books_title_idx on books(title);")
                .unwrap();
        assert!(!matched_lint_rule(&report, rules::LOCKTIMEOUT_WARNING.id()));
    }

    #[test]
    fn test_create_index_on_new_table() {
        let report = anon_lint(
            "create table books(id serial primary key, title text); \
            create index books_title_idx on books(title);",
        )
        .unwrap();
        assert!(!matched_lint_rule(
            &report,
            rules::CREATE_INDEX_NONCONCURRENTLY.id()
        ));
    }

    #[test]
    fn test_create_index_concurrently_is_not_dangerous_lock() {
        let report =
            anon_lint("create index concurrently books_title_idx on books(title);").unwrap();
        assert!(!matched_lint_rule(&report, rules::LOCKTIMEOUT_WARNING.id()));
    }

    #[test]
    fn test_create_index_on_existing_table() {
        let report = anon_lint("create index books_title_idx on books(title);").unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::CREATE_INDEX_NONCONCURRENTLY.id()
        ));
    }

    #[test]
    fn test_add_check_constraint_to_existing_table() {
        let report =
            anon_lint("alter table books add constraint check_price check (price > 0);").unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::ADDING_VALID_CONSTRAINT.id()
        ));
    }

    #[test]
    fn test_add_check_constraint_to_new_table() {
        let report = anon_lint(
            "create table books(id serial primary key, title text); \
            alter table books add constraint check_price check (price > 0);",
        )
        .unwrap();
        assert!(!matched_lint_rule(
            &report,
            rules::ADDING_VALID_CONSTRAINT.id()
        ));
    }

    #[test]
    fn test_add_not_valid_constraint_to_existing_table() {
        let report =
            anon_lint("alter table books add constraint check_price check (price > 0) not valid;")
                .unwrap();
        assert!(!matched_lint_rule(
            &report,
            rules::ADDING_VALID_CONSTRAINT.id()
        ));
    }

    #[test]
    fn test_adding_exclusion_constraint_to_existing_table() {
        let report =
            anon_lint("alter table books add constraint exclude_price exclude (price with =);")
                .unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::ADDING_EXCLUSION_CONSTRAINT.id()
        ));
    }

    #[test]
    fn test_adding_exclusion_constraint_on_new_table() {
        let report = anon_lint(
            "create table books(id serial primary key, title text);\
             alter table books add constraint exclude_price exclude (price with =);",
        )
        .unwrap();
        assert!(!matched_lint_rule(
            &report,
            rules::ADDING_EXCLUSION_CONSTRAINT.id()
        ));
    }

    #[test]
    fn test_adding_unique_constraint_using_idx() {
        let report = anon_lint(
            "alter table books add constraint unique_title unique using index unique_title_idx;",
        )
        .unwrap();
        assert!(!matched_lint_rule(
            &report,
            rules::ADD_NEW_UNIQUE_CONSTRAINT_WITHOUT_USING_INDEX.id()
        ));
    }

    #[test]
    fn test_adding_unique_constraint() {
        let report =
            anon_lint("alter table books add constraint unique_title unique (title);").unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::ADD_NEW_UNIQUE_CONSTRAINT_WITHOUT_USING_INDEX.id()
        ));
    }

    #[test]
    fn test_adding_unique_constraint_on_new_table() {
        let report = anon_lint(
            "create table books(id serial primary key, title text);\
             alter table books add constraint unique_title unique (title);",
        )
        .unwrap();
        assert!(!matched_lint_rule(
            &report,
            rules::ADD_NEW_UNIQUE_CONSTRAINT_WITHOUT_USING_INDEX.id()
        ));
    }

    #[test]
    fn test_skipping_with_regex_skips_lint() {
        let regex = Regex::new(".*unique.*").unwrap();

        let sql = "alter table books add constraint unique_title unique (title);";

        let report = lint(None, sql, &[], false, &[regex]).unwrap();

        assert!(!matched_lint_rule(
            &report,
            rules::ADD_NEW_UNIQUE_CONSTRAINT_WITHOUT_USING_INDEX.id()
        ));
    }

    #[test]
    fn test_sets_column_to_not_null_on_visible_table() {
        let report = anon_lint("alter table books alter column title set not null;").unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::MAKE_COLUMN_NOT_NULLABLE_WITH_LOCK.id()
        ));
    }

    #[test]
    fn test_sets_column_to_not_null_on_new_table() {
        let report = anon_lint(
            "create table books(id serial primary key, title text);\
             alter table books alter column title set not null;",
        )
        .unwrap();
        assert!(!matched_lint_rule(
            &report,
            rules::MAKE_COLUMN_NOT_NULLABLE_WITH_LOCK.id()
        ));
    }

    #[test]
    fn test_adding_json_column() {
        let report = anon_lint("alter table books add column data json;").unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::SET_COLUMN_TYPE_TO_JSON.id()
        ));
    }

    #[test]
    fn test_alter_to_json_type() {
        let report = anon_lint("alter table books alter column data type json;").unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::SET_COLUMN_TYPE_TO_JSON.id()
        ));
    }

    #[test]
    fn test_sets_new_data_type_to_column() {
        let report = anon_lint("alter table books alter column data type jsonb;").unwrap();
        assert!(matched_lint_rule(&report, rules::CHANGE_COLUMN_TYPE.id()));
    }

    #[test]
    fn test_ignore_json_rule_id() {
        let id = rules::SET_COLUMN_TYPE_TO_JSON.id();
        let sql = format!("-- eugene: ignore {id}\nalter table books add column data json;");
        let report = anon_lint(sql).unwrap();
        assert!(!matched_lint_rule(&report, id));
    }

    #[test]
    fn test_creates_table_with_json_column() {
        let report = anon_lint("create table books(id serial primary key, data json);").unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::SET_COLUMN_TYPE_TO_JSON.id()
        ));
    }

    #[test]
    fn test_alter_table_add_serial_column() {
        let report = anon_lint("alter table books add column id serial;").unwrap();
        assert!(matched_lint_rule(
            &report,
            ADDED_SERIAL_OR_STORED_GENERATED_COLUMN.id
        ));
        let report = anon_lint("alter table books add column id bigserial;").unwrap();
        assert!(matched_lint_rule(
            &report,
            ADDED_SERIAL_OR_STORED_GENERATED_COLUMN.id
        ));
    }

    #[test]
    fn test_alter_table_generated_always_column() {
        let report =
            anon_lint("alter table books add column id int generated always as identity;").unwrap();
        assert!(!matched_lint_rule(
            &report,
            ADDED_SERIAL_OR_STORED_GENERATED_COLUMN.id
        ));
    }

    #[test]
    fn test_alter_table_stored_generated_column() {
        let report = anon_lint(
            "alter table books add column id int generated always as (1 + old_id) stored;",
        )
        .unwrap();
        assert!(matched_lint_rule(
            &report,
            ADDED_SERIAL_OR_STORED_GENERATED_COLUMN.id
        ));
    }

    #[test]
    fn test_altered_table_several_times() {
        let report = anon_lint(
            "
             alter table books add column data jsonb;\
             alter table books add column price numeric;
        ",
        )
        .unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::MULTIPLE_ALTER_TABLES_WHERE_ONE_WILL_DO.id()
        ));
    }

    #[test]
    fn test_altered_table_once_with_multiple_statements() {
        let report = anon_lint(
            "
             alter table books add column data jsonb, add column price numeric;
        ",
        )
        .unwrap();
        assert!(!matched_lint_rule(
            &report,
            rules::MULTIPLE_ALTER_TABLES_WHERE_ONE_WILL_DO.id()
        ));
    }

    #[test]
    fn test_create_an_enum() {
        let report = anon_lint("create type mood as enum ('happy', 'sad');").unwrap();
        assert!(matched_lint_rule(&report, rules::CREATING_ENUM.id()));
    }

    #[test]
    fn test_add_pk_using_index() {
        let report =
            anon_lint("alter table books add primary key using index books_pkey;").unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::ADD_PRIMARY_KEY_USING_INDEX.id()
        ));
    }

    #[test]
    fn test_add_pk_with_using_index() {
        let sql = "alter table books add primary key (id);";
        let report = anon_lint(sql).unwrap();
        assert!(!matched_lint_rule(
            &report,
            rules::ADD_PRIMARY_KEY_USING_INDEX.id()
        ));
        // Covered by another lint
        assert!(matched_lint_rule(
            &report,
            rules::ADD_NEW_UNIQUE_CONSTRAINT_WITHOUT_USING_INDEX.id()
        ));
    }

    #[test]
    fn test_add_column_with_constraint_to_existing_table() {
        let sql = "ALTER TABLE foo ADD COLUMN IF NOT EXISTS bar BIGINT
	CONSTRAINT my_check CHECK (bar IS NULL OR (bar IS NOT NULL AND qux IS NOT NULL));";
        let report = anon_lint(sql).unwrap();
        assert!(matched_lint_rule(
            &report,
            rules::ADDING_VALID_CONSTRAINT.id()
        ));
    }
}