2010-04-13 03:46:27 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\Connect\MySQLSchemaManager;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\FieldType\DBClassName;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
|
|
|
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2010-04-13 03:46:27 +02:00
|
|
|
class DataObjectSchemaGenerationTest extends SapphireTest {
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DataObjectSchemaGenerationTest_DO',
|
2012-09-14 04:55:07 +02:00
|
|
|
'DataObjectSchemaGenerationTest_IndexDO'
|
2010-04-13 03:46:27 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUpOnce() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
// enable fulltext option on this table
|
2012-09-26 23:34:00 +02:00
|
|
|
Config::inst()->update('DataObjectSchemaGenerationTest_IndexDO', 'create_table_options',
|
2013-06-21 00:32:08 +02:00
|
|
|
array(MySQLSchemaManager::ID => 'ENGINE=MyISAM'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
parent::setUpOnce();
|
|
|
|
}
|
2012-09-11 02:53:08 +02:00
|
|
|
|
2010-04-13 03:46:27 +02:00
|
|
|
/**
|
|
|
|
* Check that once a schema has been generated, then it doesn't need any more updating
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldsDontRerequestChanges() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$schema = DB::get_schema();
|
|
|
|
$test = $this;
|
2010-04-13 03:46:27 +02:00
|
|
|
DB::quiet();
|
2012-09-11 02:53:08 +02:00
|
|
|
|
2010-04-13 03:46:27 +02:00
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
// Verify that it doesn't need to be recreated
|
2013-06-21 00:32:08 +02:00
|
|
|
$schema->schemaUpdate(function() use ($test, $schema) {
|
|
|
|
$obj = new DataObjectSchemaGenerationTest_DO();
|
|
|
|
$obj->requireTable();
|
|
|
|
$needsUpdating = $schema->doesSchemaNeedUpdating();
|
|
|
|
$schema->cancelSchemaUpdate();
|
|
|
|
$test->assertFalse($needsUpdating);
|
|
|
|
});
|
2012-09-11 02:53:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that updates to a class fields are reflected in the database
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldsRequestChanges() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$schema = DB::get_schema();
|
|
|
|
$test = $this;
|
2012-09-11 02:53:08 +02:00
|
|
|
DB::quiet();
|
|
|
|
|
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
// Let's insert a new field here
|
2013-02-27 01:34:50 +01:00
|
|
|
Config::inst()->update('DataObjectSchemaGenerationTest_DO', 'db', array(
|
|
|
|
'SecretField' => 'Varchar(100)'
|
|
|
|
));
|
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
// Verify that the above extra field triggered a schema update
|
2013-06-21 00:32:08 +02:00
|
|
|
$schema->schemaUpdate(function() use ($test, $schema) {
|
|
|
|
$obj = new DataObjectSchemaGenerationTest_DO();
|
|
|
|
$obj->requireTable();
|
|
|
|
$needsUpdating = $schema->doesSchemaNeedUpdating();
|
|
|
|
$schema->cancelSchemaUpdate();
|
|
|
|
$test->assertTrue($needsUpdating);
|
|
|
|
});
|
2012-09-11 02:53:08 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Check that indexes on a newly generated class do not subsequently request modification
|
2012-09-11 02:53:08 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIndexesDontRerequestChanges() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$schema = DB::get_schema();
|
|
|
|
$test = $this;
|
2012-09-11 02:53:08 +02:00
|
|
|
DB::quiet();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
// Verify that it doesn't need to be recreated
|
2013-06-21 00:32:08 +02:00
|
|
|
$schema->schemaUpdate(function() use ($test, $schema) {
|
|
|
|
$obj = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$obj->requireTable();
|
|
|
|
$needsUpdating = $schema->doesSchemaNeedUpdating();
|
|
|
|
$schema->cancelSchemaUpdate();
|
|
|
|
$test->assertFalse($needsUpdating);
|
|
|
|
});
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
// Test with alternate index format, although these indexes are the same
|
2013-02-27 01:34:50 +01:00
|
|
|
Config::inst()->remove('DataObjectSchemaGenerationTest_IndexDO', 'indexes');
|
|
|
|
Config::inst()->update('DataObjectSchemaGenerationTest_IndexDO', 'indexes',
|
|
|
|
Config::inst()->get('DataObjectSchemaGenerationTest_IndexDO', 'indexes_alt')
|
|
|
|
);
|
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
// Verify that it still doesn't need to be recreated
|
2013-06-21 00:32:08 +02:00
|
|
|
$schema->schemaUpdate(function() use ($test, $schema) {
|
|
|
|
$obj2 = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$obj2->requireTable();
|
|
|
|
$needsUpdating = $schema->doesSchemaNeedUpdating();
|
|
|
|
$schema->cancelSchemaUpdate();
|
|
|
|
$test->assertFalse($needsUpdating);
|
|
|
|
});
|
2010-04-13 03:46:27 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
/**
|
|
|
|
* Check that updates to a dataobject's indexes are reflected in DDL
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIndexesRerequestChanges() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$schema = DB::get_schema();
|
|
|
|
$test = $this;
|
2012-09-11 02:53:08 +02:00
|
|
|
DB::quiet();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
// Update the SearchFields index here
|
2013-02-27 01:34:50 +01:00
|
|
|
Config::inst()->update('DataObjectSchemaGenerationTest_IndexDO', 'indexes', array(
|
|
|
|
'SearchFields' => array(
|
|
|
|
'value' => 'Title'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
// Verify that the above index change triggered a schema update
|
2013-06-21 00:32:08 +02:00
|
|
|
$schema->schemaUpdate(function() use ($test, $schema) {
|
|
|
|
$obj = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$obj->requireTable();
|
|
|
|
$needsUpdating = $schema->doesSchemaNeedUpdating();
|
|
|
|
$schema->cancelSchemaUpdate();
|
|
|
|
$test->assertTrue($needsUpdating);
|
|
|
|
});
|
2012-09-11 02:53:08 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-03 23:46:53 +01:00
|
|
|
/**
|
|
|
|
* Tests the generation of the ClassName spec and ensure it's not unnecessarily influenced
|
|
|
|
* by the order of classnames of existing records
|
|
|
|
*/
|
|
|
|
public function testClassNameSpecGeneration() {
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = DataObject::getSchema();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-03 23:46:53 +01:00
|
|
|
// Test with blank entries
|
2015-09-10 05:46:23 +02:00
|
|
|
DBClassName::clear_classname_cache();
|
2015-09-04 05:49:22 +02:00
|
|
|
$do1 = new DataObjectSchemaGenerationTest_DO();
|
2016-10-06 06:31:38 +02:00
|
|
|
$fields = $schema->databaseFields(DataObjectSchemaGenerationTest_DO::class, false);
|
2016-06-15 06:03:16 +02:00
|
|
|
/** @skipUpgrade */
|
2015-09-04 05:49:22 +02:00
|
|
|
$this->assertEquals("DBClassName", $fields['ClassName']);
|
2014-03-03 23:46:53 +01:00
|
|
|
$this->assertEquals(
|
2015-09-04 05:49:22 +02:00
|
|
|
array(
|
|
|
|
'DataObjectSchemaGenerationTest_DO' => 'DataObjectSchemaGenerationTest_DO',
|
|
|
|
'DataObjectSchemaGenerationTest_IndexDO' => 'DataObjectSchemaGenerationTest_IndexDO'
|
|
|
|
),
|
|
|
|
$do1->dbObject('ClassName')->getEnum()
|
2014-03-03 23:46:53 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
|
2014-03-03 23:46:53 +01:00
|
|
|
// Test with instance of subclass
|
|
|
|
$item1 = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$item1->write();
|
2015-09-10 05:46:23 +02:00
|
|
|
DBClassName::clear_classname_cache();
|
2014-03-03 23:46:53 +01:00
|
|
|
$this->assertEquals(
|
2015-09-04 05:49:22 +02:00
|
|
|
array(
|
|
|
|
'DataObjectSchemaGenerationTest_DO' => 'DataObjectSchemaGenerationTest_DO',
|
|
|
|
'DataObjectSchemaGenerationTest_IndexDO' => 'DataObjectSchemaGenerationTest_IndexDO'
|
|
|
|
),
|
|
|
|
$item1->dbObject('ClassName')->getEnum()
|
2014-03-03 23:46:53 +01:00
|
|
|
);
|
|
|
|
$item1->delete();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-03 23:46:53 +01:00
|
|
|
// Test with instance of main class
|
|
|
|
$item2 = new DataObjectSchemaGenerationTest_DO();
|
|
|
|
$item2->write();
|
2015-09-10 05:46:23 +02:00
|
|
|
DBClassName::clear_classname_cache();
|
2014-03-03 23:46:53 +01:00
|
|
|
$this->assertEquals(
|
2015-09-04 05:49:22 +02:00
|
|
|
array(
|
|
|
|
'DataObjectSchemaGenerationTest_DO' => 'DataObjectSchemaGenerationTest_DO',
|
|
|
|
'DataObjectSchemaGenerationTest_IndexDO' => 'DataObjectSchemaGenerationTest_IndexDO'
|
|
|
|
),
|
|
|
|
$item2->dbObject('ClassName')->getEnum()
|
2014-03-03 23:46:53 +01:00
|
|
|
);
|
|
|
|
$item2->delete();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-03 23:46:53 +01:00
|
|
|
// Test with instances of both classes
|
|
|
|
$item1 = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$item1->write();
|
|
|
|
$item2 = new DataObjectSchemaGenerationTest_DO();
|
|
|
|
$item2->write();
|
2015-09-10 05:46:23 +02:00
|
|
|
DBClassName::clear_classname_cache();
|
2014-03-03 23:46:53 +01:00
|
|
|
$this->assertEquals(
|
2015-09-04 05:49:22 +02:00
|
|
|
array(
|
|
|
|
'DataObjectSchemaGenerationTest_DO' => 'DataObjectSchemaGenerationTest_DO',
|
|
|
|
'DataObjectSchemaGenerationTest_IndexDO' => 'DataObjectSchemaGenerationTest_IndexDO'
|
|
|
|
),
|
|
|
|
$item1->dbObject('ClassName')->getEnum()
|
2014-03-03 23:46:53 +01:00
|
|
|
);
|
|
|
|
$item1->delete();
|
|
|
|
$item2->delete();
|
|
|
|
}
|
2010-04-13 03:46:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectSchemaGenerationTest_DO extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2010-04-13 03:46:27 +02:00
|
|
|
'Enum1' => 'Enum("A, B, C, D","")',
|
|
|
|
'Enum2' => 'Enum("A, B, C, D","A")',
|
2015-11-02 01:59:45 +01:00
|
|
|
'NumberField' => 'Decimal',
|
|
|
|
'FloatingField' => 'Decimal(10,3,1.1)',
|
|
|
|
'TextValue' => 'Varchar',
|
2016-06-15 06:03:16 +02:00
|
|
|
'Date' => 'Datetime',
|
2015-11-02 01:59:45 +01:00
|
|
|
'MyNumber' => 'Int'
|
2010-04-13 03:46:27 +02:00
|
|
|
);
|
2012-09-11 02:53:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DataObjectSchemaGenerationTest_IndexDO extends DataObjectSchemaGenerationTest_DO implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-09-11 02:53:08 +02:00
|
|
|
'Title' => 'Varchar(255)',
|
|
|
|
'Content' => 'Text'
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $indexes = array(
|
2012-09-14 04:55:07 +02:00
|
|
|
'NameIndex' => 'unique ("Title")',
|
2012-09-11 02:53:08 +02:00
|
|
|
'SearchFields' => array(
|
|
|
|
'type' => 'fulltext',
|
|
|
|
'name' => 'SearchFields',
|
|
|
|
'value' => '"Title","Content"'
|
|
|
|
)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/** @config */
|
|
|
|
private static $indexes_alt = array(
|
2012-09-14 04:55:07 +02:00
|
|
|
'NameIndex' => array(
|
|
|
|
'type' => 'unique',
|
|
|
|
'name' => 'NameIndex',
|
|
|
|
'value' => '"Title"'
|
|
|
|
),
|
|
|
|
'SearchFields' => 'fulltext ("Title","Content")'
|
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
}
|