For anyone interested in this thread, I have figured it all out. Here is a modified (generalized) version of the function I wrote. Obviously, your SQL statements will be different depending on how your legacy favorites information is stored.
function migrateFavorites() {
global $wpdb;
$userstable = $wpdb->prefix . "users";
$usermetatable = $wpdb->prefix . "usermeta";
$postmetatable = $wpdb->prefix . "postmeta";
// ---------------------------------------------------------------------
// --- Find and loop through each user with favorites
// ---------------------------------------------------------------------
$sql = $wpdb->prepare("SELECT * FROM <table> WHERE ...");
$results = $wpdb->get_results($sql);
if (!$results) {
echo msg("No users found to migrate","warning");
} else {
$count = 0;
foreach($results as $result) {
$count++;
$username = $result->username;
$user_id = $result->ID;
$usermeta = get_user_meta($user_id, "simplefavorites", true);
// -----------------------------------------------------------------
// --- Get all favorites for this user
// -----------------------------------------------------------------
$sql = $wpdb->prepare("SELECT * FROM <table> WHERE ...");
$getfavorites = $wpdb->get_results($sql);
$newposts = array();
foreach($getfavorites as $favorite) {
$post_id = $favorite->post_id;
$newposts[] = intval($post_id);
// -------------------------------------------------------------
// --- Increment the favorites count for this content_id
// -------------------------------------------------------------
if ( ! add_post_meta( $post_id, 'simplefavorites_count', 1, true ) ) {
$postmeta = get_post_meta($post_id, 'simplefavorites_count');
$newcount = $postmeta[0] + 1;
update_post_meta ( $post_id, 'simplefavorites_count', $newcount );
}
}
// -----------------------------------------------------------------
// --- Now add or update usermeta table
// -----------------------------------------------------------------
if (!$usermeta) {
$favorites[] = array(
'site_id' => intval("1"),
'posts' => $newposts
);
} else {
$favorites = $usermeta;
$existing_posts = $usermeta[0]['posts'];
$allposts = array_merge($existing_posts, $newposts);
$allposts = array_unique ($allposts); // --- avoid duplicate posts
$favorites[0]['posts'] = $allposts;
}
$error = update_user_meta( $user_id, 'simplefavorites', $favorites );
if ( get_user_meta($user_id, 'simplefavorites', true ) != $favorites ) {
echo msg ("An error occurred' favorites","error");
} else {
echo msg ("Finished migrating $count users' favorites","#666");
}
}
}
}