Skip to content

Commit b94fd3a

Browse files
author
Gil Mizrahi
authored
Read Native Query SQL from files (#372)
<!-- The PR description should answer 2 (maybe 3) important questions: --> ### What Currently we can specify native queries by inlining their SQL as a string in the `sql` field. We'd like to be able to reference a `.sql` file instead, so we can avoid escaping characters and can use newlines. The new config format will accept the following structures: ```json "sql": "<inline sql>" (as before, but also:) "sql": { "inline": "<inline sql>" } "sql": { "file": "<relative file path>" } ``` ### How - We parse the `NativeQuerySql` which represents the SQL field using an intermediate new type `NativeQuerySqlExternal`. - as part of the parse_configuration step and after serde deserialization, we traverse the metadata and convert `NativeQuerySqlExternal` to `NativeQuerySql` by reading from file (performing IO) if needed, and parsing to `NativeQueryParts`. We use the configuration directory that the user supplied to locate the relative path. We replace the existing `sql` field in nativequeryinfo with an Either external or internal representation. This is not ideal because it breaks the parse don't validate principle. We would like to mitigate this in the future by separating the `RawConfiguration` types and the (execution time) `Configuration` types completely. We will do that as part of a larger ticket of redesigning the configuration format so it can be split across multiple files.
1 parent 690dfe9 commit b94fd3a

File tree

24 files changed

+590
-121
lines changed

24 files changed

+590
-121
lines changed

Cargo.lock

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

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- Support for reading Native Query SQL from files.
8+
([#372](https://github.com/hasura/ndc-postgres/pull/372))
9+
510
### Changed
611

712
- Do not print information about when the cli decides not to write to a file.

crates/configuration/src/configuration.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,29 @@ pub async fn parse_configuration(
6060
environment: impl Environment,
6161
) -> Result<Configuration, Error> {
6262
let configuration_file = configuration_dir.as_ref().join(CONFIGURATION_FILENAME);
63-
let configuration_file_contents = fs::read_to_string(&configuration_file).await?;
64-
let configuration: version3::RawConfiguration =
63+
64+
let configuration_file_contents =
65+
fs::read_to_string(&configuration_file)
66+
.await
67+
.map_err(|err| {
68+
Error::IoErrorButStringified(format!("{}: {}", &configuration_file.display(), err))
69+
})?;
70+
let mut configuration: version3::RawConfiguration =
6571
serde_json::from_str(&configuration_file_contents).map_err(|error| Error::ParseError {
6672
file_path: configuration_file.clone(),
6773
line: error.line(),
6874
column: error.column(),
6975
message: error.to_string(),
7076
})?;
77+
// look for native query sql file references and read from disk.
78+
for native_query_sql in configuration.metadata.native_queries.0.values_mut() {
79+
native_query_sql.sql = metadata::NativeQuerySqlEither::NativeQuerySql(
80+
native_query_sql
81+
.sql
82+
.from_external(configuration_dir.as_ref())
83+
.map_err(Error::IoErrorButStringified)?,
84+
);
85+
}
7186
let connection_uri =
7287
match configuration.connection_settings.connection_uri {
7388
ConnectionUri(Secret::Plain(uri)) => Ok(uri),

crates/configuration/src/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ pub enum Error {
2121
file_path: std::path::PathBuf,
2222
message: String,
2323
},
24+
25+
#[error("I/O error: {0}")]
26+
IoErrorButStringified(String),
27+
2428
#[error("I/O error: {0}")]
2529
IoError(#[from] std::io::Error),
2630
}

crates/connectors/ndc-postgres/src/connector.rs

+3
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ impl<Env: Environment + Send + Sync> ConnectorSetup for PostgresSetup<Env> {
253253
]))
254254
}
255255
configuration::Error::IoError(inner) => connector::ParseError::IoError(inner),
256+
configuration::Error::IoErrorButStringified(inner) => {
257+
connector::ParseError::Other(inner.into())
258+
}
256259
})
257260

258261
// Note that we don't log validation errors, because they are part of the normal business

crates/query-engine/metadata/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ workspace = true
1010
[dependencies]
1111
schemars = { version = "0.8.16", features = ["smol_str"] }
1212
serde = { version = "1.0.197", features = ["derive"] }
13+
14+
[dev-dependencies]
15+
serde_json = "1.0.114"

0 commit comments

Comments
 (0)