def replaceRow()

in APS to SQL DW Migration - Schema and Data Migration with PolyBase/3_ChangeSchemas/ChangeSchemas.py [0:0]


def replaceRow(oneRow, baseDbName, apsDbName, apsSchema, SQLDWSchema):
    newRow = oneRow
    #################################################################
    # For all patterns below, use same newPat until it is redefined. 
    #newPat = " [" + SQLDWSchema + "]."          #leave gap         # ==> [SQLDWSchema].
    newPat = "[" + SQLDWSchema + "]."                              # ==> [SQLDWSchema].

    oldPat = "[" + apsDbName + "].[" + apsSchema + "]."           # [apsDbName].[apsSchema]. 
    repText = re.compile(re.escape(oldPat), re.IGNORECASE)
    newRow = repText.sub(newPat, newRow)

    oldPat = apsDbName + ".[" + apsSchema + "]."                  # apsDbName.[apsSchema].   
    repText = re.compile(re.escape(oldPat), re.IGNORECASE)           
    newRow = repText.sub(newPat, newRow)

    oldPat = "[" + apsDbName + "]." + apsSchema + "."              # [apsDbName].apsSchema.   
    repText = re.compile(re.escape(oldPat), re.IGNORECASE)       
    newRow = repText.sub(newPat, newRow)

    oldPat = apsDbName + "." + apsSchema + "."                     # apsDbName.apsSchema.   
    repText = re.compile(re.escape(oldPat), re.IGNORECASE)
    newRow = repText.sub(newPat, newRow)

    #################################################################
    # for these use cases involving OBJECT_ID()
    #### For all patterns below, use same newPat until it is redefined. 
    newPat = "'[" + SQLDWSchema + "]."  

    # IF OBJECT_ID ('[CSBI_STAGE].[CSS.TMP_TableName]') IS NOT NULL
    oldPat = "'[" + apsDbName + "].[" + apsSchema + "]."           
    repText = re.compile(re.escape(oldPat), re.IGNORECASE) 
    newRow = repText.sub(newPat, newRow)

    # IF OBJECT_ID ('[CSBI_STAGE].CSS.TMP_TableName') IS NOT NULL
    oldPat = "'[" + apsDbName + "]." + apsSchema + "."          
    repText = re.compile(re.escape(oldPat), re.IGNORECASE) 
    newRow = repText.sub(newPat, newRow)

    # IF OBJECT_ID ('CSBI_STAGE.[CSS].TMP_TableName') IS NOT NULL
    oldPat = "'" + apsDbName + ".[" + apsSchema + "]."          
    repText = re.compile(re.escape(oldPat), re.IGNORECASE) 
    newRow = repText.sub(newPat, newRow)

    # IF OBJECT_ID ('CSBI_STAGE.CSS.TMP_TableName') IS NOT NULL
    oldPat = "'" + apsDbName + "." + apsSchema + "."         
    repText = re.compile(re.escape(oldPat), re.IGNORECASE) 
    newRow = repText.sub(newPat, newRow)

    #################################################################
    # System Tables 
    # This is a new set of patterns 
    # For all patterns below, use same newPat until it is redefined. 
    newPat = " sys."                                              # ==> sys. 

    oldPat = "[" + apsDbName + "]" + ".sys."                      # [apsDbName].sys. 
    repText = re.compile(re.escape(oldPat), re.IGNORECASE)  
    newRow = repText.sub(newPat, newRow)

    oldPat = apsDbName + ".sys."                                   # apsDbName.sys. 
    repText = re.compile(re.escape(oldPat), re.IGNORECASE)       
    newRow = repText.sub(newPat, newRow)

    #################################################################
    if (baseDbName.upper() == apsDbName.upper()):                  # [CSS]. need to know baseDbName 
        # For all patterns below, use same newPat until it is redefined.   
        newPat = " [" + SQLDWSchema + "]."                         # => [SQLDWSchema].

        oldPat = " [" + apsSchema + "]."                           # [schema]. with implied DB 
        repText = re.compile(re.escape(oldPat), re.IGNORECASE)
        newRow = repText.sub(newPat, newRow)                       # => [SQLDWSchema].

        oldPat = " " + apsSchema + "."                             # schema.   with implied DB   
        repText = re.compile(re.escape(oldPat), re.IGNORECASE)     
        newRow = repText.sub(newPat, newRow)

        ############################################
        # For IF OBJECT_ID ()
        ### For all patterns below, use same newPat until it is redefined. 
        newPat = "'[" + SQLDWSchema + "]."  
 
        # IF OBJECT_ID ('[CSS].TMP_TableName') IS NOT NULL              
        oldPat = "'[" + apsSchema + "]."                         
        repText = re.compile(re.escape(oldPat), re.IGNORECASE)
        newRow = repText.sub(newPat, newRow)  

        # IF OBJECT_ID ('CSS.TMP_TableName') IS NOT NULL     
        oldPat = "'" + apsSchema + "."                         
        repText = re.compile(re.escape(oldPat), re.IGNORECASE)
        newRow = repText.sub(newPat, newRow)      


    return newRow