Skip to content

Commit 00298b7

Browse files
authored
Merge pull request #108 from supabase/fix/improve-error-msgs
improve error msgs
2 parents d6a1583 + 8f39afe commit 00298b7

File tree

5 files changed

+104
-25
lines changed

5 files changed

+104
-25
lines changed

cli/Cargo.lock

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ anyhow = "1.0.69"
1111
clap = { version = "4.1.6", features = ["derive"] }
1212
dirs = "5.0.1"
1313
postgrest = "1.5.0"
14+
regex = "1.9"
1415
reqwest = { version = "0.11.14", features = ["json", "native-tls-vendored"] }
1516
rpassword = "7.2.0"
1617
serde = { version = "1.0.156", features = ["derive"] }

cli/src/commands/publish.rs

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ pub async fn publish(
2424
let request = create_publish_package_request(&payload);
2525
client.publish_package(&jwt, &request).await?;
2626

27+
if payload.install_files.is_empty() {
28+
return Err(anyhow::anyhow!("No valid script file (.sql) found."));
29+
}
30+
2731
let mut num_published = 0;
2832

2933
for install_file in &payload.install_files {

cli/src/models.rs

+39-22
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl Payload {
146146

147147
if !util::is_valid_extension_name(&extension_name) {
148148
return Err(anyhow::anyhow!(
149-
"invalid extension name detected {}",
149+
"Invalid extension name detected: {}. It must begin with an alphabet, contain only alphanumeric characters or `_` and should be between 2 and 32 characters long.",
150150
extension_name
151151
));
152152
}
@@ -166,31 +166,46 @@ impl Payload {
166166
match &parts[..] {
167167
[file_ext_name, ver] => {
168168
// Make sure the file's extension name matches the control file
169-
if file_ext_name == &extension_name && util::is_valid_version(ver) {
170-
let ifile = InstallFile {
171-
filename: file_name.to_string(),
172-
version: ver.to_string(),
173-
body: fs::read_to_string(&path)
174-
.context(format!("Failed to read file {}", &file_name))?,
175-
};
176-
install_files.push(ifile);
169+
if file_ext_name != &extension_name {
170+
println!("Warning: file `{file_name}` will be skipped because its extension name(`{file_ext_name}`) doesn't match `{extension_name}`");
171+
continue;
172+
}
173+
if !util::is_valid_version(ver) {
174+
println!("Warning: file `{file_name}` will be skipped because its version (`{ver}`) is invalid. It should be have the format `major.minor.patch`.");
175+
continue;
177176
}
177+
178+
let ifile = InstallFile {
179+
filename: file_name.to_string(),
180+
version: ver.to_string(),
181+
body: fs::read_to_string(&path)
182+
.context(format!("Failed to read file {}", &file_name))?,
183+
};
184+
install_files.push(ifile);
178185
}
179186
[file_ext_name, from_ver, to_ver] => {
180187
// Make sure the file's extension name matches the control file
181-
if file_ext_name == &extension_name
182-
&& util::is_valid_version(from_ver)
183-
&& util::is_valid_version(to_ver)
184-
{
185-
let ufile = UpgradeFile {
186-
filename: file_name.to_string(),
187-
from_version: from_ver.to_string(),
188-
to_version: to_ver.to_string(),
189-
body: fs::read_to_string(&path)
190-
.context(format!("Failed to read file {}", &file_name))?,
191-
};
192-
upgrade_files.push(ufile);
188+
if file_ext_name != &extension_name {
189+
println!("Warning: file `{file_name}` will be skipped because its extension name(`{file_ext_name}`) doesn't match `{extension_name}`");
190+
continue;
193191
}
192+
if !util::is_valid_version(from_ver) {
193+
println!("Warning: file `{file_name}` will be skipped because its from version(`{from_ver}`) is invalid. It should be have the format `major.minor.patch`.");
194+
continue;
195+
}
196+
if !util::is_valid_version(to_ver) {
197+
println!("Warning: file `{file_name}` will be skipped because its from version(`{to_ver}`) is invalid. It should be have the format `major.minor.patch`.");
198+
continue;
199+
}
200+
201+
let ufile = UpgradeFile {
202+
filename: file_name.to_string(),
203+
from_version: from_ver.to_string(),
204+
to_version: to_ver.to_string(),
205+
body: fs::read_to_string(&path)
206+
.context(format!("Failed to read file {}", &file_name))?,
207+
};
208+
upgrade_files.push(ufile);
194209
}
195210
_ => (),
196211
}
@@ -264,7 +279,9 @@ impl ControlFileRef {
264279
return self.read_control_line_value(line);
265280
}
266281
}
267-
Err(anyhow::anyhow!("default version is required"))
282+
Err(anyhow::anyhow!(
283+
"`default_version` in control file is required"
284+
))
268285
}
269286

270287
fn read_control_line_value(&self, line: &str) -> anyhow::Result<String> {

cli/src/util.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::Context;
2+
use regex::Regex;
23
use sqlx::postgres::PgConnection;
34
use sqlx::Connection;
45
use std::fs::{File, OpenOptions};
@@ -10,11 +11,28 @@ pub async fn get_connection(connection_str: &str) -> anyhow::Result<PgConnection
1011
.context("Failed to establish PostgreSQL connection")
1112
}
1213

13-
pub fn is_valid_extension_name(_name: &str) -> bool {
14-
true
14+
pub fn is_valid_extension_name(name: &str) -> bool {
15+
let name_regex = Regex::new(r"^[A-z][A-z0-9\_]{2,32}$").expect("regex is valid");
16+
name_regex.is_match(name)
1517
}
1618

17-
pub fn is_valid_version(_version: &str) -> bool {
19+
pub fn is_valid_version(version: &str) -> bool {
20+
let nums: Vec<&str> = version.split('.').collect();
21+
if nums.len() != 3 {
22+
println!("1");
23+
return false;
24+
}
25+
26+
for num_str in nums {
27+
let num: i16 = match num_str.parse() {
28+
Ok(n) => n,
29+
_ => return false,
30+
};
31+
if num < 0 {
32+
return false;
33+
}
34+
}
35+
1836
true
1937
}
2038

0 commit comments

Comments
 (0)