| name | databricks-upgrade-migration |
| description | Upgrade Databricks runtime versions and migrate between features.
Use when upgrading DBR versions, migrating to Unity Catalog,
or updating deprecated APIs and features.
Trigger with phrases like "databricks upgrade", "DBR upgrade",
"databricks migration", "unity catalog migration", "hive to unity".
|
| allowed-tools | Read, Write, Edit, Bash(databricks:*), Grep |
| version | 1.0.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
Databricks Upgrade & Migration
Overview
Upgrade Databricks Runtime versions and migrate between platform features.
Prerequisites
- Admin access to workspace
- Test environment for validation
- Understanding of current workload dependencies
Instructions
Step 1: Runtime Version Upgrade
Version Compatibility Matrix
| Current DBR | Target DBR | Breaking Changes | Migration Effort |
|---|
| 12.x | 13.x | Spark 3.4 changes | Low |
| 13.x | 14.x | Python 3.10 default | Medium |
| 14.x | 15.x | Unity Catalog required | High |
Upgrade Process
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.compute import ClusterSpec
def upgrade_cluster_dbr(
w: WorkspaceClient,
cluster_id: str,
target_version: str = "14.3.x-scala2.12",
dry_run: bool = True,
) -> dict:
"""
Upgrade cluster to new DBR version.
Args:
w: WorkspaceClient
cluster_id: Cluster to upgrade
target_version: Target Spark version
dry_run: If True, only validate without applying
Returns:
Upgrade plan or result
"""
cluster = w.clusters.get(cluster_id)
upgrade_plan = {
"cluster_id": cluster_id,
"cluster_name": cluster.cluster_name,
"current_version": cluster.spark_version,
"target_version": target_version,
"changes": [],
}
if cluster.spark_conf:
deprecated_configs = [
"spark.databricks.delta.preview.enabled",
"spark.sql.legacy.createHiveTableByDefault",
]
for config in deprecated_configs:
if config in cluster.spark_conf:
upgrade_plan["changes"].append({
"type": "remove_config",
"config": config,
"reason": "Deprecated in target version",
})
if cluster.cluster_libraries:
for lib cluster.cluster_libraries:
dry_run:
w.clusters.edit(
cluster_id=cluster_id,
spark_version=target_version,
spark_conf={
k: v k, v (cluster.spark_conf {}).items()
k deprecated_configs
}
)
upgrade_plan[] =
:
upgrade_plan[] =
upgrade_plan
Step 2: Unity Catalog Migration
Migration Steps
CREATE CATALOG IF NOT EXISTS main;
CREATE SCHEMA IF NOT EXISTS main.migrated;
SYNC SCHEMA main.migrated
FROM hive_metastore.old_schema;
CREATE TABLE main.migrated.customers AS
SELECT * FROM hive_metastore.old_schema.customers;
CREATE VIEW main.migrated.customer_summary AS
SELECT * FROM hive_metastore.old_schema.customer_summary;
GRANT USAGE ON CATALOG main TO `data-team`;
GRANT SELECT ON SCHEMA main.migrated TO `data-team`;
SHOW TABLES IN main.migrated;
DESCRIBE TABLE EXTENDED main.migrated.customers;
Python Migration Script
from databricks.sdk import WorkspaceClient
from pyspark.sql import SparkSession
def migrate_schema_to_unity(
spark: SparkSession,
source_schema: str,
target_catalog: str,
target_schema: str,
tables: list[str] = None,
method: str = "sync",
) -> list[dict]:
"""
Migrate Hive Metastore schema to Unity Catalog.
Args:
spark: SparkSession
source_schema: Hive metastore schema (e.g., "hive_metastore.old_db")
target_catalog: Unity Catalog catalog name
target_schema: Target schema name
tables: Specific tables to migrate (None = all)
method: "sync" (in-place) or "copy" (duplicate data)
Returns:
List of migration results
"""
results = []
if tables is None:
tables_df = spark.sql(f"SHOW TABLES IN {source_schema}")
tables = [row.tableName for row in tables_df.collect()]
spark.sql(f"CREATE SCHEMA IF NOT EXISTS {target_catalog}.{target_schema}")
for table in tables:
source_table = f"{source_schema}.{table}"
target_table = f"{target_catalog}.{target_schema}.{table}"
:
method == :
spark.sql()
:
spark.sql()
results.append({
: table,
: ,
: method,
})
Exception e:
results.append({
: table,
: ,
: (e),
})
results
Step 3: API Migration (v2.0 to v2.1)
from databricks.sdk import WorkspaceClient
def migrate_api_calls(w: WorkspaceClient):
"""Update deprecated API usage patterns."""
pass
Step 4: Delta Lake Upgrade
def upgrade_delta_tables(
spark: SparkSession,
catalog: str,
schema: str,
min_reader: int = 3,
min_writer: int = 7,
) -> list[dict]:
"""
Upgrade Delta Lake protocol for tables.
Protocol version benefits:
- Reader 2+: Column mapping
- Reader 3+: Deletion vectors
- Writer 5+: Change Data Feed
- Writer 7+: Deletion vectors, liquid clustering
"""
results = []
tables = spark.sql(f"SHOW TABLES IN {catalog}.{schema}").collect()
for table_row in tables:
table = f"{catalog}.{schema}.{table_row.tableName}"
try:
detail = spark.sql(f"DESCRIBE DETAIL {table}").first()
current_reader = detail.minReaderVersion
current_writer = detail.minWriterVersion
if current_reader < min_reader or current_writer < min_writer:
spark.sql(f"""
ALTER TABLE {table}
SET TBLPROPERTIES (
'delta.minReaderVersion' = '{min_reader}',
'delta.minWriterVersion' = '{min_writer}'
)
""")
results.append({
"table": table,
"status": "UPGRADED",
"from": f"r/w",
: ,
})
:
results.append({
: table,
: ,
})
Exception e:
results.append({
: table,
: ,
: (e),
})
results
Output
- Upgraded DBR version
- Unity Catalog migration complete
- Updated API calls
- Delta Lake protocol upgraded
Error Handling
| Issue | Cause | Solution |
|---|
| Incompatible library | Version mismatch | Update library version |
| Permission error | Missing grants | Add Unity Catalog grants |
| Table sync failed | Location access | Check storage permissions |
| Protocol downgrade | Reader/writer too high | Clone to new table |
Examples
Complete Migration Runbook
#!/bin/bash
echo "Creating backup..."
databricks workspace export-dir /production /tmp/backup --overwrite
echo "Testing on staging..."
databricks bundle deploy -t staging
databricks bundle run -t staging migration-test-job
echo "Running migration..."
python scripts/migrate_to_unity_catalog.py
echo "Validating..."
databricks bundle run -t staging validation-job
echo "Updating jobs..."
databricks bundle deploy -t prod
echo "Migration complete!"
Resources
Next Steps
For CI/CD integration, see databricks-ci-integration.