Index: modules/menu/menu.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/menu/menu.install,v
retrieving revision 1.19
diff -u -r1.19 menu.install
--- modules/menu/menu.install	20 Jul 2009 18:51:33 -0000	1.19
+++ modules/menu/menu.install	30 Aug 2009 07:00:49 -0000
@@ -21,9 +21,12 @@
     'secondary-menu' => 'The <em>Secondary menu</em> is the default source for the Secondary links which are often used for legal notices, contact details, and other navigation items that play a lesser role than the Main links.',
   );
   $t = get_t();
-  $query = db_insert('menu_custom')->fields(array('menu_name', 'title', 'description'));
   foreach ($system_menus as $menu_name => $title) {
-    $query->values(array('menu_name' => $menu_name, 'title' => $t($title), 'description' => $t($descriptions[$menu_name])))->execute();
+    menu_custom_save(array(
+      'menu_name' => $menu_name,
+      'title' => $t($title),
+      'description' => $t($descriptions[$menu_name]),
+    ));
   }
 }
 
Index: modules/menu/menu.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/menu/menu.module,v
retrieving revision 1.201
diff -u -r1.201 menu.module
--- modules/menu/menu.module	29 Aug 2009 05:46:03 -0000	1.201
+++ modules/menu/menu.module	30 Aug 2009 07:00:56 -0000
@@ -496,3 +496,38 @@
 
   return $query->execute()->fetchAllKeyed();
 }
+
+/**
+ * Save a custom menu.
+ *
+ * @param $custom_menu
+ *   The custom menu array. Keys are:
+ *   - menu_name    Unique key for menu.
+ *   - title        Menu title; displayed at top of block.
+ *   - description  Menu description.
+ */
+function menu_custom_save(array $custom_menu) {
+  db_merge('menu_custom')
+    ->key(array('menu_name' => $custom_menu['menu_name']))
+    ->fields(array(
+        'title' => $custom_menu['title'],
+        'description' => $custom_menu['description'],
+    ))
+    ->execute();
+}
+
+/**
+ * Delete custom menu from the database
+ *
+ * @param $menu_name
+ *   The name of the custom menu to be deleted.
+ */
+function menu_custom_delete($menu_name) {
+  // Delete all the links in the menu and the menu from the list of custom menus.
+  db_delete('menu_custom')
+    ->condition('menu_name', $menu_name)
+    ->execute();
+  db_delete('menu_links')
+    ->condition('menu_name', $menu_name)
+    ->execute();
+}
Index: modules/menu/menu.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/menu/menu.test,v
retrieving revision 1.20
diff -u -r1.20 menu.test
--- modules/menu/menu.test	21 Aug 2009 14:27:44 -0000	1.20
+++ modules/menu/menu.test	30 Aug 2009 07:01:00 -0000
@@ -87,6 +87,34 @@
     $this->menu = $this->addCustomMenu();
     $this->doMenuTests($this->menu['menu_name']);
     $this->addInvalidMenuLink($this->menu['menu_name']);
