AnsweredAssumed Answered

ERROR 000301: The workspace is of the wrong type

Question asked by LarryAdgate on Jul 6, 2017
Latest reply on Jul 19, 2017 by jhead@lakeworth

This script is far from perfect, but I'm getting a very strange error message and I was hopeful someone could help me solve the issue. Thank You for your time: 

ERROR 000301: The workspace is of the wrong type


import arcpy
import sys
import os
import time, datetime
#import pyodbc
import traceback

# \\\\Constant Vars\\\\
# Database Connection
editDB ="Database Connections/EdnaRoad_Admin.sde"
# Current Day
Day = time.strftime("%m-%d-%Y", time.localtime())
# Current Time
Time = time.strftime("%I:%M:%S %p", time.localtime())


# \\\\pyodbc.connect vars\\\\
#sqlsvrname = 'gisavglis'
#sqldbname = 'Arden'
loggingTableName = "dbo.PYTHON_SCRIPT_LOGGING"

# Set workspace
workspace = editDB

# Set the workspace environment
arcpy.env.workspace = workspace
arcpy.env.overwriteOutput = True

def log_messages(scriptName,Status, ErrorCode):
    runDate = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
    egdb_conn = arcpy.ArcSDESQLExecute(workspace)
    sql = "INSERT INTO {4} (ScriptName,RunDate,Status,ErrorCode) VALUES ('{0}','{1}','{2}','{3}')".format(scriptName, runDate, Status, ErrorCode, loggingTableName)
    try:
        egdb_conn.startTransaction()
        print("Attempt to execute SQL Statement: {0}".format(sql))
        egdb_return = egdb_conn.execute(sql)
        egdb_conn.commitTransaction()
    except Exception as err:
        print(err)
        egdb_return = False

try:
    # Start Time
    print 'Process Started at ' + str(Day) + " " + str(Time)

    # block new connections to the working and prod database.
    print "Blocking Connections..."
    arcpy.AcceptConnections(editDB, False)

    # disconnect all users from the working and prod database.
    print "Disconnecting Users..."
    arcpy.DisconnectUser(editDB, "ALL")

    # Get a list of all versions besides QAQC and DEFAULT to pass into the ReconcileVersions tool.
    ver1List = [ver1.name for ver1 in arcpy.da.ListVersions(editDB) if ver1.name != 'DBO.Quality Control' and ver1.name != 'dbo.DEFAULT']

    # Execute the ReconcileVersions tool with QAQC Target Version then delete all versions besides QAQC
    print "Reconcile/post versions to Quality Control...."
    arcpy.ReconcileVersions_management(editDB, "ALL_VERSIONS", "DBO.Quality Control", ver1List, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_EDIT_VERSION", "POST", "")

    # Extract QAQC version from the list of versions to pass to ReconcileVersions tool.
    ver2List = [ver2.name for ver2 in arcpy.da.ListVersions(editDB) if ver2.name == 'DBO.Quality Control']

    # Execute the ReconcileVersions tool with DEFAULT Target Version and delete QAQC version
    print "Reconcile/post Quality Control to DEFAULT..."
    arcpy.ReconcileVersions_management(editDB, "ALL_VERSIONS", "dbo.DEFAULT", ver2List, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_EDIT_VERSION", "POST", "")

    print("==============================")
    print ("Unregister Tables Started")
    print("==============================")
#                                                   **** wildcards for tables I don't want to include ****
    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")

# If you want to include all tables in the database use this instead:
#    for tbs in arcpy.ListTables():
#       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")

    # Run the compress tool.
    print "Compressing database..."
    arcpy.Compress_management(editDB)

# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ script initiation, Rec/Post process error handling \\\\\\\\\\\\\\\\\\\\\\\\\\\\\

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
    #write error log info
    print 'Logging error to database....'
# THIS SECTION FOR LOGGING ERRORS TO A DB TABLE
    log_messages('ReconcilePost_AssetIDs_QAQC','Failure', failMsg)
    sys.exit()

# ////////////////////////////////////// REGISTER AS VERSIONED AND RE-CREATE VERSIONS /////////////////////////////////

try:
    print("==============================")
    print ("register tables is started")
    print("==============================")
    #                                                   **** wildcards for tables I don't want to version ****
    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")

# If you want to include all tables in the database use this:
#    for tbs in arcpy.ListTables():
#       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")


    # create versions
    #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')

    # /////////////////////////////////// ANALYZE DATASETS AND CALC STATISTICS /////////////////////////////////////

    # NOTE: Rebuild indexes can accept a Python list of datasets.

    # Get a list of all the datasets the user has access to.
    # First, get all the stand alone tables, feature classes and rasters.
    dataList = arcpy.ListTables() + arcpy.ListFeatureClasses() + arcpy.ListRasters()

    # Next, for feature datasets get all of the datasets and featureclasses
    # from the list and add them to the master list.
    for dataset in arcpy.ListDatasets("*", "Feature"):
        arcpy.env.workspace = os.path.join(workspace, dataset)
        dataList += arcpy.ListFeatureClasses() + arcpy.ListDatasets()

    # reset the workspace
    arcpy.env.workspace = workspace

    # Concatenate all datasets into a list
    datasetList = [ds for ds in dataList]

    print "rebuilding indexes"
    # Execute rebuild indexes
    # Note: to use the "SYSTEM" option the workspace user must be an administrator.
    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"

    #Allow the database to begin accepting connections again
    print "Set databases to allow connections..."
    arcpy.AcceptConnections(editDB, True)

# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ If Script Successful, log into database \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

    print 'Data has been synched succesfully and logged in the tracking database'

#     conn = pyodbc.connect('Trusted_Connection=yes', driver='{SQL Server}', server="ggogisapp1.scwater.com", database= "Database Connections/Arden.Admin.sde, ALL")
#     cursor = conn.cursor()
#     # You will need to create a table in sql to log to and add fields that you want to log into and pass them as params to this 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'


# /////////////////////////////////Register as Versioned Error handling area////////////////////////////////////////////

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
    #write error log info
    print 'Logging error to database....'
    # THIS SECTION FOR LOGGING ERRORS TO A DB TABLE
    log_messages('ReconcilePost_AssetIDs_QAQC','Failure', failMsg)
    conn = pyodbc.connect('Trusted_Connection=yes', driver='{SQL Server}', server=sqlsvrname, database=sqldbname)
    cursor = conn.cursor()
    #You will need to create a table in sql to log to and add fields that you want to log into and pass them as params to this 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()

print "Script has run its course and is now complete"

Outcomes