sqlHelperRemovePackages <- function()

in R/R/sqlPackage.R [2192:2296]


sqlHelperRemovePackages <- function(connectionString, pkgs, pkgsToDrop, pkgsToReport, scope, owner, verbose, languageName)
{
    user <- "" # user argument for Drop External Library
    queryUser <- "CURRENT_USER" # user argument for select to discover external_library_id

    scopeint <- parseScope(scope)

    if (scopeint == 0 && owner == '')
    {
        # if scope is public the user has to be either dbo or member of db_owner
        # if current user is already dbo we just proceed, else if user
        # is member of db_owner (e.g. RevoTester) we run as 'dbo' to
        # force it to install into the public folder instead of the private.
        #
        currentUser <- sqlSelectUser(connectionString);

        if (currentUser == "dbo")
        {
            user <- ""
            queryUser = "CURRENT_USER"
        }
        else
        {
            user <- "dbo"
            queryUser = "'dbo'"
        }
    }
    else
    {
        user <- owner
        if (nchar(owner) > 0)
        {
            queryUser <- paste0("'", owner, "'")
        }
    }

    hodbc <- NULL
    haveTransaction <- FALSE
    pkgsSuccess <- c()

    tryCatch({
        hodbc <- connectToServer(connectionString)

        checkResult(dbBegin(hodbc), expectedResult=TRUE, errorMessage="failed to create transaction")

        haveTransaction <- TRUE

        # first drop potentially bad packages that fails to install during SPEES
        # then uninstall fully installed packages that will combine DROP + SPEES
        #
        if (length(pkgsToDrop) > 0)
        {
            lapply(pkgsToDrop, sqlDropExternalLibrary, hodbc = hodbc, user=user )
            pkgsSuccess <- c(pkgsSuccess, pkgsToDrop)
        }

        if (length(pkgs) > 0)
        {
            # get the external library ids of all packages to be removed so we can cross-reference
            # with the view sys.external_library_setup_errors for errors reported
            # by the external library uninstaller
            #
            externalLibraryIds <- sqlQueryExternalLibraryId(hodbc, pkgs, scopeint, queryUser, languageName)
            lapply(pkgs, sqlDropExternalLibrary, hodbc = hodbc, user=user)
            pkgsSuccess <- c(pkgsSuccess, sqlSyncRemovePackages(hodbc, c(pkgs,pkgsToReport), externalLibraryIds, scope, user, queryUser, verbose, languageName))
        }
        else if(length(pkgsToReport) > 0)
        {
            pkgsSuccess <- c(pkgsSuccess, sqlSyncRemovePackages(hodbc, pkgsToReport, externalLibraryIds = NULL, scope, user, queryUser = NULL, verbose = verbose, languageName))
        }

        dbCommit(hodbc)
        haveTransaction = FALSE
    },
    error = function(err)
    {
        stop(sprintf("Removal of packages %s failed with error %s", paste(c(pkgs, pkgsToDrop, pkgsToReport), collapse = ', '), err$message), call. = FALSE)
    },
    finally =
    {
        # If we still have a transaction, there was an error and we rollback
        #
        if(haveTransaction)
        {
            dbRollback(hodbc)
        }

        if(!is.null(hodbc))
        {
            dbDisconnect(hodbc)
        }
    })

    if(length(pkgsSuccess) > 0)
    {
        if(verbose)
        {
            write(sprintf("%s  Successfully removed packages from SQL server (%s).", pkgTime(), paste(pkgsSuccess, collapse = ', ')), stdout())
        }
        else
        {
            write(sprintf("Successfully removed packages from SQL server (%s).", paste(pkgsSuccess, collapse = ', ')), stdout())
        }
    }
}