dannycjones commented on code in PR #3032:
URL: https://github.com/apache/iceberg-rust/pull/3032#discussion_r3831846116


##########
crates/catalog/sql/src/catalog.rs:
##########
@@ -343,6 +441,52 @@ impl SqlCatalog {
         .await
         .map_err(from_sqlx_error)?;
 
+        let detected_schema_version = SchemaVersion::detect(&pool).await?;
+        let desired_schema_version = config.schema_version;
+
+        // Detect schema by describing columns. If desired schema is 
configured then automigrate, otherwise gracefully support older schemas.
+        let schema_version = match (detected_schema_version, 
desired_schema_version) {
+            (SchemaVersion::V1, Some(SchemaVersion::V1) | None) => {
+                tracing::debug!(
+                    "detected {CATALOG_TABLE_NAME} schema {} which already 
supports views",
+                    detected_schema_version,
+                );
+                SchemaVersion::V1
+            }
+            (SchemaVersion::V0, Some(desired_schema_version @ 
SchemaVersion::V1)) => {
+                tracing::warn!(
+                    "table {CATALOG_TABLE_NAME} has inferred schema {} but 
desired schema {}, performing migration",
+                    detected_schema_version,
+                    desired_schema_version,
+                );
+                if let Some(migration_sql) = SchemaVersion::V1.migration_sql() 
{
+                    sqlx::query(&migration_sql)
+                        .execute(&pool)
+                        .await
+                        .map_err(from_sqlx_error)?;
+                }
+                SchemaVersion::V1
+            }
+            (SchemaVersion::V0, Some(SchemaVersion::V0) | None) => {
+                tracing::warn!(
+                    "table {CATALOG_TABLE_NAME} has inferred schema {}; SQL 
catalog is initialized without view support, table creation, and table 
registration. \
+                    To auto-migrate the database schema, set {}=V1",
+                    detected_schema_version,
+                    SQL_CATALOG_PROP_SCHEMA_VERSION,
+                );
+                SchemaVersion::V0
+            }
+            (SchemaVersion::V1, Some(desired_schema_version @ 
SchemaVersion::V0)) => {
+                return Err(Error::new(
+                    ErrorKind::FeatureUnsupported,
+                    format!(
+                        "table {CATALOG_TABLE_NAME} has inferred schema {} but 
desired schema {}, downgrade migration is not supported",
+                        detected_schema_version, desired_schema_version,
+                    ),
+                ));
+            }

Review Comment:
   I'll move to no-op to be aligned with Java and also unsurprising when 
upgrading.



-- 
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]

Reply via email to