English

How To: Automate reconcile, post and compress processes

Summary

Reconciling and posting versions in a multi-tiered versioning workflow such as: child to parent to default followed by compress is a typical workflow performed by GIS administrators or managers.

Procedure

The Python script below provides a method for automating the reconcile, post, and compress workflow. The workflow assumes three tiers (Default > Parent > Child), such that child versions are reconciled to parent version and parent versions are reconciled to default.

Note:
The script also compresses the geodatabase to state 0, deletes all versions except DEFAULT and re-creates the child versions.

  1. Modify the vtree variable in the script according to the version tree.

    For example, populate parent and child versions in the following manner:'Parent':'Child', etc. Do not list DEFAULT.

    In the example below, the version named SDE.Child is a child of SDE.Parent. The version named SDE.Edit is a child of SDE.QA.

    Example:

    vTree = {'SDE.Parent':'SDE.Child','SDE.QA':'SDE.Edit'}

  2. Set the workspace variable to reference the path to the sde connection file. Be sure the sde connection file user is the SDE Administrator:

    Example:

    workspace = r"Database Connections\MXD2.sde"

  3. Set the defaultVersion variable to reference either the dbo.DEFAULT or sde.DEFAULT in accordance to the environment.

    Example:

    defaultVersion = "sde.DEFAULT"

  4. Finally, set the logging variables according to your preference, that is, the naming convention and location of the logs.

    Example:

    logName = "RecPostLog.txt"
    logName2 = "RecPostDefaultLog.txt"
    logName3 = "CompressLog.txt"
    logWorkspace = r"C:\temp"


  5. Code:
    #Reconcile and posting versions at 10.0

    import arcpy, os, sys, string

    #Populate parent and child versions in the following manner('Parent':'Child', etc). DO NOT LIST DEFAULT
    vTree = {'SDE.Parent':'SDE.Child','SDE.QA':'SDE.Edit'}

    #Reconcile and post child versions with parent
    def RecPostNonDefault(workspace,logWorkspace,logName):
    outLog = open(os.path.join(logWorkspace, logName), 'w')
    for key, val in vTree.iteritems():
    arcpy.ReconcileVersion_management(workspace, val, key,"BY_OBJECT", "FAVOR_TARGET_VERSION", "NO_LOCK_AQUIRED", "NO_ABORT", "POST")
    print "Reconciling and posting {0} to {1}".format(val, key)
    outLog.write("Reconciling and posting {0} to {1}".format(val, key))
    outLog.write("\n")
    outLog.close()
    del outLog, key, val

    #Reconcile and post with parent
    def RecPostDefault(workspace,logWorkspace,logName2,defaultVersion):
    outLog = open(os.path.join(logWorkspace, logName2), 'w')
    #Reconcile and post parents with DEFAULT
    for key, val in vTree.iteritems():
    arcpy.ReconcileVersion_management(workspace, key, defaultVersion,"BY_OBJECT", "FAVOR_TARGET_VERSION", "NO_LOCK_AQUIRED", "NO_ABORT", "POST")
    print "Reconciling and posting {0} to DEFAULT".format(key)
    outLog.write("Reconciling and posting {0} to DEFAULT".format(key))
    outLog.write("\n")
    outLog.close()
    del outLog, key, val

    def DeleteChildVersions(workspace):
    arcpy.ClearWorkspaceCache_management()
    for key, val in vTree.iteritems():
    arcpy.DeleteVersion_management(workspace, val)
    print "Deleted {0}".format(val)


    def DeleteParentVersions(workspace):
    arcpy.ClearWorkspaceCache_management()
    for key, val in vTree.iteritems():
    arcpy.DeleteVersion_management(workspace, key)
    print "Deleted {0}".format(key)

    #Compress database
    def Compress(workspace,logWorkspace,logName3):
    arcpy.ClearWorkspaceCache_management()
    outLog = open(os.path.join(logWorkspace, logName3), 'w')
    arcpy.Compress_management(workspace)
    print ("Compressed database {0}".format(workspace))
    outLog.write("Compressed database {0}".format(workspace))
    outLog.close()

    def RecreateVersions(workspace, defaultVersion):
    for key, val in vTree.iteritems():
    arcpy.CreateVersion_management(workspace,defaultVersion, key[4:], "PUBLIC")
    print "Created version {0}".format(key)
    arcpy.CreateVersion_management(workspace, key, val[4:], "PUBLIC")
    print "Created version {0}".format(val)

    if __name__=="__main__":

    workspace = r"Database Connections\MXD2.sde"
    defaultVersion = "sde.DEFAULT"
    logName = "RecPostLog.txt"
    logName2 = "RecPostDefaultLog.txt"
    logName3 = "CompressLog.txt"
    logWorkspace = r"C:\temp"
    RecPostNonDefault(workspace,logWorkspace,logName)
    RecPostDefault(workspace,logWorkspace,logName2,defaultVersion)
    DeleteChildVersions(workspace)
    DeleteParentVersions(workspace)
    Compress(workspace,logWorkspace,logName3)
    RecreateVersions(workspace, defaultVersion)