@@ -390,31 +390,49 @@ def drop_schema(
390390 """
391391 logger .debug (f"drop_schema called with: { schema_name } (type: { type (schema_name )} )" )
392392
393- # Parse schema_name into an exp.Table to properly handle both string and Table cases
394- table = exp .to_table (schema_name )
395-
396- if table .catalog :
397- # 3-part name detected (catalog.db.table) - this shouldn't happen for schema operations
398- raise SQLMeshError (
399- f"Invalid schema name format: { schema_name } . Expected 'schema' or 'catalog.schema'"
400- )
401- elif table .db :
402- # Catalog-qualified schema: catalog.schema
403- catalog_name = table .db
404- schema_only = table .name
405- logger .debug (
406- f"Detected catalog.schema format: catalog='{ catalog_name } ', schema='{ schema_only } '"
407- )
408-
409- # Switch to the catalog first
410- self .set_current_catalog (catalog_name )
411-
412- # Use just the schema name
413- super ().drop_schema (schema_only , ignore_if_not_exists , cascade , ** drop_args )
393+ # Handle Table objects created by schema_() function
394+ if isinstance (schema_name , exp .Table ) and not schema_name .name :
395+ # This is a schema Table object - check for catalog qualification
396+ if schema_name .catalog :
397+ # Catalog-qualified schema: catalog.schema
398+ catalog_name = schema_name .catalog
399+ schema_only = schema_name .db
400+ logger .debug (
401+ f"Detected catalog-qualified schema: catalog='{ catalog_name } ', schema='{ schema_only } '"
402+ )
403+ # Switch to the catalog first
404+ self .set_current_catalog (catalog_name )
405+ # Use just the schema name
406+ super ().drop_schema (schema_only , ignore_if_not_exists , cascade , ** drop_args )
407+ else :
408+ # Schema only, no catalog
409+ schema_only = schema_name .db
410+ logger .debug (f"Detected schema-only: schema='{ schema_only } '" )
411+ super ().drop_schema (schema_only , ignore_if_not_exists , cascade , ** drop_args )
414412 else :
415- # No catalog qualification, use as-is
416- logger .debug (f"No catalog detected, using original: { schema_name } " )
417- super ().drop_schema (schema_name , ignore_if_not_exists , cascade , ** drop_args )
413+ # Handle string or table name inputs by parsing as table
414+ table = exp .to_table (schema_name )
415+
416+ if table .catalog :
417+ # 3-part name detected (catalog.db.table) - this shouldn't happen for schema operations
418+ raise SQLMeshError (
419+ f"Invalid schema name format: { schema_name } . Expected 'schema' or 'catalog.schema', got 3-part name"
420+ )
421+ elif table .db :
422+ # Catalog-qualified schema: catalog.schema
423+ catalog_name = table .db
424+ schema_only = table .name
425+ logger .debug (
426+ f"Detected catalog.schema format: catalog='{ catalog_name } ', schema='{ schema_only } '"
427+ )
428+ # Switch to the catalog first
429+ self .set_current_catalog (catalog_name )
430+ # Use just the schema name
431+ super ().drop_schema (schema_only , ignore_if_not_exists , cascade , ** drop_args )
432+ else :
433+ # No catalog qualification, use as-is
434+ logger .debug (f"No catalog detected, using original: { schema_name } " )
435+ super ().drop_schema (schema_name , ignore_if_not_exists , cascade , ** drop_args )
418436
419437 def create_schema (
420438 self ,
@@ -428,31 +446,49 @@ def create_schema(
428446 """
429447 logger .debug (f"create_schema called with: { schema_name } (type: { type (schema_name )} )" )
430448
431- # Parse schema_name into an exp.Table to properly handle both string and Table cases
432- table = exp .to_table (schema_name )
433-
434- if table .catalog :
435- # 3-part name detected (catalog.db.table) - this shouldn't happen for schema operations
436- raise SQLMeshError (
437- f"Invalid schema name format: { schema_name } . Expected 'schema' or 'catalog.schema'"
438- )
439- elif table .db :
440- # Catalog-qualified schema: catalog.schema
441- catalog_name = table .db
442- schema_only = table .name
443- logger .debug (
444- f"Detected catalog.schema format: catalog='{ catalog_name } ', schema='{ schema_only } '"
445- )
446-
447- # Switch to the catalog first
448- self .set_current_catalog (catalog_name )
449-
450- # Use just the schema name
451- super ().create_schema (schema_only , ignore_if_exists , ** kwargs )
449+ # Handle Table objects created by schema_() function
450+ if isinstance (schema_name , exp .Table ) and not schema_name .name :
451+ # This is a schema Table object - check for catalog qualification
452+ if schema_name .catalog :
453+ # Catalog-qualified schema: catalog.schema
454+ catalog_name = schema_name .catalog
455+ schema_only = schema_name .db
456+ logger .debug (
457+ f"Detected catalog-qualified schema: catalog='{ catalog_name } ', schema='{ schema_only } '"
458+ )
459+ # Switch to the catalog first
460+ self .set_current_catalog (catalog_name )
461+ # Use just the schema name
462+ super ().create_schema (schema_only , ignore_if_exists , ** kwargs )
463+ else :
464+ # Schema only, no catalog
465+ schema_only = schema_name .db
466+ logger .debug (f"Detected schema-only: schema='{ schema_only } '" )
467+ super ().create_schema (schema_only , ignore_if_exists , ** kwargs )
452468 else :
453- # No catalog qualification, use as-is
454- logger .debug (f"No catalog detected, using original: { schema_name } " )
455- super ().create_schema (schema_name , ignore_if_exists , ** kwargs )
469+ # Handle string or table name inputs by parsing as table
470+ table = exp .to_table (schema_name )
471+
472+ if table .catalog :
473+ # 3-part name detected (catalog.db.table) - this shouldn't happen for schema operations
474+ raise SQLMeshError (
475+ f"Invalid schema name format: { schema_name } . Expected 'schema' or 'catalog.schema', got 3-part name"
476+ )
477+ elif table .db :
478+ # Catalog-qualified schema: catalog.schema
479+ catalog_name = table .db
480+ schema_only = table .name
481+ logger .debug (
482+ f"Detected catalog.schema format: catalog='{ catalog_name } ', schema='{ schema_only } '"
483+ )
484+ # Switch to the catalog first
485+ self .set_current_catalog (catalog_name )
486+ # Use just the schema name
487+ super ().create_schema (schema_only , ignore_if_exists , ** kwargs )
488+ else :
489+ # No catalog qualification, use as-is
490+ logger .debug (f"No catalog detected, using original: { schema_name } " )
491+ super ().create_schema (schema_name , ignore_if_exists , ** kwargs )
456492
457493 def _ensure_schema_exists (self , table_name : TableName ) -> None :
458494 """
0 commit comments