Skip to content

Commit 8af181b

Browse files
fixie fixie messagie
1 parent 97c566a commit 8af181b

File tree

6 files changed

+71
-5
lines changed

6 files changed

+71
-5
lines changed

crates/pgt_typecheck/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pgt_schema_cache.workspace = true
2222
pgt_text_size.workspace = true
2323
pgt_treesitter.workspace = true
2424
pgt_treesitter_grammar.workspace = true
25+
tracing.workspace = true
2526
regex = "1.11.1"
2627
sqlx.workspace = true
2728
tokio.workspace = true

crates/pgt_typecheck/src/diagnostics.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ struct ErrorRewriteRule {
104104
rewrite: fn(&regex::Captures, &IdentifierReplacement) -> String,
105105
}
106106

107-
static ERROR_RULES: Lazy<Vec<ErrorRewriteRule>> = Lazy::new(|| {
107+
static ERROR_REWRITE_RULES: Lazy<Vec<ErrorRewriteRule>> = Lazy::new(|| {
108108
vec![
109109
ErrorRewriteRule {
110-
pattern: Regex::new(r#"invalid input syntax for type (\w+): "([^"]*)""#).unwrap(),
110+
pattern: Regex::new(r#"invalid input syntax for type ([\w\s]+): "([^"]*)""#).unwrap(),
111111
rewrite: |caps, replacement| {
112112
let expected_type = &caps[1];
113113
format!(
@@ -134,14 +134,16 @@ pub fn rewrite_error_message(
134134
pg_error_message: &str,
135135
replacement: &IdentifierReplacement,
136136
) -> String {
137-
// try each rule
138-
for rule in ERROR_RULES.iter() {
137+
tracing::debug!("Rewriting error message: {}", pg_error_message);
138+
for rule in ERROR_REWRITE_RULES.iter() {
139139
if let Some(caps) = rule.pattern.captures(pg_error_message) {
140140
return (rule.rewrite)(&caps, replacement);
141141
}
142142
}
143143

144-
// fallback: generic value replacement
144+
// if we don't have a matching error-rewrite-rule,
145+
// we'll fallback to replacing default values with their types,
146+
// e.g. `""` is replaced with `text`.
145147
let unquoted_default = replacement.default_value.trim_matches('\'');
146148
pg_error_message
147149
.replace(&format!("\"{}\"", unquoted_default), &replacement.type_name)

crates/pgt_typecheck/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub async fn check_sql(
4848
// each typecheck operation.
4949
conn.close_on_drop();
5050

51+
tracing::debug!("replacing types with {:#?}", params.identifiers);
5152
let typed_replacement = apply_identifiers(
5253
params.identifiers,
5354
params.schema_cache,

crates/pgt_typecheck/tests/diagnostics.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,50 @@ async fn operator_does_not_exist(test_db: PgPool) {
208208
.test()
209209
.await;
210210
}
211+
212+
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
213+
async fn function_parameter_type_with_multiple_words(test_db: PgPool) {
214+
// the type "timestamp with time zone" has multiple words
215+
let setup = r#"
216+
create table public.products (
217+
id serial primary key,
218+
released timestamp with time zone not null
219+
);
220+
"#;
221+
222+
TestSetup {
223+
name: "testing_type_with_multiple_words",
224+
setup: Some(setup),
225+
query: r#"select * from public.products where released = pid;"#,
226+
test_db: &test_db,
227+
typed_identifiers: vec![TypedIdentifier {
228+
path: "delete_product".to_string(),
229+
name: Some("pid".to_string()),
230+
type_: IdentifierType {
231+
schema: None,
232+
name: "uuid".to_string(),
233+
is_array: false,
234+
},
235+
}],
236+
}
237+
.test()
238+
.await;
239+
240+
TestSetup {
241+
name: "testing_operator_type_with_multiple_words",
242+
setup: None,
243+
query: r#"delete from public.products where released > pid;"#,
244+
test_db: &test_db,
245+
typed_identifiers: vec![TypedIdentifier {
246+
path: "delete_product".to_string(),
247+
name: Some("pid".to_string()),
248+
type_: IdentifierType {
249+
schema: None,
250+
name: "numeric".to_string(),
251+
is_array: false,
252+
},
253+
}],
254+
}
255+
.test()
256+
.await;
257+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: crates/pgt_typecheck/tests/diagnostics.rs
3+
assertion_line: 96
4+
expression: content
5+
---
6+
delete from public.products where ~~~released > pid~~~;
7+
8+
operator does not exist: timestamp with time zone &gt; integer
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: crates/pgt_typecheck/tests/diagnostics.rs
3+
expression: content
4+
---
5+
select * from public.products where released = ~~~pid~~~;
6+
7+
`pid` is of type uuid, not timestamp with time zone

0 commit comments

Comments
 (0)