Rebecca Strauch, GISP & Larry Adgate
There are a few variables at the beginning that you will need to tailor to your environment:
editDB = (your database connection)
sqlsvrname = (name of your sql server instance)
sqldbname = (name of your sql server db within your instance)
There are several other functions and cursors that will need to have my parameters replaced with your own. It should be pretty clear where those are though.
__author__ = 'wilson'
import arcpy
import sys
import os
import time
import pyodbc
import traceback
editDB ="Database Connections/DBO@CityworksGIS.sde"
Day = time.strftime("%m-%d-%Y", time.localtime())
Time = time.strftime("%I:%M:%S %p", time.localtime())
sqlsvrname = 'gis'
sqldbname = 'CityworksGIS'
workspace = editDB
arcpy.env.workspace = workspace
arcpy.env.overwriteOutput = True
try:
print 'Process Started at ' + str(Day) + " " + str(Time)
print "Blocking Connections..."
arcpy.AcceptConnections(editDB, False)
print "Disconnecting Users..."
arcpy.DisconnectUser(editDB, "ALL")
ver1List = [ver1.name for ver1 in arcpy.da.ListVersions(editDB) if ver1.name != 'DBO.QAQC' and ver1.name != 'dbo.DEFAULT']
print "Reconcile/post versions to QAQC...."
arcpy.ReconcileVersions_management(editDB, "ALL_VERSIONS", "DBO.QAQC", ver1List, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "DELETE_VERSION")
ver2List = [ver2.name for ver2 in arcpy.da.ListVersions(editDB) if ver2.name == 'DBO.QAQC']
print "Reconcile/post QAQC to DEFAULT..."
arcpy.ReconcileVersions_management(editDB, "ALL_VERSIONS", "dbo.DEFAULT", ver2List, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "DELETE_VERSION")
print("==============================")
print ("Unregister Tables Started")
print("==============================")
for tbs in [tb for tb in arcpy.ListTables() if '_log' not in tb and 'GenerateId' not in tb and 'PYTHON_SCRIPT' not in tb]:
print tbs
arcpy.UnregisterAsVersioned_management(tbs,"NO_KEEP_EDIT","COMPRESS_DEFAULT")
print("==============================")
print ("Unregister Feature Classes is started")
print("==============================")
datasets = arcpy.ListDatasets("*", "Feature")
for dataset in datasets:
fcList = arcpy.ListFeatureClasses("*","",dataset)
for fc in fcList:
print fc
arcpy.UnregisterAsVersioned_management(fc,"NO_KEEP_EDIT","COMPRESS_DEFAULT")
print "Compressing database..."
arcpy.Compress_management(editDB)
except:
print 'An error occured'
failMsg = '\nSCRIPT FAILURE IN SCRIPT INITIATION OR RECONCILE-POST PROCESS, \n'
failMsg += 'Most recent GP messages below.\n'
failMsg += arcpy.GetMessages() +'\n'
failMsg += '\nTraceback messages below.\n'
failMsg += traceback.format_exc().splitlines()[-1]
print failMsg
print 'Logging error to database....'
conn = pyodbc.connect('Trusted_Connection=yes', driver='{SQL Server}', server=sqlsvrname, database=sqldbname)
cursor = conn.cursor()
cursor.execute("""INSERT INTO [dbo].[PYTHON_SCRIPT_TRACKER] (ScriptName,RunDate,Status,ErrorCode) VALUES ('ReconcilePost_AssetIDs_QAQC',GetDate(),'Failure',(?)); """,failMsg)
conn.commit()
conn.close()
sys.exit()
try:
print("==============================")
print ("register tables is started")
print("==============================")
for tbs in [tb for tb in arcpy.ListTables() if '_log' not in tb and 'GenerateId' not in tb and 'PYTHON_SCRIPT' not in tb]:
print tbs
arcpy.RegisterAsVersioned_management(tbs, "NO_EDITS_TO_BASE")
print("==============================")
print ("register Feature Class is started")
print("==============================")
datasets = arcpy.ListDatasets("*", "Feature")
for dataset in datasets:
fcList = arcpy.ListFeatureClasses("*","",dataset)
for fc in fcList:
print fc
arcpy.RegisterAsVersioned_management(fc, "NO_EDITS_TO_BASE")
print "Creating Versions..."
arcpy.CreateVersion_management(editDB, 'dbo.DEFAULT', 'QAQC', 'PRIVATE')
arcpy.CreateVersion_management(editDB, 'DBO.QAQC', 'MICHAEL', 'PUBLIC')
arcpy.CreateVersion_management(editDB, 'DBO.QAQC', 'KAREN', 'PUBLIC')
arcpy.CreateVersion_management(editDB, 'DBO.QAQC', 'COLLECTOR', 'PUBLIC')
dataList = arcpy.ListTables() + arcpy.ListFeatureClasses() + arcpy.ListRasters()
for dataset in arcpy.ListDatasets("*", "Feature"):
arcpy.env.workspace = os.path.join(workspace, dataset)
dataList += arcpy.ListFeatureClasses() + arcpy.ListDatasets()
arcpy.env.workspace = workspace
datasetList = [ds for ds in dataList]
print "rebuilding indexes"
arcpy.RebuildIndexes_management(workspace, "SYSTEM", datasetList, "ALL")
print('Rebuild Complete')
print "analyzing datasets"
arcpy.AnalyzeDatasets_management(workspace, "SYSTEM", datasetList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")
print "analysis complete"
print "Set databases to allow connections..."
arcpy.AcceptConnections(editDB, True)
print 'Data has been synched succesfully and logged in the tracking database'
conn = pyodbc.connect('Trusted_Connection=yes', driver='{SQL Server}', server=sqlsvrname, database=sqldbname)
cursor = conn.cursor()
cursor.execute("""INSERT INTO [dbo].[PYTHON_SCRIPT_TRACKER] (ScriptName,RunDate,Status,ErrorCode) VALUES ('ReconcilePost_AssetIDs_QAQC',GetDate(),'Success','All Steps Completed Without Error'); """)
conn.commit()
del cursor
conn.close()
print 'Script is finished'
except:
print 'An error occured'
failMsg = '\nSCRIPT FAILURE IN VERSIONING PROCESS\n'
failMsg += 'Most recent GP messages below.\n'
failMsg += arcpy.GetMessages() +'\n'
failMsg += '\nTraceback messages below.\n'
failMsg += traceback.format_exc().splitlines()[-1]
print failMsg
print 'Logging error to database....'
conn = pyodbc.connect('Trusted_Connection=yes', driver='{SQL Server}', server=sqlsvrname, database=sqldbname)
cursor = conn.cursor()
cursor.execute("""INSERT INTO [dbo].[PYTHON_SCRIPT_TRACKER] (ScriptName,RunDate,Status,ErrorCode) VALUES ('ReconcilePost_AssetIDs_QAQC',GetDate(),'Failure',(?)); """,failMsg)
conn.commit()
conn.close()
sys.exit()
Feel free to let me know if you have any questions or find room for improvement!
My email is under the author portion of the script if you need it.