ENHANCEMENT Added Database->renameField() and Database->dontRequireField()

ENHANCEMENT Added DatabaseTest

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@65494 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-11-09 23:56:24 +00:00
parent 3cfd4d84a9
commit 117806756c
3 changed files with 96 additions and 0 deletions

View File

@ -86,6 +86,15 @@ abstract class Database extends Object {
* @param string $spec The field specification, eg 'INTEGER NOT NULL'
*/
abstract function createField($table, $field, $spec);
/**
* Change the database column name of the given field.
*
* @param string $tableName The name of the tbale the field is in.
* @param string $oldName The name of the field to change.
* @param string $newName The new name of the field
*/
abstract function renameField($tableName, $oldName, $newName);
/**
* Get a list of all the fields for the given table.
@ -343,6 +352,24 @@ abstract class Database extends Object {
}
Profiler::unmark('requireField');
}
/**
* If the given field exists, move it out of the way by renaming it to _obsolete_(fieldname).
*
* @param string $table
* @param string $fieldName
*/
function dontRequireField($table, $fieldName) {
$fieldList = $this->fieldList($table);
if(array_key_exists($fieldName, $fieldList)) {
$suffix = '';
while(isset($fieldList[strtolower("_obsolete_{$fieldName}$suffix")])) {
$suffix = $suffix ? ($suffix+1) : 2;
}
$this->renameField($table, $fieldName, "_obsolete_{$fieldName}$suffix");
Database::alteration_message("Field $table.$fieldName: renamed to $table._obsolete_{$fieldName}$suffix","obsolete");
}
}
/**
* Execute a complex manipulation on the database.

View File

@ -255,6 +255,20 @@ class MySQLDatabase extends Database {
$this->query("ALTER TABLE `$tableName` CHANGE `$fieldName` `$fieldName` $fieldSpec");
}
/**
* Change the database column name of the given field.
*
* @param string $tableName The name of the tbale the field is in.
* @param string $oldName The name of the field to change.
* @param string $newName The new name of the field
*/
public function renameField($tableName, $oldName, $newName) {
$fieldList = $this->fieldList($tableName);
if(array_key_exists($oldName, $fieldList)) {
$this->query("ALTER TABLE `$tableName` CHANGE `$oldName` `$newName` " . $fieldList[$oldName]);
}
}
public function fieldList($table) {
$fields = DB::query("SHOW FULL FIELDS IN `$table`");
foreach($fields as $field) {

View File

@ -0,0 +1,55 @@
<?php
/**
* @package sapphire
* @subpackage Testing
*/
class DatabaseTest extends SapphireTest {
function testDontRequireField() {
$conn = DB::getConn();
$this->assertArrayHasKey(
'MyField',
$conn->fieldList('DatabaseTest_MyObject')
);
$conn->dontRequireField('DatabaseTest_MyObject', 'MyField');
$this->assertArrayHasKey(
'_obsolete_MyField',
$conn->fieldList('DatabaseTest_MyObject'),
'Field is renamed to _obsolete_<fieldname> through dontRequireField()'
);
// tested schema updates, so need to rebuild the database
self::kill_temp_db();
self::create_temp_db();
}
function testRenameField() {
$conn = DB::getConn();
$conn->renameField('DatabaseTest_MyObject', 'MyField', 'MyRenamedField');
$this->assertArrayHasKey(
'MyRenamedField',
$conn->fieldList('DatabaseTest_MyObject'),
'New fieldname is set through renameField()'
);
$this->assertArrayNotHasKey(
'MyField',
$conn->fieldList('DatabaseTest_MyObject'),
'Old fieldname isnt preserved through renameField()'
);
// tested schema updates, so need to rebuild the database
self::kill_temp_db();
self::create_temp_db();
}
}
class DatabaseTest_MyObject extends DataObject implements TestOnly {
static $db = array(
'MyField' => 'Varchar'
);
}
?>