2023-11-21 23:56:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\FrameworkTest\GridFieldArbitraryData;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Core\Extension;
|
2024-09-26 07:17:35 +02:00
|
|
|
use SilverStripe\Dev\Command\DbBuild;
|
|
|
|
use SilverStripe\PolyExecution\PolyOutput;
|
2023-11-21 23:56:15 +01:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\ORM\Queries\SQLSelect;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the table and adds default records for the ArbitraryDataModel.
|
|
|
|
*/
|
2024-09-26 07:17:35 +02:00
|
|
|
class DbBuildExtension extends Extension
|
2023-11-21 23:56:15 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This extension hook is on TestSessionEnvironment, which is used by behat but not by phpunit.
|
2024-09-26 07:17:35 +02:00
|
|
|
* For whatever reason, behat doesn't build the db, so we can't rely on the below onAfterbuild
|
2023-11-21 23:56:15 +01:00
|
|
|
* being run in that scenario.
|
|
|
|
*/
|
|
|
|
protected function onAfterStartTestSession()
|
|
|
|
{
|
2024-09-26 07:17:35 +02:00
|
|
|
$output = PolyOutput::create(
|
|
|
|
Director::is_cli() ? PolyOutput::FORMAT_ANSI : PolyOutput::FORMAT_HTML,
|
|
|
|
PolyOutput::VERBOSITY_QUIET
|
|
|
|
);
|
|
|
|
$output->startList();
|
|
|
|
$this->buildTable($output);
|
|
|
|
$output->stopList();
|
2023-11-21 23:56:15 +01:00
|
|
|
$this->populateData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-09-26 07:17:35 +02:00
|
|
|
* This extension hook is on DbBuild, after building the database.
|
2023-11-21 23:56:15 +01:00
|
|
|
*/
|
2024-09-26 07:17:35 +02:00
|
|
|
protected function onAfterBuild(PolyOutput $output, bool $populate, bool $testMode): void
|
2023-11-21 23:56:15 +01:00
|
|
|
{
|
|
|
|
if ($testMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-26 07:17:35 +02:00
|
|
|
$output->writeln('<options=bold>Creating table for frameworktest arbitrary data</>');
|
|
|
|
$output->startList();
|
|
|
|
$this->buildTable($output);
|
|
|
|
$output->stopList();
|
2023-11-21 23:56:15 +01:00
|
|
|
if ($populate) {
|
2024-09-26 07:17:35 +02:00
|
|
|
$output->writeln('<options=bold>Creating database records arbitrary data</>');
|
|
|
|
$output->startList();
|
2023-11-21 23:56:15 +01:00
|
|
|
$this->populateData();
|
2024-09-26 07:17:35 +02:00
|
|
|
$output->stopList();
|
2023-11-21 23:56:15 +01:00
|
|
|
}
|
2024-09-26 07:17:35 +02:00
|
|
|
$output->writeln(['<options=bold>Frameworktest database build completed!</>', '']);
|
2023-11-21 23:56:15 +01:00
|
|
|
}
|
|
|
|
|
2024-09-26 07:17:35 +02:00
|
|
|
private function buildTable(PolyOutput $output): void
|
2023-11-21 23:56:15 +01:00
|
|
|
{
|
|
|
|
$tableName = ArbitraryDataModel::TABLE_NAME;
|
|
|
|
|
|
|
|
// Log data
|
2024-09-26 07:17:35 +02:00
|
|
|
$showRecordCounts = DbBuild::config()->get('show_record_counts');
|
|
|
|
if ($showRecordCounts && DB::get_schema()->hasTable($tableName)) {
|
|
|
|
try {
|
|
|
|
$count = SQLSelect::create()->setFrom($tableName)->count();
|
|
|
|
$countSuffix = " ($count records)";
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$countSuffix = ' (error getting record count)';
|
2023-11-21 23:56:15 +01:00
|
|
|
}
|
2024-09-26 07:17:35 +02:00
|
|
|
} else {
|
|
|
|
$countSuffix = "";
|
2023-11-21 23:56:15 +01:00
|
|
|
}
|
2024-09-26 07:17:35 +02:00
|
|
|
// We're adding one list item, but we need to do it this way for consistency
|
|
|
|
// with the rest of the db build output
|
|
|
|
$output->writeListItem($tableName . $countSuffix);
|
2023-11-21 23:56:15 +01:00
|
|
|
|
|
|
|
// Get field schema
|
|
|
|
$fields = [
|
|
|
|
'ID' => 'PrimaryKey',
|
|
|
|
'LastEdited' => 'DBDatetime',
|
|
|
|
'Created' => 'DBDatetime',
|
|
|
|
];
|
|
|
|
$fieldNames = array_keys(ArbitraryDataAdmin::getInitialRecords()[0]);
|
|
|
|
foreach ($fieldNames as $fieldName) {
|
|
|
|
if ($fieldName === 'ID') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$fields[$fieldName] = 'Varchar';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the table to the database
|
|
|
|
DB::get_schema()->schemaUpdate(function () use ($tableName, $fields) {
|
|
|
|
DB::require_table(
|
|
|
|
$tableName,
|
|
|
|
$fields,
|
|
|
|
null,
|
|
|
|
true,
|
|
|
|
DataObject::config()->get('create_table_options')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private function populateData(): void
|
|
|
|
{
|
|
|
|
$tableName = ArbitraryDataModel::TABLE_NAME;
|
|
|
|
$count = SQLSelect::create()->setFrom($tableName)->count();
|
|
|
|
|
|
|
|
if ($count <= 0) {
|
|
|
|
$now = DBDatetime::now()->Rfc2822();
|
|
|
|
$data = ArbitraryDataAdmin::getInitialRecords();
|
|
|
|
foreach ($data as $record) {
|
|
|
|
unset($record['ID']);
|
|
|
|
$record['LastEdited'] = $now;
|
|
|
|
$record['Created'] = $now;
|
|
|
|
|
|
|
|
$item = ArbitraryDataModel::create($record);
|
|
|
|
$item->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::alteration_message('Added default records for frameworktest arbitrary data', 'created');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|