Skip to content

Commit ac38018

Browse files
authored
docs: update readme example output & adjust rule copy (#447)
Trying to make the rule copy more consistent, but will need to go through in the future and figure out a proper style guide for it. Also updated the readme example with the new annotate-snippets powered tty output!
1 parent b04a423 commit ac38018

17 files changed

+89
-60
lines changed

README.md

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,70 @@ pip install squawk-cli
2121
https://github.com/sbdchd/squawk/releases
2222
```
2323

24-
### Without installation (Docker)
24+
### Or via Docker
2525

26-
You can run Squawk without installation using Docker. The official image is available on GitHub Container Registry.
26+
You can also run Squawk using Docker. The official image is available on GitHub Container Registry.
2727

2828
```shell
2929
# Assuming you want to check sql files in the current directory
3030
docker run --rm -v $(pwd):/data ghcr.io/sbdchd/squawk:latest *.sql
3131
```
3232

33-
## Usage
34-
35-
```shell
36-
❯ squawk example.sql
37-
example.sql:2:1: warning: prefer-text-field
38-
39-
2 | --
40-
3 | -- Create model Bar
41-
4 | --
42-
5 | CREATE TABLE "core_bar" (
43-
6 | "id" serial NOT NULL PRIMARY KEY,
44-
7 | "alpha" varchar(100) NOT NULL
45-
8 | );
46-
47-
note: Changing the size of a varchar field requires an ACCESS EXCLUSIVE lock.
48-
help: Use a text field with a check constraint.
33+
### Or via the Playground
4934

50-
example.sql:9:2: warning: require-concurrent-index-creation
35+
Use the WASM powered playground to check your SQL locally in the browser!
5136

52-
9 |
53-
10 | CREATE INDEX "field_name_idx" ON "table_name" ("field_name");
37+
<https://play.squawkhq.com>
5438

55-
note: Creating an index blocks writes.
56-
note: Create the index CONCURRENTLY.
57-
58-
example.sql:11:2: warning: disallowed-unique-constraint
59-
60-
11 |
61-
12 | ALTER TABLE table_name ADD CONSTRAINT field_name_constraint UNIQUE (field_name);
39+
## Usage
6240

63-
note: Adding a UNIQUE constraint requires an ACCESS EXCLUSIVE lock which blocks reads.
64-
help: Create an index CONCURRENTLY and create the constraint using the index.
41+
```shell
42+
❯ squawk example.sql
43+
warning[prefer-bigint-over-int]: Using 32-bit integer fields can result in hitting the max `int` limit.
44+
--> example.sql:6:10
45+
|
46+
6 | "id" serial NOT NULL PRIMARY KEY,
47+
| ^^^^^^
48+
|
49+
= help: Use 64-bit integer values instead to prevent hitting this limit.
50+
warning[prefer-identity]: Serial types make schema, dependency, and permission management difficult.
51+
--> example.sql:6:10
52+
|
53+
6 | "id" serial NOT NULL PRIMARY KEY,
54+
| ^^^^^^
55+
|
56+
= help: Use Identity columns instead.
57+
warning[prefer-text-field]: Changing the size of a `varchar` field requires an `ACCESS EXCLUSIVE` lock, that will prevent all reads and writes to the table.
58+
--> example.sql:7:13
59+
|
60+
7 | "alpha" varchar(100) NOT NULL
61+
| ^^^^^^^^^^^^
62+
|
63+
= help: Use a `TEXT` field with a `CHECK` constraint.
64+
warning[require-concurrent-index-creation]: During normal index creation, table updates are blocked, but reads are still allowed.
65+
--> example.sql:10:1
66+
|
67+
10 | CREATE INDEX "field_name_idx" ON "table_name" ("field_name");
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
69+
|
70+
= help: Use `CONCURRENTLY` to avoid blocking writes.
71+
warning[constraint-missing-not-valid]: By default new constraints require a table scan and block writes to the table while that scan occurs.
72+
--> example.sql:12:24
73+
|
74+
12 | ALTER TABLE table_name ADD CONSTRAINT field_name_constraint UNIQUE (field_name);
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76+
|
77+
= help: Use `NOT VALID` with a later `VALIDATE CONSTRAINT` call.
78+
warning[disallowed-unique-constraint]: Adding a `UNIQUE` constraint requires an `ACCESS EXCLUSIVE` lock which blocks reads and writes to the table while the index is built.
79+
--> example.sql:12:28
80+
|
81+
12 | ALTER TABLE table_name ADD CONSTRAINT field_name_constraint UNIQUE (field_name);
82+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
83+
|
84+
= help: Create an index `CONCURRENTLY` and create the constraint using the index.
85+
86+
Find detailed examples and solutions for each rule at https://squawkhq.com/docs/rules
87+
Found 7 issues in 1 file (checked 1 source file)
6588
```
6689
6790
### `squawk --help`

crates/squawk_linter/src/rules/constraint_missing_not_valid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub(crate) fn constraint_missing_not_valid(ctx: &mut Linter, parse: &Parse<Sourc
131131
Rule::ConstraintMissingNotValid,
132132
"By default new constraints require a table scan and block writes to the table while that scan occurs.".into(),
133133
add_constraint.syntax().text_range(),
134-
None,
134+
"Use `NOT VALID` with a later `VALIDATE CONSTRAINT` call.".to_string(),
135135
));
136136
}
137137
}

