diff --git a/includes/theme.maintenance.inc b/includes/theme.maintenance.inc
index 9152164..0a79c13 100644
--- includes/theme.maintenance.inc
+++ includes/theme.maintenance.inc
@@ -35,20 +35,20 @@ function _drupal_maintenance_theme() {
     $custom_theme = (isset($conf['maintenance_theme']) ? $conf['maintenance_theme'] : 'seven');
   }
   else {
-    if (!db_is_active()) {
-      // Because we are operating in a crippled environment, we need to
-      // bootstrap just enough to allow hook invocations to work.
-      require_once DRUPAL_ROOT . '/includes/database/database.inc';
-      $module_list['system']['filename'] = 'modules/system/system.module';
-      module_list(TRUE, FALSE, FALSE, $module_list);
-      drupal_load('module', 'system');
-    }
-
     // We use the default theme as the maintenance theme. If a default theme
     // isn't specified in the database or in settings.php, we use Garland.
     $custom_theme = variable_get('maintenance_theme', variable_get('theme_default', 'garland'));
   }
 
+  if (defined('MAINTENANCE_MODE') || !db_is_active()) {
+    // Because we are operating in a crippled environment, we need to
+    // bootstrap just enough to allow hook invocations and the theme rebuild to work.
+    require_once DRUPAL_ROOT . '/includes/database/database.inc';
+    $module_list['system']['filename'] = 'modules/system/system.module';
+    module_list(TRUE, FALSE, FALSE, $module_list);
+    drupal_load('module', 'system');
+  }
+
   $themes = list_themes();
 
   // list_themes() triggers a drupal_alter() in maintenance mode, but we can't
