dannycjones commented on code in PR #2380:
URL: https://github.com/apache/iceberg-rust/pull/2380#discussion_r3339654412
##########
crates/catalog/sql/src/catalog.rs:
##########
@@ -310,13 +384,54 @@ impl SqlCatalog {
.await
.map_err(from_sqlx_error)?;
+ // Probe for the `iceberg_type` column to detect whether the catalog
table is already a schema version v1 (which supports views).
+ let is_v1 = match sqlx::query(&format!(
+ "SELECT {CATALOG_FIELD_RECORD_TYPE} FROM {CATALOG_TABLE_NAME}
LIMIT 0"
+ ))
+ .execute(&pool)
+ .await
+ {
+ Ok(_) => true,
+ // The database rejected the query: the `iceberg_type` column (or
table) is absent,
+ // so this is a genuine V0 schema.
+ Err(sqlx::Error::Database(_)) => false,
+ // Any other error (connection dropped, pool timeout,
misconfiguration, ...) is not a
+ // signal about the schema version. Surface it rather than
misclassifying as V0.
+ Err(e) => return Err(from_sqlx_error(e)),
+ };
+
+ // Migrate the schema to V1 if the catalog table does not support
views and the caller
+ // opted in via `sql.schema-version=V1`.
+ let schema_version = if is_v1 {
+ tracing::debug!("detected {CATALOG_TABLE_NAME} schema v1 which
already supports views");
+ SchemaVersion::V1
+ } else if config.schema_version == Some(SchemaVersion::V1) {
+ tracing::warn!(
+ "detected {CATALOG_TABLE_NAME} schema v0; migrating to v1 to
enable view support"
+ );
+ if let Some(migration_sql) = SchemaVersion::V1.migration_sql() {
+ sqlx::query(&migration_sql)
+ .execute(&pool)
+ .await
+ .map_err(from_sqlx_error)?;
+ }
Review Comment:
I think we should add something to this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]