diff --git a/core/model/Database.php b/core/model/Database.php index 2055ef871..8f7de074b 100755 --- a/core/model/Database.php +++ b/core/model/Database.php @@ -171,6 +171,9 @@ abstract class SS_Database { $this->schemaUpdateTransaction = array(); } + /** + * Completes a schema-updated transaction, executing all the schema chagnes. + */ function endSchemaUpdate() { foreach($this->schemaUpdateTransaction as $tableName => $changes) { switch($changes['command']) { @@ -186,6 +189,20 @@ abstract class SS_Database { } $this->schemaUpdateTransaction = null; } + + /** + * Cancels the schema updates requested after a beginSchemaUpdate() call. + */ + function cancelSchemaUpdate() { + $this->schemaUpdateTransaction = null; + } + + /** + * Returns true if schema modifications were requested after a beginSchemaUpdate() call. + */ + function doesSchemaNeedUpdating() { + return (bool)$this->schemaUpdateTransaction; + } // Transactional schema altering functions - they don't do anyhting except for update schemaUpdateTransaction diff --git a/tests/model/DatabaseTest.php b/tests/model/DatabaseTest.php index dc2fb60ab..572400947 100644 --- a/tests/model/DatabaseTest.php +++ b/tests/model/DatabaseTest.php @@ -57,7 +57,22 @@ class DatabaseTest extends SapphireTest { "MySQLDatabase tables can be changed to InnoDB through DataObject::\$create_table_options" ); } + } + + function testSchemaUpdateChecking() { + $db = DB::getConn(); + + // Initially, no schema changes necessary + $db->beginSchemaUpdate(); + $this->assertFalse($db->doesSchemaNeedUpdating()); + // If we make a change, then the schema will need updating + $db->transCreateTable("TestTable"); + $this->assertTrue($db->doesSchemaNeedUpdating()); + + // If we make cancel the change, then schema updates are no longer necessary + $db->cancelSchemaUpdate(); + $this->assertFalse($db->doesSchemaNeedUpdating()); } }