function v1_v2()

in upgrades.js [25:113]


function v1_v2(err, s3Info, configPre, forwardCallback) {
    if (configPre.clusterEndpoint) {
        logger.warn("Upgrading to Version 2.x multi-cluster configuration");

        // bind the current config items into the new config map
        var clusterConfig = {
            M: {}
        };
        clusterConfig.M.clusterEndpoint = configPre.clusterEndpoint;
        clusterConfig.M.clusterPort = configPre.clusterPort;
        clusterConfig.M.clusterDB = configPre.clusterDB;
        clusterConfig.M.connectUser = configPre.connectUser;
        clusterConfig.M.connectPassword = configPre.connectPassword;
        clusterConfig.M.targetTable = configPre.targetTable;
        clusterConfig.M.truncateTarget = configPre.truncateTarget;

        // update dynamo, adding the new config map as 'loadClusters' and
        // removing the old values
        var updateRequest = {
            Key: {
                s3Prefix: {
                    S: s3Info.prefix
                }
            },
            TableName: configTable,
            UpdateExpression: "SET #loadCluster = :newLoadCluster, lastUpdate = :updateTime, #ver = :version "
                + "REMOVE clusterEndpoint, clusterPort, clusterDB, connectUser, connectPassword, targetTable, truncateTarget",
            ExpressionAttributeValues: {
                ":newLoadCluster": {
                    L: [clusterConfig]
                },
                ":updateTime": {
                    S: common.getFormattedDate()
                },
                ":version": {
                    S: pjson.version
                }
            },
            ExpressionAttributeNames: {
                "#ver": 'version',
                "#loadCluster": 'loadClusters'
            },
            /*
             * current can't be the target version, or someone else has done an
             * upgrade
             */
            ConditionExpression: "(attribute_not_exists(#ver) or #ver != :version) and attribute_not_exists(#loadCluster)",
            // add the ALL_NEW return values so we have the get the config after
            // update
            ReturnValues: "ALL_NEW"
        };

        logger.debug(JSON.stringify(updateRequest));

        dynamoClient.updateItem(updateRequest, function (err, data) {
            if (err) {
                if (err.code === conditionCheckFailed) {
                    // no problem - configuration was upgraded by someone else
                    // while we were running - requery and return
                    var dynamoLookup = {
                        Key: {
                            s3Prefix: {
                                S: s3Info.prefix
                            }
                        },
                        TableName: configTable,
                        ConsistentRead: true
                    };

                    dynamoClient.getItem(dynamoLookup, function (err, data) {
                        forwardCallback(null, s3Info, data.Item);
                    });
                } else {
                    // unknown error - return the original configuration with
                    // the
                    // error
                    forwardCallback(err, s3Info, configPre);
                }
            } else {
                // update was OK - go ahead with the new item returned by the
                // upgrade call
                forwardCallback(null, s3Info, data.Attributes);
            }
        });
    } else {
        // no upgrade required
        forwardCallback(null, s3Info, configPre);
    }
}