From a2143680e8ae819d3e9404aba636eb6cb168c319 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Sat, 17 Feb 2018 19:02:50 +1300 Subject: [PATCH] NEW: Add record count to dev/build output. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This small piece of metadata is intended to expose record counts to developers as an information radiator: from time to time, SilverStripe tables can get very large, and this exposes this information without the developer seeking it out. It’s reasonable to expect that count(*) calls aren’t too time consuming, even on large tables. On a small test run, dev/build execution went from 3.85s to 3.98s (a 3% or 130ms increase). Given the small relative impact it should be okay. Where this is inappropriate, it can be disabled with a config setting. --- src/ORM/DatabaseAdmin.php | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ORM/DatabaseAdmin.php b/src/ORM/DatabaseAdmin.php index 945b787e8..cc5cd1646 100644 --- a/src/ORM/DatabaseAdmin.php +++ b/src/ORM/DatabaseAdmin.php @@ -48,6 +48,11 @@ class DatabaseAdmin extends Controller 'RememberLoginHash' => 'SilverStripe\\Security\\RememberLoginHash', ]; + /** + * Config setting to enabled/disable the display of record counts on the dev/build output + */ + private static $show_record_counts = true; + protected function init() { parent::init(); @@ -272,9 +277,11 @@ class DatabaseAdmin extends Controller } } + $showRecordCounts = (boolean)$this->config()->show_record_counts; + // Initiate schema update $dbSchema = DB::get_schema(); - $dbSchema->schemaUpdate(function () use ($dataClasses, $testMode, $quiet) { + $dbSchema->schemaUpdate(function () use ($dataClasses, $testMode, $quiet, $showRecordCounts) { $dataObjectSchema = DataObject::getSchema(); foreach ($dataClasses as $dataClass) { @@ -292,10 +299,21 @@ class DatabaseAdmin extends Controller // Log data if (!$quiet) { - if (Director::is_cli()) { - echo " * $tableName\n"; + if ($showRecordCounts && DB::get_schema()->hasTable($tableName)) { + try { + $count = DB::query("SELECT COUNT(*) FROM \"$tableName\"")->value(); + $countSuffix = " ($count records)"; + } catch (Exception $e) { + $countSuffix = " (error getting record count)"; + } } else { - echo "
  • $tableName
  • \n"; + $countSuffix = ""; + } + + if (Director::is_cli()) { + echo " * $tableName$countSuffix\n"; + } else { + echo "
  • $tableName$countSuffix
  • \n"; } }