2010-04-13 03:46:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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
|
|
|
);
|
2012-09-14 04:55:07 +02:00
|
|
|
|
|
|
|
function setUpOnce() {
|
|
|
|
|
|
|
|
// enable fulltext option on this table
|
|
|
|
Config::inst()->update('DataObjectSchemaGenerationTest_IndexDO', 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM'));
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function testFieldsDontRerequestChanges() {
|
|
|
|
$db = DB::getConn();
|
|
|
|
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
|
2012-09-14 04:55:07 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
// Verify that it doesn't need to be recreated
|
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$obj = new DataObjectSchemaGenerationTest_DO();
|
|
|
|
$obj->requireTable();
|
|
|
|
$needsUpdating = $db->doesSchemaNeedUpdating();
|
|
|
|
$db->cancelSchemaUpdate();
|
|
|
|
$this->assertFalse($needsUpdating);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that updates to a class fields are reflected in the database
|
|
|
|
*/
|
|
|
|
function testFieldsRequestChanges() {
|
|
|
|
$db = DB::getConn();
|
|
|
|
DB::quiet();
|
|
|
|
|
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
|
|
|
|
|
|
|
// Let's insert a new field here
|
2012-09-14 04:55:07 +02:00
|
|
|
$oldDB = DataObjectSchemaGenerationTest_DO::$db;
|
2012-09-11 02:53:08 +02:00
|
|
|
DataObjectSchemaGenerationTest_DO::$db['SecretField'] = 'Varchar(100)';
|
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
// Verify that the above extra field triggered a schema update
|
2012-09-11 02:53:08 +02:00
|
|
|
$db->beginSchemaUpdate();
|
2012-09-14 04:55:07 +02:00
|
|
|
$obj = new DataObjectSchemaGenerationTest_DO();
|
|
|
|
$obj->requireTable();
|
2010-04-13 03:46:27 +02:00
|
|
|
$needsUpdating = $db->doesSchemaNeedUpdating();
|
|
|
|
$db->cancelSchemaUpdate();
|
2012-09-11 02:53:08 +02:00
|
|
|
$this->assertTrue($needsUpdating);
|
2012-09-14 04:55:07 +02:00
|
|
|
|
|
|
|
// Restore db configuration
|
|
|
|
DataObjectSchemaGenerationTest_DO::$db = $oldDB;
|
2012-09-11 02:53:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that indexes on a newly generated class do not subsequently request modification
|
|
|
|
*/
|
|
|
|
function testIndexesDontRerequestChanges() {
|
|
|
|
$db = DB::getConn();
|
|
|
|
DB::quiet();
|
2010-04-13 03:46:27 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
2012-09-14 04:55:07 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
// Verify that it doesn't need to be recreated
|
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$obj = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$obj->requireTable();
|
2012-09-14 04:55:07 +02:00
|
|
|
$needsUpdating = $db->doesSchemaNeedUpdating();
|
|
|
|
$db->cancelSchemaUpdate();
|
|
|
|
$this->assertFalse($needsUpdating);
|
2012-09-11 02:53:08 +02:00
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
// Test with alternate index format, although these indexes are the same
|
|
|
|
$oldIndexes = DataObjectSchemaGenerationTest_IndexDO::$indexes;
|
|
|
|
DataObjectSchemaGenerationTest_IndexDO::$indexes = DataObjectSchemaGenerationTest_IndexDO::$indexes_alt;
|
|
|
|
|
|
|
|
// Verify that it still doesn't need to be recreated
|
2012-09-11 02:53:08 +02:00
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$obj2 = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$obj2->requireTable();
|
|
|
|
$needsUpdating = $db->doesSchemaNeedUpdating();
|
|
|
|
$db->cancelSchemaUpdate();
|
2010-04-13 03:46:27 +02:00
|
|
|
$this->assertFalse($needsUpdating);
|
2012-09-14 04:55:07 +02:00
|
|
|
|
|
|
|
// Restore old index format
|
|
|
|
DataObjectSchemaGenerationTest_IndexDO::$indexes = $oldIndexes;
|
2010-04-13 03:46:27 +02:00
|
|
|
}
|
2012-09-11 02:53:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that updates to a dataobject's indexes are reflected in DDL
|
|
|
|
*/
|
|
|
|
function testIndexesRerequestChanges() {
|
|
|
|
$db = DB::getConn();
|
|
|
|
DB::quiet();
|
|
|
|
|
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
// Update the SearchFields index here
|
|
|
|
$oldIndexes = DataObjectSchemaGenerationTest_IndexDO::$indexes;
|
2012-09-11 02:53:08 +02:00
|
|
|
DataObjectSchemaGenerationTest_IndexDO::$indexes['SearchFields']['value'] = '"Title"';
|
|
|
|
|
2012-09-14 04:55:07 +02:00
|
|
|
// Verify that the above index change triggered a schema update
|
2012-09-11 02:53:08 +02:00
|
|
|
$db->beginSchemaUpdate();
|
2012-09-14 04:55:07 +02:00
|
|
|
$obj = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$obj->requireTable();
|
2012-09-11 02:53:08 +02:00
|
|
|
$needsUpdating = $db->doesSchemaNeedUpdating();
|
|
|
|
$db->cancelSchemaUpdate();
|
|
|
|
$this->assertTrue($needsUpdating);
|
2012-09-14 04:55:07 +02:00
|
|
|
|
|
|
|
// Restore old indexes
|
|
|
|
DataObjectSchemaGenerationTest_IndexDO::$indexes = $oldIndexes;
|
2012-09-11 02:53:08 +02:00
|
|
|
}
|
2010-04-13 03:46:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectSchemaGenerationTest_DO extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
|
|
|
'Enum1' => 'Enum("A, B, C, D","")',
|
|
|
|
'Enum2' => 'Enum("A, B, C, D","A")',
|
|
|
|
);
|
2012-09-11 02:53:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DataObjectSchemaGenerationTest_IndexDO extends DataObjectSchemaGenerationTest_DO implements TestOnly {
|
|
|
|
static $db = array(
|
|
|
|
'Title' => 'Varchar(255)',
|
|
|
|
'Content' => 'Text'
|
|
|
|
);
|
|
|
|
|
|
|
|
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"'
|
|
|
|
)
|
|
|
|
);
|
2012-09-14 04:55:07 +02:00
|
|
|
|
|
|
|
static $indexes_alt = array(
|
|
|
|
'NameIndex' => array(
|
|
|
|
'type' => 'unique',
|
|
|
|
'name' => 'NameIndex',
|
|
|
|
'value' => '"Title"'
|
|
|
|
),
|
|
|
|
'SearchFields' => 'fulltext ("Title","Content")'
|
|
|
|
);
|
|
|
|
}
|