+    $this->addCustomMenuApi();
+  }
+  
+  /**
+   * Add custom menu using API.
+   */
+  function addCustomMenuApi() {
+    // Add a new custom menu.
+    $menu_name = substr(md5($this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
+    $title = $this->randomName(16);
+    
+    $menu = array(
+      'menu_name' => $menu_name,
+      'title' => $title,
+      'description' => 'Description text',
+    );
+    menu_custom_save($menu);
+    
+    // Assert the menu.
+    $this->drupalGet('admin/structure/menu-customize/' . $menu_name . '/edit');
+    $this->assertText($title, t('Custom menu was added.'));
+    
+    // Edit the menu.
+    $new_title = $this->randomName(16);
+    $menu['title'] = $new_title;
+    menu_custom_save($menu);
+    $this->drupalGet('admin/structure/menu-customize/' . $menu_name . '/edit');
+    $this->assertText($new_title, t('Custom menu was edited.'));    
   }
 
   /**
@@ -147,6 +175,9 @@
     $this->assertResponse(200);
     $this->assertRaw(t('The custom menu %title has been deleted.', array('%title' => $title)), t('Custom menu was deleted'));
     $this->assertFalse(menu_load($menu_name), 'Custom menu was deleted');
+    // Test if all menu links associated to the menu were removed from database.
+    $result = db_query("SELECT menu_name FROM {menu_links} WHERE menu_name = :menu_name", array('menu_name' => $menu_name))->fetchField();
+    $this->assertFalse($result, t('All menu links associated to the custom menu were deleted.'));
   }
 
   /**
Index: modules/menu/menu.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/menu/menu.admin.inc,v
retrieving revision 1.56
diff -u -r1.56 menu.admin.inc
--- modules/menu/menu.admin.inc	24 Aug 2009 01:49:41 -0000	1.56
+++ modules/menu/menu.admin.inc	30 Aug 2009 07:00:49 -0000
@@ -527,13 +527,10 @@
   foreach ($result as $m) {
     menu_link_delete($m['mlid']);
   }
+
   // Delete all the links in the menu and the menu from the list of custom menus.
-  db_delete('menu_links')
-    ->condition('menu_name', $menu['menu_name'])
-    ->execute();
-  db_delete('menu_custom')
-    ->condition('menu_name', $menu['menu_name'])
-    ->execute();
+  menu_custom_delete($menu['menu_name']);
+
   // Delete all the blocks for this menu.
   if (module_exists('block')) {
     db_delete('block')
@@ -595,22 +592,10 @@
     ->fetchField();
 
     menu_link_save($link);
-    db_insert('menu_custom')
-      ->fields(array(
-        'menu_name' => $menu['menu_name'],
-        'title' => $menu['title'],
-        'description' => $menu['description'],
-      ))
-      ->execute();
+    menu_custom_save($menu);
   }
   else {
-    db_update('menu_custom')
-      ->fields(array(
-        'title' => $menu['title'],
-        'description' => $menu['description'],
-      ))
-      ->condition('menu_name', $menu['menu_name'])
-      ->execute();
+    menu_custom_save($menu);
     $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path", array(':path' => $path . $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
     foreach ($result as $m) {
       $link = menu_link_load($m['mlid']);
Index: modules/toolbar/toolbar.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/toolbar/toolbar.install,v
retrieving revision 1.3
diff -u -r1.3 toolbar.install
--- modules/toolbar/toolbar.install	30 Jul 2009 19:24:21 -0000	1.3
+++ modules/toolbar/toolbar.install	30 Aug 2009 07:01:00 -0000
@@ -14,14 +14,11 @@
  */
 function toolbar_install() {
   $t = get_t();
-  $query = db_insert('menu_custom')
-    ->fields(array(
-      'menu_name' => 'admin_shortcuts',
-      'title' => $t('Administration shortcuts'),
-      'description' => $t('The <em>Admininstration shortcuts</em> menu contains commonly used links for administrative tasks.')
-    ))
-    ->execute();
-
+  menu_custom_save(array(
+    'menu_name' => 'admin_shortcuts',
+    'title' => $t('Administration shortcuts'),
+    'description' => $t('The <em>Admininstration shortcuts</em> menu contains commonly used links for administrative tasks.')
+  ));
   // Add starter convenience shortcuts.
   menu_rebuild();
   $items = array(
Index: includes/menu.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/menu.inc,v
retrieving revision 1.341
diff -u -r1.341 menu.inc
--- includes/menu.inc	24 Aug 2009 01:49:41 -0000	1.341
+++ includes/menu.inc	30 Aug 2009 07:00:39 -0000
@@ -1384,7 +1384,13 @@
  * Return an array containing the names of system-defined (default) menus.
  */
 function menu_list_system_menus() {
-  return array('navigation' => 'Navigation', 'management' => 'Management', 'user-menu' => 'User menu', 'main-menu' => 'Main menu', 'secondary-menu' => 'Secondary menu');
+  return array(
+    'navigation' => 'Navigation',
+    'management' => 'Management',
+    'user-menu' => 'User menu',
+    'main-menu' => 'Main menu',
+    'secondary-menu' => 'Secondary menu'
+  );
 }
 
 /**
