Recreate Replicas Using ArcGIS Pro

11-16-2022 10:11 AM
Labels (2)
New Contributor II

I created a python script to recreate existing replicas (or create new ones) using geoprocessing tools in ArcGIS Pro and I thought I would share to the community.  This script will not run in ArcMap since the "UnregisterReplica_management" tool is new to ArcGIS Pro.  This is very useful after you add new feature classes to a parent replica feature dataset.

#description     :Creates new replicas or recreates existing replicas using geoprocessing tools available in ArcGIS Pro 3
#description     :The script is setup to work with enterprise geodatabases in SQL Server
#description     :Works for "SIMPLE", "One Way Replicas" only and is configured to operate at the individual feature dataset level
#description     :The data in the child geodatabase needs to be free of schema locks (putting the SQL database in Restricted user mode before running the script will remove schema locks)
#author          :Bill Myers
#last_update     :11/15/2022
#notes           :Runs in ArcGIS Pro 3.0.2 
#python_version  :3
import sys, arcpy
runPath = sys.path[0]
dataSchema = "GIS"
dataName = "Boundaries" #Feature dataset and replica base name.
parentGDB = "SdeDev"
childGDB = "SdeTemp"

#Feature datasets
parentFDName = "{0}.{1}.{2}".format(parentGDB, dataSchema.upper(), dataName)  #"SdeDev.GIS.Boundaries"
childFDName = "{0}.{1}.{2}".format(childGDB, dataSchema.upper(), dataName)  #"SdeTemp.GIS.Boundaries"

#SDE connection file (should be already setup in ArcGIS Pro)
parentGDBConn = "{0}\\cosgissql_{1}_{2}.sde".format(runPath, parentGDB, dataSchema)  #"cosgissql_SdeDev_GIS.sde"
childGDBConn = "{0}\\cosgissql_{1}_{2}.sde".format(runPath, childGDB, dataSchema)  #"cosgissql_SdeTemp_GIS.sde"

replicaName = "Replica_{0}_{1}".format(dataSchema, dataName)  #"Replica_GIS_Boundaries"
replicaNameFull = "{0}.{1}".format(dataSchema.upper(), replicaName)  #"GIS.Replica_GIS_Boundaries"
parentReplicaExists = True
childReplicaExists = True
parentReplicaDatasets = []
childReplicaDatasets = []
childReplicaTables = []

#Replica params
expand = "DO_NOT_ADD"
getRelated = "DO_NOT_GET_RELATED"
#expand = "ALL_ROWS"
#getRelated = "GET_RELATED"

arcpy.env.overwriteOutput = True
arcpy.env.workspace = childGDBConn

def replicaExists(gdb, name):
    exists = False
    for replica in arcpy.da.ListReplicas(gdb):
        if == name.upper():
            exists = True
    return exists
#Find if replicas exist
childReplicaExists = replicaExists(childGDBConn, replicaNameFull)
parentReplicaExists = replicaExists(parentGDBConn, replicaNameFull)

#Get parent datasets to replicate
arcpy.env.workspace = parentGDBConn + "\\" + parentFDName
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
    fullPath = parentGDBConn + "\\" + fc
if childReplicaExists and parentReplicaExists:
    arcpy.AddMessage("Removing existsing replicas: " + replicaNameFull)
    #Get child replica datasets
    for replica in arcpy.da.ListReplicas(childGDBConn):
        if == replicaNameFull:
            childReplicaDatasets = replica.datasets
    #get child tables
    for dataset in childReplicaDatasets:
        desc = arcpy.Describe(childGDBConn + "\\" + dataset)
        if hasattr(desc, "dataType") and desc.dataType == "Table":
            childReplicaTables.append(childGDBConn + "\\" + dataset)
    #reset params to handle related tables
    if len(childReplicaTables) > 0:
        expand = "ALL_ROWS"
        getRelated = "GET_RELATED"
    arcpy.AddMessage("...Unregistering Replica")
    arcpy.UnregisterReplica_management(parentGDBConn, replicaNameFull)
    arcpy.UnregisterReplica_management(childGDBConn, replicaNameFull)
    #Delete child tables
    if len(childReplicaTables) > 0:
        arcpy.AddMessage("...Deleting child tables")        
    #Delete child feature dataset
    arcpy.AddMessage("...Deleting child feature dataset")
    dataPath = childGDBConn + "\\" + childFDName

#Create Replica
arcpy.AddMessage("Creating Replica: " + replicaNameFull), "ONE_WAY_REPLICA", childGDBConn, replicaName, "SIMPLE", "PARENT_DATA_SENDER", expand, "DO_NOT_REUSE", getRelated)


William H Myers
Tags (2)
0 Kudos
0 Replies