Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Native Query SQL from files #372

Merged
merged 10 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
94 changes: 91 additions & 3 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

### Added

- Support for reading Native Query SQL from files.
([#372](https://github.com/hasura/ndc-postgres/pull/372))

### Changed

- Do not print information about when the cli decides not to write to a file.
Expand Down
19 changes: 17 additions & 2 deletions crates/configuration/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,29 @@ pub async fn parse_configuration(
environment: impl Environment,
) -> Result<Configuration, Error> {
let configuration_file = configuration_dir.as_ref().join(CONFIGURATION_FILENAME);
let configuration_file_contents = fs::read_to_string(&configuration_file).await?;
let configuration: version3::RawConfiguration =

let configuration_file_contents =
fs::read_to_string(&configuration_file)
.await
.map_err(|err| {
Error::IoErrorButStringified(format!("{}: {}", &configuration_file.display(), err))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want rust to tell me which file it couldn't read, so I added these everywhere. Sorry.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could instead change Error::IoError to take an Option<PathBuf> in addition.

})?;
let mut configuration: version3::RawConfiguration =
serde_json::from_str(&configuration_file_contents).map_err(|error| Error::ParseError {
file_path: configuration_file.clone(),
line: error.line(),
column: error.column(),
message: error.to_string(),
})?;
// look for native query sql file references and read from disk.
for native_query_sql in configuration.metadata.native_queries.0.values_mut() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

step after serialization - read from disk.

native_query_sql.sql = metadata::NativeQuerySqlEither::NativeQuerySql(
native_query_sql
.sql
.from_external(configuration_dir.as_ref())
.map_err(Error::IoErrorButStringified)?,
);
}
let connection_uri =
match configuration.connection_settings.connection_uri {
ConnectionUri(Secret::Plain(uri)) => Ok(uri),
Expand Down
4 changes: 4 additions & 0 deletions crates/configuration/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub enum Error {
file_path: std::path::PathBuf,
message: String,
},

#[error("I/O error: {0}")]
IoErrorButStringified(String),

#[error("I/O error: {0}")]
IoError(#[from] std::io::Error),
}
3 changes: 3 additions & 0 deletions crates/connectors/ndc-postgres/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ impl<Env: Environment + Send + Sync> ConnectorSetup for PostgresSetup<Env> {
]))
}
configuration::Error::IoError(inner) => connector::ParseError::IoError(inner),
configuration::Error::IoErrorButStringified(inner) => {
connector::ParseError::Other(inner.into())
}
})

// Note that we don't log validation errors, because they are part of the normal business
Expand Down
3 changes: 3 additions & 0 deletions crates/query-engine/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ workspace = true
[dependencies]
schemars = { version = "0.8.16", features = ["smol_str"] }
serde = { version = "1.0.197", features = ["derive"] }

[dev-dependencies]
serde_json = "1.0.114"
Loading
Loading