badalprasadsingh commented on code in PR #1244:
URL: https://github.com/apache/iceberg-go/pull/1244#discussion_r3530930162
##########
catalog/sql/sql.go:
##########
@@ -247,12 +286,141 @@ func (c *Catalog) DropSQLTables(ctx context.Context)
error {
}
func (c *Catalog) ensureTablesExist() error {
- return c.CreateSQLTables(context.Background())
+ ctx := context.Background()
+ if err := c.CreateSQLTables(ctx); err != nil {
+ return err
+ }
+
+ return c.migrateV0Schema(ctx)
+}
+
+func (c *Catalog) detectSchemaVersion(ctx context.Context) error {
+ hasCol, err := c.icebergTypeColumnExists(ctx)
+ if err != nil {
+ return fmt.Errorf("detecting catalog schema version: %w", err)
+ }
+
+ if hasCol {
+ c.schemaVersion = schemaV1
+ } else {
+ c.schemaVersion = schemaV0
+ }
+
+ return nil
+}
+
+func (c *Catalog) migrateV0Schema(ctx context.Context) error {
+ hasCol, err := c.icebergTypeColumnExists(ctx)
+ if err != nil {
+ return fmt.Errorf("V0 schema migration: column probe failed:
%w", err)
+ }
+ if hasCol {
+ c.schemaVersion = schemaV1
+
+ return nil
+ }
+
+ if !strings.EqualFold(c.props.Get(SchemaVersionKey, ""),
SchemaVersionV1) {
+ log.Printf("WARNING: iceberg_tables is on the legacy V0 schema
(no iceberg_type column); "+
+ "set %q=%q to migrate to V1. Operating in V0-compatible
mode (column-free queries).",
+ SchemaVersionKey, SchemaVersionV1)
+ c.schemaVersion = schemaV0
+
+ return nil
+ }
+
+ ddl, err := addIcebergTypeColumnDDL(c.db.Dialect().Name())
+ if err != nil {
+ return err
+ }
+
+ if _, err := c.db.ExecContext(ctx, ddl); err != nil {
+ has, reprobeErr := c.icebergTypeColumnExists(ctx)
+ if has {
+ c.schemaVersion = schemaV1
+
+ return nil
+ }
+ if reprobeErr != nil {
+ return fmt.Errorf("migration of V0 schema failed: %w
(re-probe also failed: %v)", err, reprobeErr)
+ }
+
+ return fmt.Errorf("migration of V0 schema failed: %w", err)
+ }
+
+ c.schemaVersion = schemaV1
+
+ return nil
+}
+
+func addIcebergTypeColumnDDL(name dialect.Name) (string, error) {
+ switch name {
+ case dialect.PG, dialect.SQLite:
+ return "ALTER TABLE iceberg_tables ADD COLUMN iceberg_type
VARCHAR", nil
Review Comment:
Well,
The point is the SQL catalogs which are already created in the past by
Iceberg-Go, would still have the current mix of `VARCHAR` as do not set
`VARCHAR(5)` during initial `V1` schema catalog creation. Happy to open a new
PR for this change across everywhere to match Java's `VARCHAR(5)`.
--
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]