NEW: Add record count to dev/build output.

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.
This commit is contained in:
Sam Minnee 2018-02-17 19:02:50 +13:00
parent 420041f2b6
commit a2143680e8

View File

@ -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 "<li>$tableName</li>\n";
$countSuffix = "";
}
if (Director::is_cli()) {
echo " * $tableName$countSuffix\n";
} else {
echo "<li>$tableName$countSuffix</li>\n";
}
}