Merge pull request #2604 from chillu/pulls/versioned-single-stage

Fixed support for single stage in Versioned
This commit is contained in:
Will Rossiter 2013-10-29 01:40:40 -07:00
commit a0b7bfc7a5
2 changed files with 57 additions and 2 deletions

View File

@ -616,7 +616,11 @@ class Versioned extends DataExtension {
} }
// If we're editing Live, then use (table)_Live instead of (table) // If we're editing Live, then use (table)_Live instead of (table)
if(Versioned::current_stage() && Versioned::current_stage() != $this->defaultStage) { if(
Versioned::current_stage()
&& Versioned::current_stage() != $this->defaultStage
&& in_array(Versioned::current_stage(), $this->stages)
) {
// If the record has already been inserted in the (table), get rid of it. // If the record has already been inserted in the (table), get rid of it.
if($manipulation[$table]['command']=='insert') { if($manipulation[$table]['command']=='insert') {
DB::query("DELETE FROM \"{$table}\" WHERE \"ID\"='$id'"); DB::query("DELETE FROM \"{$table}\" WHERE \"ID\"='$id'");

View File

@ -11,7 +11,8 @@ class VersionedTest extends SapphireTest {
protected $extraDataObjects = array( protected $extraDataObjects = array(
'VersionedTest_DataObject', 'VersionedTest_DataObject',
'VersionedTest_Subclass', 'VersionedTest_Subclass',
'VersionedTest_RelatedWithoutVersion' 'VersionedTest_RelatedWithoutVersion',
'VersionedTest_SingleStage'
); );
protected $requiredExtensions = array( protected $requiredExtensions = array(
@ -452,6 +453,46 @@ class VersionedTest extends SapphireTest {
); );
} }
public function testVersionedWithSingleStage() {
$tables = DB::tableList();
$this->assertContains(
'VersionedTest_SingleStage',
array_values($tables),
'Contains base table'
);
$this->assertContains(
'VersionedTest_SingleStage_versions',
array_values($tables),
'Contains versions table'
);
$this->assertNotContains(
'VersionedTest_SingleStage_Live',
array_values($tables),
'Does not contain separate table with _Live suffix'
);
$this->assertNotContains(
'VersionedTest_SingleStage_Stage',
array_values($tables),
'Does not contain separate table with _Stage suffix'
);
Versioned::reading_stage("Stage");
$obj = new VersionedTest_SingleStage(array('Name' => 'MyObj'));
$obj->write();
$this->assertNotNull(
VersionedTest_SingleStage::get()->byID($obj->ID),
'Writes to and reads from default stage if its set explicitly'
);
Versioned::reading_stage("Live");
$obj = new VersionedTest_SingleStage(array('Name' => 'MyObj'));
$obj->write();
$this->assertNotNull(
VersionedTest_SingleStage::get()->byID($obj->ID),
'Writes to and reads from default stage even if a non-matching stage is set'
);
}
} }
@ -517,3 +558,13 @@ class VersionedTest_Subclass extends VersionedTest_DataObject implements TestOnl
class VersionedTest_UnversionedWithField extends DataObject implements TestOnly { class VersionedTest_UnversionedWithField extends DataObject implements TestOnly {
private static $db = array('Version' => 'Varchar(255)'); private static $db = array('Version' => 'Varchar(255)');
} }
class VersionedTest_SingleStage extends DataObject implements TestOnly {
private static $db = array(
'Name' => 'Varchar'
);
private static $extensions = array(
'Versioned("Stage")'
);
}