This is the original code with debug output to show exactly what is being used as source and replace. This is what happens running inside Pro. I have moved the print output inline to help, also shown at bottom.
- The first print shows the connectionProperties on the layer before running any code.
- The next shows the same thing but as variable used in the updateConnectionProperties method
- The next shows the replace variable. Unless I am not seeing something this is replacing every parameter just using the new settings.
- The database is replaced with the new database name
- The dataset is replaced with the new fully qualified name of the dataset
- The final shows the layer.connectionProperties immediately after the replace operation
- I did run with the 'workspace_factory' property but included but accidentally copied this output and too much effort to redo the entire code shot below
After that I break out to simplify and only show the single layer
aprx = arcpy.mp.ArcGISProject("CURRENT")
db_source = 'Staging'
db_target = 'fort_collins'
for map in aprx.listMaps():
print(map.name)
for layer in map.listLayers():
if layer.connectionProperties == None:
continue
print(layer.name)
pprint.pprint(layer.connectionProperties)
{'connection_info': {'authentication_mode': 'DBMS',
'database': 'Staging',
'db_connection_properties': 'portal07',
'dbclient': 'sqlserver',
'instance': 'sde:sqlserver:portal07',
'password': '<*********>',
'server': 'portal07',
'user': 'gis',
'version': 'sde.DEFAULT'},
'dataset': 'Staging.GIS.PipelineAssembly',
'workspace_factory': 'SDE'}
source_dataset = str(layer.connectionProperties['dataset'])
target_dataset = str(source_dataset).replace(db_source, db_target)
source_info = layer.connectionProperties['connection_info']
target_info = dict(source_info)
target_info['database'] = db_target
target_info['user'] = 'gis'
target_info['password'] = 'gis'
source_properties = {'connection_info': source_info, 'dataset': source_dataset}
target_properties = {'connection_info': target_info, 'dataset': target_dataset}
pprint.pprint(source_properties)
{'connection_info': {'authentication_mode': 'DBMS',
'database': 'Staging',
'db_connection_properties': 'portal07',
'dbclient': 'sqlserver',
'instance': 'sde:sqlserver:portal07',
'password': '<*********>',
'server': 'portal07',
'user': 'gis',
'version': 'sde.DEFAULT'},
'dataset': 'Staging.GIS.PipelineAssembly'}
pprint.pprint(target_properties)
{'connection_info': {'authentication_mode': 'DBMS',
'database': 'fort_collins',
'db_connection_properties': 'portal07',
'dbclient': 'sqlserver',
'instance': 'sde:sqlserver:portal07',
'password': 'gis',
'server': 'portal07',
'user': 'gis',
'version': 'sde.DEFAULT'},
'dataset': 'fort_collins.GIS.PipelineAssembly'}
layer.updateConnectionProperties(source_properties, target_properties)
pprint.pprint(layer.connectionProperties)
{'connection_info': {'authentication_mode': 'DBMS',
'database': 'Staging',
'db_connection_properties': 'portal07',
'dbclient': 'sqlserver',
'instance': 'sde:sqlserver:portal07',
'password': '<*********>',
'server': 'portal07',
'user': 'gis',
'version': 'sde.DEFAULT'},
'dataset': 'Staging.GIS.PipelineAssembly',
'workspace_factory': 'SDE'}
break
break
Map
Pipeline Assembly
{'connection_info': {'authentication_mode': 'DBMS',
'database': 'Staging',
'db_connection_properties': 'portal07',
'dbclient': 'sqlserver',
'instance': 'sde:sqlserver:portal07',
'password': '<*********>',
'server': 'portal07',
'user': 'gis',
'version': 'sde.DEFAULT'},
'dataset': 'Staging.GIS.PipelineAssembly',
'workspace_factory': 'SDE'}
{'connection_info': {'authentication_mode': 'DBMS',
'database': 'Staging',
'db_connection_properties': 'portal07',
'dbclient': 'sqlserver',
'instance': 'sde:sqlserver:portal07',
'password': '<*********>',
'server': 'portal07',
'user': 'gis',
'version': 'sde.DEFAULT'},
'dataset': 'Staging.GIS.PipelineAssembly'}
{'connection_info': {'authentication_mode': 'DBMS',
'database': 'fort_collins',
'db_connection_properties': 'portal07',
'dbclient': 'sqlserver',
'instance': 'sde:sqlserver:portal07',
'password': 'gis',
'server': 'portal07',
'user': 'gis',
'version': 'sde.DEFAULT'},
'dataset': 'fort_collins.GIS.PipelineAssembly'}
{'connection_info': {'authentication_mode': 'DBMS',
'database': 'Staging',
'db_connection_properties': 'portal07',
'dbclient': 'sqlserver',
'instance': 'sde:sqlserver:portal07',
'password': '<*********>',
'server': 'portal07',
'user': 'gis',
'version': 'sde.DEFAULT'},
'dataset': 'Staging.GIS.PipelineAssembly',
'workspace_factory': 'SDE'}
Thanks,
-Joe