2010-04-13 03:46:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class DataObjectSchemaGenerationTest extends SapphireTest {
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DataObjectSchemaGenerationTest_DO',
|
|
|
|
);
|
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-11 02:53:08 +02:00
|
|
|
// Verify that it doesn't need to be recreated
|
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$obj = new DataObjectSchemaGenerationTest_DO();
|
|
|
|
$obj->requireTable();
|
|
|
|
$db->endSchemaUpdate();
|
2010-04-13 03:46:27 +02:00
|
|
|
|
2012-09-11 02:53:08 +02:00
|
|
|
// Test table within this database
|
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$obj2 = new DataObjectSchemaGenerationTest_DO();
|
|
|
|
$obj2->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
|
2010-04-13 03:46:27 +02:00
|
|
|
// Verify that it doesn't need to be recreated
|
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$obj = new DataObjectSchemaGenerationTest_DO();
|
|
|
|
$obj->requireTable();
|
2012-09-11 02:53:08 +02:00
|
|
|
$db->endSchemaUpdate();
|
|
|
|
|
|
|
|
// Let's insert a new field here
|
|
|
|
DataObjectSchemaGenerationTest_DO::$db['SecretField'] = 'Varchar(100)';
|
|
|
|
|
|
|
|
// Test table within this database
|
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$obj2 = new DataObjectSchemaGenerationTest_DO();
|
|
|
|
$obj2->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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
// enable fulltext option on this table
|
|
|
|
Config::inst()->update('DataObjectSchemaGenerationTest_IndexDO', 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM'));
|
|
|
|
|
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
|
|
|
// Verify that it doesn't need to be recreated
|
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$obj = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$obj->requireTable();
|
|
|
|
$db->endSchemaUpdate();
|
|
|
|
|
|
|
|
// Test table within this database
|
|
|
|
$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-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();
|
|
|
|
|
|
|
|
// enable fulltext option on this table
|
|
|
|
Config::inst()->update('DataObjectSchemaGenerationTest_IndexDO', 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM'));
|
|
|
|
|
|
|
|
// Table will have been initially created by the $extraDataObjects setting
|
|
|
|
// Verify that it doesn't need to be recreated
|
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$obj = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$obj->requireTable();
|
|
|
|
$db->endSchemaUpdate();
|
|
|
|
|
|
|
|
// Let's insert a new field here
|
|
|
|
DataObjectSchemaGenerationTest_IndexDO::$indexes['SearchFields']['value'] = '"Title"';
|
|
|
|
|
|
|
|
// Test table within this database
|
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$obj2 = new DataObjectSchemaGenerationTest_IndexDO();
|
|
|
|
$obj2->requireTable();
|
|
|
|
$needsUpdating = $db->doesSchemaNeedUpdating();
|
|
|
|
$db->cancelSchemaUpdate();
|
|
|
|
|
|
|
|
$this->assertTrue($needsUpdating);
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
// Space between 'unique' and '("Name")' is critical. @todo - Robustify?
|
|
|
|
'NameIndex' => 'unique ("Title")',
|
|
|
|
'SearchFields' => array(
|
|
|
|
'type' => 'fulltext',
|
|
|
|
'name' => 'SearchFields',
|
|
|
|
'value' => '"Title","Content"'
|
|
|
|
)
|
|
|
|
);
|
2010-04-13 03:46:27 +02:00
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|