Recreate Replicas Using ArcGIS Pro

116
0
11-16-2022 10:11 AM
Labels (2)
BillMyers
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.

#----------------------------------------------------------------------
#title           :RecreateReplica.py
#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
#----------------------------------------------------------------------
#Setup
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"

#Replicas
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"

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

#----------------------------------------------------------------------
def replicaExists(gdb, name):
    exists = False
    for replica in arcpy.da.ListReplicas(gdb):
        if replica.name.upper() == name.upper():
            exists = True
            break
    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
    parentReplicaDatasets.append(fullPath)
#arcpy.AddMessage(parentReplicaDatasets)
            
if childReplicaExists and parentReplicaExists:
    arcpy.AddMessage("Removing existsing replicas: " + replicaNameFull)
    #Get child replica datasets
    for replica in arcpy.da.ListReplicas(childGDBConn):
        if replica.name == replicaNameFull:
            childReplicaDatasets = replica.datasets
            #arcpy.AddMessage(childReplicaDatasets)
            
    #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"
        
    #Unregister
    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")        
        arcpy.Delete_management(childReplicaTables)
    
    #Delete child feature dataset
    arcpy.AddMessage("...Deleting child feature dataset")
    dataPath = childGDBConn + "\\" + childFDName
    arcpy.Delete_management(dataPath)

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

 

William H Myers
Tags (2)
0 Kudos
0 Replies