-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API TableBuilder class reduces inconsistency between temp & prod db
- Loading branch information
1 parent
33600c3
commit 1140cb3
Showing
3 changed files
with
74 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace SilverStripe\ORM\Connect; | ||
|
||
use SilverStripe\Control\Director; | ||
use SilverStripe\Core\Injector\Injectable; | ||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\ORM\DB; | ||
|
||
class TableBuilder | ||
{ | ||
use Injectable; | ||
|
||
public function buildTables(DBSchemaManager $dbSchema, array $dataClasses, array $extraDataObjects = [], bool $quiet = false, bool $testMode = false, bool $showRecordCounts = false) | ||
{ | ||
$dbSchema->schemaUpdate(function () use ($dataClasses, $extraDataObjects, $testMode, $quiet, $showRecordCounts) { | ||
$dataObjectSchema = DataObject::getSchema(); | ||
|
||
foreach ($dataClasses as $dataClass) { | ||
// Check if class exists before trying to instantiate - this sidesteps any manifest weirdness | ||
if (!class_exists($dataClass)) { | ||
continue; | ||
} | ||
|
||
// Check if this class should be excluded as per testing conventions | ||
/** @var DataObject $SNG */ | ||
$SNG = new $dataClass([], DataObject::CREATE_SINGLETON); | ||
if (!$testMode && $SNG instanceof TestOnly) { | ||
continue; | ||
} | ||
|
||
// Log data | ||
if (!$quiet) { | ||
$tableName = $dataObjectSchema->tableName($dataClass); | ||
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 { | ||
$countSuffix = ""; | ||
} | ||
|
||
if (Director::is_cli()) { | ||
echo " * $tableName$countSuffix\n"; | ||
} else { | ||
echo "<li>$tableName$countSuffix</li>\n"; | ||
} | ||
} | ||
|
||
// Instruct the class to apply its schema to the database | ||
$SNG->requireTable(); | ||
|
||
// If we have additional dataobjects which need schema (i.e. for tests), do so here: | ||
if ($extraDataObjects) { | ||
foreach ($extraDataObjects as $dataClass) { | ||
$SNG = new $dataClass([], DataObject::CREATE_SINGLETON); | ||
if ($SNG instanceof DataObject) { | ||
$SNG->requireTable(); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters