diff --git a/core/modules/block/block.module b/core/modules/block/block.module
index 4d942ed..2bb0f05 100644
--- a/core/modules/block/block.module
+++ b/core/modules/block/block.module
@@ -512,6 +512,25 @@ function _block_rehash($theme = NULL) {
 }
 
 /**
+ * Update the {block} table with the blocks currently exported by modules for
+ * all themes.
+ */
+function _block_rehash_all() {
+  // Rehash blocks for active themes. We don't use list_themes() here,
+  // because if MAINTENANCE_MODE is defined it skips reading the database,
+  // and we can't tell which themes are active.
+  $themes = db_select('system', 's')
+    ->fields('s', array('name'))
+    ->condition('type', 'theme')
+    ->condition('status', 1)
+    ->execute();
+
+  foreach ($themes as $theme) {
+    _block_rehash($theme->name);
+  }
+}
+
+/**
  * Returns information from database about a user-created (custom) block.
  *
  * @param $bid
@@ -921,13 +940,7 @@ function _block_get_renderable_block($element) {
  * Implements hook_flush_caches().
  */
 function block_flush_caches() {
-  // Rehash blocks for active themes. We don't use list_themes() here,
-  // because if MAINTENANCE_MODE is defined it skips reading the database,
-  // and we can't tell which themes are active.
-  $themes = db_query("SELECT name FROM {system} WHERE type = 'theme' AND status = 1");
-  foreach ($themes as $theme) {
-    _block_rehash($theme->name);
-  }
+ _block_rehash_all();
 
   return array('block');
 }
@@ -1022,6 +1035,14 @@ function block_admin_paths() {
 }
 
 /**
+ * Implements hook_modules_enabled().
+ */
+function block_modules_enabled() {
+  // Rehash the blocks.
+  _block_rehash_all();
+}
+
+/**
  * Implements hook_modules_uninstalled().
  *
  * Cleans up {block} and {block_role} tables from modules' blocks.
diff --git a/core/modules/block/block.test b/core/modules/block/block.test
index dbd7dc4..19d7719 100644
--- a/core/modules/block/block.test
+++ b/core/modules/block/block.test
@@ -448,7 +448,7 @@ class NewDefaultThemeBlocks extends DrupalWebTestCase {
     $result = db_query('SELECT * FROM {block} WHERE theme = :theme', array(':theme' => 'stark'));
     foreach ($result as $block) {
       unset($block->theme, $block->bid);
-      $this->assertEqual($blocks[$block->module][$block->delta], $block, t('Block %name matched', array('%name' => $block->module . '-' . $block->delta)));
+      $this->assertEqual($blocks[$block->module][$block->delta], $block, t('Block matched: @module-@delta', array('@module' => $block->module, '@delta' => $block->delta)));
     }
   }
 }
