Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## v1.6.1 - 2025-05-02

### Fixed

- Fixed panic when formatting violations (#426).

## v1.6.0 - 2025-04-02

### Added

- Added `ban-alter-domain-with-add-constraint` and `ban-create-domain-with-constraint` (#418). Thanks @johnmastro!

## v1.5.5 - 2025-03-20
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "squawk"
version = "1.6.0"
version = "1.6.1"
authors = ["Steve Dignam <steve@dignam.xyz>"]
edition = "2018"
license = "GPL-3.0"
Expand Down
57 changes: 53 additions & 4 deletions cli/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,17 +386,28 @@ pub fn pretty_violations(
#[allow(clippy::cast_sign_loss)]
let start = start as usize;

// 1-indexed
// remove the leading whitespace on last line
let lineno = sql[..start].trim_end().lines().count() + 1;
let mut lineno = 0;

for (idx, char) in sql.chars().enumerate() {
if char == '\n' {
lineno += 1;
}

if idx == start {
break;
}
}

lineno += 1;

let content = if let Some(len) = len {
#[allow(clippy::cast_sign_loss)]
&sql[start..=start + len as usize]
} else {
// Use current line
let tail = sql[start..].find('\n').unwrap_or(sql.len() - start);
&sql[start..=start + tail]

&sql.chars().skip(start).take(tail + 1).collect::<String>()
};

// TODO(sbdchd): could remove the leading whitespace and comments to
Expand Down Expand Up @@ -666,6 +677,7 @@ mod test_reporter {
check_sql_with_rule,
violations::{RuleViolation, RuleViolationKind},
};
use squawk_parser::ast::Span;

fn lint_sql(sql: &str) -> Vec<RuleViolation> {
check_sql_with_rule(sql, &RuleViolationKind::AddingRequiredField, None, false).unwrap()
Expand Down Expand Up @@ -825,4 +837,41 @@ SELECT 1;
}
"#);
}

#[test]
fn regression_slicing_issue_425() {
// Squawk was crashing with an slicing issue.
let sql = "ALTER TABLE test ADD COLUMN IF NOT EXISTS test INTEGER;";
let violation = RuleViolation::new(
RuleViolationKind::PreferBigInt,
Span {
start: 42,
len: None,
},
None,
);
pretty_violations(vec![violation], sql, "main.sql");
}
#[test]
fn highlight_column_for_issues() {
// Display only the columns with issues for large DDLs.
fn lint_sql(sql: &str) -> Vec<RuleViolation> {
check_sql_with_rule(sql, &RuleViolationKind::PreferTextField, None, false).unwrap()
}
// Squawk was crashing with an slicing issue.
let sql = "create table test_table (
col1 varchar(255),
col2 varchar(255),
col3 varchar(255)
--- other columns
);";
let violations = lint_sql(sql);
let res = pretty_violations(violations, sql, "main.sql");
let columns = res
.violations
.iter()
.map(|v| v.sql.clone())
.collect::<String>();
assert_display_snapshot!(columns);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: cli/src/reporter.rs
expression: columns
---
col1 varchar(255),
col2 varchar(255),
col3 varchar(255)

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ViolationContent {
violations: [
ReportViolation {
file: "main.sql",
line: 1,
line: 2,
column: 2,
level: Warning,
messages: [
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{
squawk = final.rustPlatform.buildRustPackage {
pname = "squawk";
version = "1.6.0";
version = "1.6.1";

cargoLock = {
lockFile = ./Cargo.lock;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "squawk-cli",
"version": "1.6.0",
"version": "1.6.1",
"description": "linter for PostgreSQL, focused on migrations",
"repository": "git@github.com:sbdchd/squawk.git",
"author": "Steve Dignam <steve@dignam.xyz>",
Expand Down
Loading