From 9a5f467d6a6f8db0140f29964241e3b3c40d611c Mon Sep 17 00:00:00 2001
From: Fabian Franz <github@fabian-franz.de>
Date: Wed, 24 Oct 2012 06:11:53 +0200
Subject: [PATCH 1/2] Issue #1784548 by Fabianx | Damien Tournoud: Fixed DB Log is missing watchdog table after enabling - Need to catch and ignore exceptions thrown in dblog_watchdog().

---
 core/modules/dblog/dblog.module                    |   35 +++++++++++--------
 .../dblog/lib/Drupal/dblog/Tests/DBLogTest.php     |   36 ++++++++++++++++++++
 2 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/core/modules/dblog/dblog.module b/core/modules/dblog/dblog.module
index 2e8422f..e24e913 100644
--- a/core/modules/dblog/dblog.module
+++ b/core/modules/dblog/dblog.module
@@ -146,20 +146,27 @@ function _dblog_get_message_types() {
  * Note: Some values may be truncated to meet database column size restrictions.
  */
 function dblog_watchdog(array $log_entry) {
-  Database::getConnection('default', 'default')->insert('watchdog')
-    ->fields(array(
-      'uid' => $log_entry['uid'],
-      'type' => substr($log_entry['type'], 0, 64),
-      'message' => $log_entry['message'],
-      'variables' => serialize($log_entry['variables']),
-      'severity' => $log_entry['severity'],
-      'link' => substr($log_entry['link'], 0, 255),
-      'location' => $log_entry['request_uri'],
-      'referer' => $log_entry['referer'],
-      'hostname' => substr($log_entry['ip'], 0, 128),
-      'timestamp' => $log_entry['timestamp'],
-    ))
-    ->execute();
+  try {
+    Database::getConnection('default', 'default')->insert('watchdog')
+      ->fields(array(
+        'uid' => $log_entry['uid'],
+        'type' => substr($log_entry['type'], 0, 64),
+        'message' => $log_entry['message'],
+        'variables' => serialize($log_entry['variables']),
+        'severity' => $log_entry['severity'],
+        'link' => substr($log_entry['link'], 0, 255),
+        'location' => $log_entry['request_uri'],
+        'referer' => $log_entry['referer'],
+        'hostname' => substr($log_entry['ip'], 0, 128),
+        'timestamp' => $log_entry['timestamp'],
+      ))
+      ->execute();
+  }
+  catch (Exception $e) {
+    // Exception is ignored so that watchdog does not break
+    // pages during the installation process or is not able
+    // to create the watchdog table during installation.
+  }
 }
 
 /**
diff --git a/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php b/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php
index 8107cc8..ecac5a2 100644
--- a/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php
+++ b/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php
@@ -512,6 +512,42 @@ protected function testFilter() {
     $this->assertText(t('Database log cleared.'), 'Confirmation message found');
   }
 
+  protected function testDBLogException() {
+    $log = array(
+      'type'        => 'custom',
+      'message'     => 'Log entry added to test watchdog handling of Exceptions.',
+      'variables'   => array(),
+      'severity'    => WATCHDOG_NOTICE,
+      'link'        => NULL,
+      'user'        => $this->big_user,
+      'uid'         => isset($this->big_user->uid) ? $this->big_user->uid : 0,
+      'request_uri' => $base_root . request_uri(),
+      'referer'     => $_SERVER['HTTP_REFERER'],
+      'ip'          => ip_address(),
+      'timestamp'   => REQUEST_TIME,
+    );
+
+    // Remove watchdog table temporarily
+    // to simulate it missing during installation
+    db_query("DROP TABLE {watchdog}");
+
+    // Add a watchdog entry.
+    // This should not throw an Exception, but fail silently
+    dblog_watchdog($log);
+
+    // Restore schema to -1 and re-enable the module
+    // Make sure the install API is available.
+    include_once DRUPAL_ROOT . '/includes/install.inc';
+    module_disable(array('watchdog'));
+    drupal_uninstall_modules(array('watchdog'));
+    module_enable(array('watchdog'));
+
+    // Assert its working again after re-enabling
+    dblog_watchdog($log);
+    $count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField();
+    $this->assertTrue($count == 1, format_string('Dblog row count of @count is equal to 1', array('@count' => $count)));
+  }
+
   /**
    * Gets the database log event information from the browser page.
    *
-- 
1.7.4.1


From 34e03bd7b5fa6667e0d3d8ce7ba92b6923250d7f Mon Sep 17 00:00:00 2001
From: Fabian Franz <github@fabian-franz.de>
Date: Sat, 27 Oct 2012 06:52:14 +0200
Subject: [PATCH 2/2] Add other watchdog test.

---
 .../lib/Drupal/system/Tests/Common/Watchdog.php    |   68 ++++++++++++++++++++
 1 files changed, 68 insertions(+), 0 deletions(-)
 create mode 100644 core/modules/system/lib/Drupal/system/Tests/Common/Watchdog.php

diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/Watchdog.php b/core/modules/system/lib/Drupal/system/Tests/Common/Watchdog.php
new file mode 100644
index 0000000..f7fbeee
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Tests/Common/Watchdog.php
@@ -0,0 +1,68 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\system\Tests\Common\Watchdog.
+ */
+
+namespace Drupal\system\Tests\Common;
+
+use Drupal\simpletest\UnitTestBase;
+
+/**
+ * Tests the watchdog exception handling
+ */
+class Watchdog  extends UnitTestBase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Watchdog',
+      'description' => 'Tests watchdog exception handling.',
+      'group' => 'Common',
+    );
+  }
+
+ function setUp() {
+   parent::setUp();
+   drupal_install_system();
+   $this->rebuildContainer();
+ }
+
+ function tearDown() {
+   $modules = array('dblog');
+   foreach ($modules as $module) {
+     module_load_install($module);
+     $function = $module . '_schema';
+     if (function_exists($function)) {
+       foreach ($function() as $table => $schema) {
+         db_drop_table($table);
+       }
+     }
+   }
+   parent::tearDown();
+ }
+
+
+  /**
+   * Tests watchdog exception handling 
+   */
+  function testWatchdogDbLog() {
+    drupal_load('module', 'dblog');
+
+    // This should not throw an Exception even though the watchdog table does not exist, yet
+    $this->assertIdentical(FALSE, db_table_exists('watchdog'), 'Watchdog table does not exist.');
+    watchdog('custom', 'Trying to add log entry while watchdog table does not exist.');
+
+    // Now enable the module
+    module_enable(array('dblog'));
+    $this->rebuildContainer();
+    
+    // Clear watchdog table first
+    db_delete('watchdog')->execute();
+
+    // Now it should succeed.
+    watchdog('custom', 'Log entry added to test watchdog handling of Exceptions.');
+    $count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField();
+    $this->assertTrue($count == 1, format_string('Dblog row count of @count is equal to 1', array('@count' => $count)));
+  }
+
+}
-- 
1.7.4.1

