Scripting an ArcSDE geodatabase upgrade in SQL Server

You can modify and use the Python scripts provided in this topic to connect to and upgrade an ArcSDE geodatabase.

The first script uses an existing ArcSDE connection file to connect to and upgrade a geodatabase.

The second script shows how you can provide connection information in the same script used to upgrade the geodatabase. This is especially useful if you do not have access to ArcGIS Desktop; you can run the Python script from a computer where ArcGIS Engine or ArcGIS Server Enterprise (Standard or Advanced) is installed, provide connection information, and upgrade the geodatabase.

The connection you make to the geodatabase, whether using an existing ArcSDE connection file or through parameters in the script, is a direct connection.

Be sure you have read and performed the steps in Preparing to upgrade a geodatabase in SQL Server before you upgrade.

Steps:
  1. Copy one of these scripts to the Python window in ArcGIS Desktop or to any Python IDE such as PythonWin or WING. Alter any variable values to match the information at your site..
    • If you have an existing ArcSDE connection file that uses a direct connection and connects as the ArcSDE administrator, copy and modify one of the following scripts, depending on your operating system:
      # Name: upgradesdegdb_example.py
      # Description: Connect from a Windows computer 
      # with an existing ArcSDE connection file 
      # and upgrade an ArcSDE for SQL Server geodatabase
      # Author: ESRI
      
      # Import arcpy module
      import arcpy
       
      # Local variables:
      Output_Workspace = "C:\\ArcGIS\connection_files\<Connection file>"
      Default_gdb = "C:\\ArcGIS\connection_files\<Connection file>"
      
      # Process: Upgrade Geodatabase
      arcpy.UpgradeGDB_management(<Connection file>, "PREREQUISITE_CHECK", "UPGRADE")
      # Name: upgradesdegdb_example.py
      # Description: Connect from a Linux or UNIX computer 
      # with an existing ArcSDE connection file 
      # and upgrade an ArcSDE for SQL Server geodatabase
      # Author: ESRI
      
      # Import arcpy module
      import arcpy
       
      # Local variables:
      Output_Workspace = "<user>/connections/<Connection_file>"
      Default_gdb = "<user>/connections/<Connection_file>"
      
      # Process: Upgrade Geodatabase
      arcpy.UpgradeGDB_management(<Connection_file>, "PREREQUISITE_CHECK", "UPGRADE")
    • If you do not have an ArcSDE connection file on the computer from which you are scripting the upgrade, you can copy and modify the following script:
      # Name: input_example.py
      # Description: Provide connection information to an ArcSDE geodatabase 
      # in DB2 and upgrade the geodatabase
      # Usage: Server Service Database Authentication[OPERATING_SYSTEM_AUTH | DATABASE_AUTH] User Password Version Upgrade[ TRUE | FALSE ]"
      # Author: ESRI
      
      # Import system modules
      import sys
      import os
      import arcpy
      
      def main(argv):
      
        server = argv[0]
        service = argv[1]
        database = argv[2]
        account_authentication = argv[3]
        username = argv[4]
        password = argv[5]
        version = argv[6]
        do_upgrade = argv[7]
      
        # Local variables...
        if os.name.lower() == "nt":
            slashsyntax = "\\"        
            if os.environ.get("TEMP") == None:
                temp = "c:\\temp"
            else:
                temp = os.environ.get("TEMP") 
        else:
            slashsyntax = "/"
            if os.environ.get("TMP") == None:
                temp = "/usr/tmp"
            else:
                temp = os.environ.get("TMP")
      
        Connection_File_Name = temp + slashsyntax + "connection.sde"
      
        # Check for the .sde file and delete it if present
        if os.path.exists(Connection_File_Name):
            os.remove(Connection_File_Name)
      
        #variables defined within the script; other variable options commented out at the end of the line
        saveUserInfo = "SAVE_USERNAME" #DO_NOT_SAVE_USERNAME
        saveVersionInfo = "SAVE_VERSION" #DO_NOT_SAVE_VERSION
      
        print "Creating ArcSDE Connection File..."
        # Process: Create ArcSDE Connection File...
        # Usage: out_folder_path, out_name, server, service, database, account_authentication, username, password, save_username_password,   version, save_version_info
        arcpy.CreateArcSDEConnectionFile_management(temp, "connection.sde", server, service, database, account_authentication, username, password, saveUserInfo, version, saveVersionInfo)
        
        # Process: Upgrade Geodatabase...
        try:
          if do_upgrade.lower() == "true":
              print "Upgrading Geodatabase..."
              arcpy.UpgradeGDB_management(Connection_File_Name, "PREREQUISITE_CHECK", "UPGRADE")
          else:
              print "Runnning Pre-Requisite Check..."
              arcpy.UpgradeGDB_management(Connection_File_Name, "PREREQUISITE_CHECK", "NO_UPGRADE")
        except:
              for i in range(arcpy.GetMessageCount()):
                if "ERROR" in arcpy.GetMessage(i):
                    error_msg=arcpy.GetMessage(i)
                    error_code=error_msg[6:12]
                    if error_code == "001046":
                        print(error_msg[14:])
                    elif error_code == "999999":
                        print("\nThere was a problem connecting to the database.")
                        sys.exit("Please check your connection parameters and try again.")
                    else:
                        print(error_msg)
                        sys.exit()
                else:
                    if "privileges" in arcpy.GetMessage(i):
                        sys.exit(arcpy.GetMessage(i))
      
      if __name__ == "__main__":
          if len(sys.argv) != 9:
      	print ("Usage: Server Service Database Authentication[OPERATING_SYSTEM_AUTH | DATABASE_AUTH] User Password Version Upgrade[ TRUE | FALSE ]")
              sys.exit("Incorrect number of parameters specified, expected 8, got "+str(arcpy.GetArgumentCount()))
          main(sys.argv[1:])
      
  2. Run the script you copied and altered to perform the prerequisite check and upgrade the geodatabase.

    See Writing Python scripts for more information on using Python.

Related Topics


1/26/2011