Permalink
Browse files

API Add the ability to query if the schema update is in progress.

The specific situation where this is useful is where populateDefaults on
DataObjects needs to query the database. This will break the dev/build
when it tries to create the object via singleton - the query will not be
able to be executed if the table is not there or its schema has changed.

For an example of such use case see Translatable::populateDefaults.
  • Loading branch information...
mateusz committed Oct 16, 2012
1 parent 28dce22 commit 8eb0fa91bd844cb6a1e6ad8f3cd58039c5d7aa58
Showing with 32 additions and 2 deletions.
  1. +16 −2 model/Database.php
  2. +16 −0 tests/model/DatabaseTest.php
View
@@ -180,8 +180,12 @@ public function clearCachedFieldlist($tableName=false){
* @var array
*/
protected $indexList;
-
-
+
+ /**
+ * Keeps track whether we are currently updating the schema.
+ */
+ protected $schemaIsUpdating = false;
+
/**
* Large array structure that represents a schema update transaction
*/
@@ -193,6 +197,7 @@ public function clearCachedFieldlist($tableName=false){
* Once
*/
public function beginSchemaUpdate() {
+ $this->schemaIsUpdating = true;
$this->tableList = array();
$tables = $this->tableList();
foreach($tables as $table) $this->tableList[strtolower($table)] = $table;
@@ -221,13 +226,22 @@ public function endSchemaUpdate() {
}
}
$this->schemaUpdateTransaction = null;
+ $this->schemaIsUpdating = false;
}
/**
* Cancels the schema updates requested after a beginSchemaUpdate() call.
*/
public function cancelSchemaUpdate() {
$this->schemaUpdateTransaction = null;
+ $this->schemaIsUpdating = false;
+ }
+
+ /**
+ * Returns true if we are during a schema update.
+ */
+ function isSchemaUpdating() {
+ return $this->schemaIsUpdating;
}
/**
@@ -62,6 +62,22 @@ public function testMySQLCreateTableOptions() {
}
}
+ function testIsSchemaUpdating() {
+ $db = DB::getConn();
+
+ $this->assertFalse($db->isSchemaUpdating(), 'Before the transaction the flag is false.');
+
+ $db->beginSchemaUpdate();
+ $this->assertTrue($db->isSchemaUpdating(), 'During the transaction the flag is true.');
+
+ $db->endSchemaUpdate();
+ $this->assertFalse($db->isSchemaUpdating(), 'After the transaction the flag is false.');
+
+ $db->beginSchemaUpdate();
+ $db->cancelSchemaUpdate();
+ $this->assertFalse($db->doesSchemaNeedUpdating(), 'After cancelling the transaction the flag is false');
+ }
+
public function testSchemaUpdateChecking() {
$db = DB::getConn();

0 comments on commit 8eb0fa9

Please sign in to comment.