crates/squawk_linter/src/rules/disallow_unique_constraint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::constraint_missing_not_valid::tables_created_in_transaction;
99

1010
pub(crate) fn disallow_unique_constraint(ctx: &mut Linter, parse: &Parse<SourceFile>) {
1111
let message = "Adding a `UNIQUE` constraint requires an `ACCESS EXCLUSIVE` lock which blocks reads and writes to the table while the index is built.";
12-
let help = "Create an index CONCURRENTLY and create the constraint using the index.";
12+
let help = "Create an index `CONCURRENTLY` and create the constraint using the index.";
1313
let file = parse.tree();
1414
let tables_created = tables_created_in_transaction(ctx.settings.assume_in_transaction, &file);
1515
for item in file.items() {

crates/squawk_linter/src/rules/prefer_identity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ fn check_ty_for_serial(ctx: &mut Linter, ty: Option<ast::Type>) {
2727
if is_not_valid_int_type(&ty, &SERIAL_TYPES) {
2828
ctx.report(Violation::new(
2929
Rule::PreferIdentity,
30-
"Serial types make schema, dependency, and permission management difficult. Use Identity columns instead.".into(),
30+
"Serial types make schema, dependency, and permission management difficult.".into(),
3131
ty.syntax().text_range(),
32-
"Use Identity columns instead.".to_string(),
32+
"Use an `IDENTITY` column instead.".to_string(),
3333
));
3434
};
3535
}

crates/squawk_linter/src/rules/prefer_text_field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn check_ty_for_varchar(ctx: &mut Linter, ty: Option<ast::Type>) {
5454
Rule::PreferTextField,
5555
"Changing the size of a `varchar` field requires an `ACCESS EXCLUSIVE` lock, that will prevent all reads and writes to the table.".to_string(),
5656
ty.syntax().text_range(),
57-
"Use a `text` field with a `check` constraint.".to_string(),
57+
"Use a `TEXT` field with a `CHECK` constraint.".to_string(),
5858
));
5959
};
6060
}

crates/squawk_linter/src/rules/require_concurrent_index_creation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ pub(crate) fn require_concurrent_index_creation(ctx: &mut Linter, parse: &Parse<
2222
{
2323
ctx.report(Violation::new(
2424
Rule::RequireConcurrentIndexCreation,
25-
"During a normal index creation, table updates are blocked, but reads are still allowed. `CONCURRENTLY` avoids locking the table against writes during index creation.".into(),
25+
"During normal index creation, table updates are blocked, but reads are still allowed.".into(),
2626
create_index.syntax().text_range(),
27-
None,
27+
"Use `CONCURRENTLY` to avoid blocking writes.".to_string(),
2828
));
2929
}
3030
}

crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__constraint_missing_not_valid__test__adding_check_constraint_err.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ expression: errors
77
code: ConstraintMissingNotValid,
88
message: "By default new constraints require a table scan and block writes to the table while that scan occurs.",
99
text_range: 38..94,
10-
help: None,
10+
help: Some(
11+
"Use `NOT VALID` with a later `VALIDATE CONSTRAINT` call.",
12+
),
1113
},
1214
]

crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__constraint_missing_not_valid__test__adding_fk_err.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ expression: errors
77
code: ConstraintMissingNotValid,
88
message: "By default new constraints require a table scan and block writes to the table while that scan occurs.",
99
text_range: 40..114,
10-
help: None,
10+
help: Some(
11+
"Use `NOT VALID` with a later `VALIDATE CONSTRAINT` call.",
12+
),
1113
},
1214
]

crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__disallow_unique_constraint__test__adding_unique_constraint_err.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ expression: errors
88
message: "Adding a `UNIQUE` constraint requires an `ACCESS EXCLUSIVE` lock which blocks reads and writes to the table while the index is built.",
99
text_range: 28..80,
1010
help: Some(
11-
"Create an index CONCURRENTLY and create the constraint using the index.",
11+
"Create an index `CONCURRENTLY` and create the constraint using the index.",
1212
),
1313
},
1414
]

crates/squawk_linter/src/rules/snapshots/squawk_linter__rules__disallow_unique_constraint__test__unique_constraint_inline_add_column_err.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ expression: errors
88
message: "Adding a `UNIQUE` constraint requires an `ACCESS EXCLUSIVE` lock which blocks reads and writes to the table while the index is built.",
99
text_range: 37..69,
1010
help: Some(
11-
"Create an index CONCURRENTLY and create the constraint using the index.",
11+
"Create an index `CONCURRENTLY` and create the constraint using the index.",
1212
),
1313
},
1414
]

0 commit comments

Comments
 (0)