2010-04-13 03:46:27 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2018-01-16 11:31:50 +01:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2018-10-08 06:07:50 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBEnum;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2018-01-16 11:31:50 +01:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaGenerationTest\SortedObject;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaGenerationTest\TestIndexObject;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaGenerationTest\TestObject;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class DataObjectSchemaGenerationTest extends SapphireTest
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
TestObject::class,
|
2018-01-16 11:31:50 +01:00
|
|
|
TestIndexObject::class,
|
|
|
|
SortedObject::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
public static function setUpBeforeClass(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
// Start tests
|
|
|
|
static::start();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
parent::setUpBeforeClass();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
public function testTableCaseFixed()
|
|
|
|
{
|
|
|
|
DB::quiet();
|
|
|
|
|
|
|
|
// Modify table case
|
|
|
|
DB::get_schema()->renameTable(
|
|
|
|
'DataObjectSchemaGenerationTest_DO',
|
|
|
|
'__TEMP__DataOBJECTSchemaGenerationTest_do'
|
|
|
|
);
|
|
|
|
DB::get_schema()->renameTable(
|
|
|
|
'__TEMP__DataOBJECTSchemaGenerationTest_do',
|
|
|
|
'DataOBJECTSchemaGenerationTest_do'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check table
|
|
|
|
$tables = DB::table_list();
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataOBJECTSchemaGenerationTest_do',
|
|
|
|
$tables['dataobjectschemagenerationtest_do']
|
|
|
|
);
|
|
|
|
|
|
|
|
// Rebuild table
|
|
|
|
DB::get_schema()->schemaUpdate(
|
|
|
|
function () {
|
|
|
|
TestObject::singleton()->requireTable();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check table
|
|
|
|
$tables = DB::table_list();
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaGenerationTest_DO',
|
|
|
|
$tables['dataobjectschemagenerationtest_do']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that once a schema has been generated, then it doesn't need any more updating
|
|
|
|
*/
|
|
|
|
public function testFieldsDontRerequestChanges()
|
|
|
|
{
|
|
|
|
$schema = DB::get_schema();
|
|
|
|
$test = $this;
|
|
|
|
DB::quiet();
|
|
|
|
|
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
|
|
|
|
|
|
|
// Verify that it doesn't need to be recreated
|
|
|
|
$schema->schemaUpdate(
|
|
|
|
function () use ($test, $schema) {
|
|
|
|
$obj = new TestObject();
|
|
|
|
$obj->requireTable();
|
|
|
|
$needsUpdating = $schema->doesSchemaNeedUpdating();
|
|
|
|
$schema->cancelSchemaUpdate();
|
|
|
|
$test->assertFalse($needsUpdating);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that updates to a class fields are reflected in the database
|
|
|
|
*/
|
|
|
|
public function testFieldsRequestChanges()
|
|
|
|
{
|
|
|
|
$schema = DB::get_schema();
|
|
|
|
$test = $this;
|
|
|
|
DB::quiet();
|
|
|
|
|
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
|
|
|
|
|
|
|
// Let's insert a new field here
|
2022-11-15 06:20:54 +01:00
|
|
|
TestObject::config()->merge(
|
2016-12-16 05:34:21 +01:00
|
|
|
'db',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'SecretField' => 'Varchar(100)'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Verify that the above extra field triggered a schema update
|
|
|
|
$schema->schemaUpdate(
|
|
|
|
function () use ($test, $schema) {
|
|
|
|
$obj = new TestObject();
|
|
|
|
$obj->requireTable();
|
|
|
|
$needsUpdating = $schema->doesSchemaNeedUpdating();
|
|
|
|
$schema->cancelSchemaUpdate();
|
|
|
|
$test->assertTrue($needsUpdating);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that indexes on a newly generated class do not subsequently request modification
|
|
|
|
*/
|
|
|
|
public function testIndexesDontRerequestChanges()
|
|
|
|
{
|
|
|
|
$schema = DB::get_schema();
|
|
|
|
$test = $this;
|
|
|
|
DB::quiet();
|
|
|
|
|
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
|
|
|
|
|
|
|
// Verify that it doesn't need to be recreated
|
|
|
|
$schema->schemaUpdate(
|
|
|
|
function () use ($test, $schema) {
|
|
|
|
$obj = new TestIndexObject();
|
|
|
|
$obj->requireTable();
|
|
|
|
$needsUpdating = $schema->doesSchemaNeedUpdating();
|
|
|
|
$schema->cancelSchemaUpdate();
|
|
|
|
$test->assertFalse($needsUpdating);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test with alternate index format, although these indexes are the same
|
|
|
|
$config = TestIndexObject::config();
|
2017-02-22 04:15:08 +01:00
|
|
|
$config->set('indexes', $config->get('indexes_alt'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Verify that it still doesn't need to be recreated
|
|
|
|
$schema->schemaUpdate(
|
|
|
|
function () use ($test, $schema) {
|
|
|
|
$obj2 = new TestIndexObject();
|
|
|
|
$obj2->requireTable();
|
|
|
|
$needsUpdating = $schema->doesSchemaNeedUpdating();
|
|
|
|
$schema->cancelSchemaUpdate();
|
|
|
|
$test->assertFalse($needsUpdating);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that updates to a dataobject's indexes are reflected in DDL
|
|
|
|
*/
|
|
|
|
public function testIndexesRerequestChanges()
|
|
|
|
{
|
|
|
|
$schema = DB::get_schema();
|
|
|
|
$test = $this;
|
|
|
|
DB::quiet();
|
|
|
|
|
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
|
|
|
|
|
|
|
// Update the SearchFields index here
|
2022-11-15 06:20:54 +01:00
|
|
|
TestIndexObject::config()->merge(
|
2016-12-16 05:34:21 +01:00
|
|
|
'indexes',
|
2017-05-18 18:59:57 +02:00
|
|
|
[
|
|
|
|
'SearchFields' => [
|
|
|
|
'columns' => ['Title'],
|
|
|
|
],
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Verify that the above index change triggered a schema update
|
|
|
|
$schema->schemaUpdate(
|
|
|
|
function () use ($test, $schema) {
|
|
|
|
$obj = new TestIndexObject();
|
|
|
|
$obj->requireTable();
|
|
|
|
$needsUpdating = $schema->doesSchemaNeedUpdating();
|
|
|
|
$schema->cancelSchemaUpdate();
|
|
|
|
$test->assertTrue($needsUpdating);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the generation of the ClassName spec and ensure it's not unnecessarily influenced
|
|
|
|
* by the order of classnames of existing records
|
2017-09-19 06:55:39 +02:00
|
|
|
* @skipUpgrade
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testClassNameSpecGeneration()
|
|
|
|
{
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
|
|
|
|
// Test with blank entries
|
2018-10-08 06:07:50 +02:00
|
|
|
DBEnum::flushCache();
|
2016-12-16 05:34:21 +01:00
|
|
|
$do1 = new TestObject();
|
|
|
|
$fields = $schema->databaseFields(TestObject::class, false);
|
|
|
|
$this->assertEquals("DBClassName", $fields['ClassName']);
|
|
|
|
$this->assertEquals(
|
2017-09-19 06:55:39 +02:00
|
|
|
[
|
|
|
|
TestObject::class,
|
|
|
|
TestIndexObject::class,
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$do1->dbObject('ClassName')->getEnum()
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Test with instance of subclass
|
|
|
|
$item1 = new TestIndexObject();
|
|
|
|
$item1->write();
|
2018-10-08 06:07:50 +02:00
|
|
|
DBEnum::flushCache();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-09-19 06:55:39 +02:00
|
|
|
[
|
|
|
|
TestObject::class,
|
|
|
|
TestIndexObject::class,
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$item1->dbObject('ClassName')->getEnum()
|
|
|
|
);
|
|
|
|
$item1->delete();
|
|
|
|
|
|
|
|
// Test with instance of main class
|
|
|
|
$item2 = new TestObject();
|
|
|
|
$item2->write();
|
2018-10-08 06:07:50 +02:00
|
|
|
DBEnum::flushCache();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-09-19 06:55:39 +02:00
|
|
|
[
|
|
|
|
TestObject::class,
|
|
|
|
TestIndexObject::class,
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$item2->dbObject('ClassName')->getEnum()
|
|
|
|
);
|
|
|
|
$item2->delete();
|
|
|
|
|
|
|
|
// Test with instances of both classes
|
|
|
|
$item1 = new TestIndexObject();
|
|
|
|
$item1->write();
|
|
|
|
$item2 = new TestObject();
|
|
|
|
$item2->write();
|
2018-10-08 06:07:50 +02:00
|
|
|
DBEnum::flushCache();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-09-19 06:55:39 +02:00
|
|
|
[
|
|
|
|
TestObject::class,
|
|
|
|
TestIndexObject::class,
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$item1->dbObject('ClassName')->getEnum()
|
|
|
|
);
|
|
|
|
$item1->delete();
|
|
|
|
$item2->delete();
|
|
|
|
}
|
2018-01-16 11:31:50 +01:00
|
|
|
|
|
|
|
public function testSortFieldBecomeIndexes()
|
|
|
|
{
|
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(SortedObject::class);
|
|
|
|
$this->assertContains([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['Sort'],
|
|
|
|
], $indexes);
|
|
|
|
DataObject::getSchema()->reset();
|
2022-11-15 06:20:54 +01:00
|
|
|
Config::inst()->set(SortedObject::class, 'default_sort', 'Sort ASC');
|
2018-01-16 11:31:50 +01:00
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(SortedObject::class);
|
|
|
|
$this->assertContains([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['Sort'],
|
|
|
|
], $indexes);
|
|
|
|
DataObject::getSchema()->reset();
|
2022-11-15 06:20:54 +01:00
|
|
|
Config::inst()->set(SortedObject::class, 'default_sort', 'Sort DESC');
|
2018-01-16 11:31:50 +01:00
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(SortedObject::class);
|
|
|
|
$this->assertContains([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['Sort'],
|
|
|
|
], $indexes);
|
|
|
|
DataObject::getSchema()->reset();
|
2022-11-15 06:20:54 +01:00
|
|
|
Config::inst()->set(SortedObject::class, 'default_sort', '"Sort" DESC');
|
2018-01-16 11:31:50 +01:00
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(SortedObject::class);
|
|
|
|
$this->assertContains([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['Sort'],
|
|
|
|
], $indexes);
|
|
|
|
DataObject::getSchema()->reset();
|
2022-11-15 06:20:54 +01:00
|
|
|
Config::inst()->set(SortedObject::class, 'default_sort', '"DataObjectSchemaGenerationTest_SortedObject"."Sort" ASC');
|
2018-01-16 11:31:50 +01:00
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(SortedObject::class);
|
|
|
|
$this->assertContains([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['Sort'],
|
|
|
|
], $indexes);
|
|
|
|
DataObject::getSchema()->reset();
|
2022-11-15 06:20:54 +01:00
|
|
|
Config::inst()->set(SortedObject::class, 'default_sort', '"Sort" DESC, "Title" ASC');
|
2018-01-16 11:31:50 +01:00
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(SortedObject::class);
|
|
|
|
$this->assertContains([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['Sort'],
|
|
|
|
], $indexes);
|
|
|
|
$this->assertContains([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['Title'],
|
|
|
|
], $indexes);
|
|
|
|
DataObject::getSchema()->reset();
|
|
|
|
// make sure that specific indexes aren't overwritten
|
2022-11-15 06:20:54 +01:00
|
|
|
Config::inst()->merge(SortedObject::class, 'indexes', [
|
2018-01-16 11:31:50 +01:00
|
|
|
'Sort' => [
|
|
|
|
'type' => 'unique',
|
|
|
|
'columns' => ['Sort'],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(SortedObject::class);
|
|
|
|
$this->assertContains([
|
|
|
|
'type' => 'unique',
|
|
|
|
'columns' => ['Sort'],
|
|
|
|
], $indexes);
|
|
|
|
}
|
2010-04-13 03:46:27 +02:00
|
|
|
}
|