diff --git a/ORM/Connect/DBSchemaManager.php b/ORM/Connect/DBSchemaManager.php index cacd9347d..d07cd12fb 100644 --- a/ORM/Connect/DBSchemaManager.php +++ b/ORM/Connect/DBSchemaManager.php @@ -22,10 +22,18 @@ abstract class DBSchemaManager { * data corruption in tables, you can disable this behaviour and handle it * outside of this class, e.g. through a nightly system task with extended logging capabilities. * - * @var boolean + * @var bool */ private static $check_and_repair_on_build = true; + /** + * Check if tables should be renamed in a case-sensitive fashion. + * Note: This should still work even on case-insensitive databases. + * + * @var bool + */ + private static $fix_table_case_on_build = true; + /** * Instance of the database controller this schema belongs to * @@ -151,8 +159,8 @@ abstract class DBSchemaManager { } } } finally { - $this->schemaUpdateTransaction = null; - $this->schemaIsUpdating = false; + $this->schemaUpdateTransaction = null; + $this->schemaIsUpdating = false; } } @@ -305,7 +313,10 @@ abstract class DBSchemaManager { $this->transCreateTable($table, $options, $extensions); $this->alterationMessage("Table $table: created", "created"); } else { - if (Config::inst()->get('SilverStripe\ORM\Connect\DBSchemaManager', 'check_and_repair_on_build')) { + if (Config::inst()->get(static::class, 'fix_table_case_on_build')) { + $this->fixTableCase($table); + } + if (Config::inst()->get(static::class, 'check_and_repair_on_build')) { $this->checkAndRepairTable($table); } @@ -778,6 +789,37 @@ abstract class DBSchemaManager { */ abstract public function checkAndRepairTable($tableName); + + /** + * Ensure the given table has the correct case + * + * @param string $tableName Name of table in desired case + */ + public function fixTableCase($tableName) + { + // Check if table exists + $tables = $this->tableList(); + if (!array_key_exists(strtolower($tableName), $tables)) { + return; + } + + // Check if case differs + $currentName = $tables[strtolower($tableName)]; + if ($currentName === $tableName) { + return; + } + + $this->alterationMessage( + "Table $tableName: renamed from $currentName", + "repaired" + ); + + // Rename via temp table to avoid case-sensitivity issues + $tempTable = "__TEMP__{$tableName}"; + $this->renameTable($currentName, $tempTable); + $this->renameTable($tempTable, $tableName); + } + /** * Returns the values of the given enum field * diff --git a/ORM/Connect/MySQLSchemaManager.php b/ORM/Connect/MySQLSchemaManager.php index 714e619dc..e8a3fa2b6 100644 --- a/ORM/Connect/MySQLSchemaManager.php +++ b/ORM/Connect/MySQLSchemaManager.php @@ -144,24 +144,14 @@ class MySQLSchemaManager extends DBSchemaManager { } // Perform check - if (!$this->runTableCheckCommand("CHECK TABLE \"$tableName\"")) { - if ($this->runTableCheckCommand("CHECK TABLE \"" . strtolower($tableName) . "\"")) { - $this->alterationMessage( - "Table $tableName: renamed from lowercase", - "repaired" - ); - $this->renameTable(strtolower($tableName), $tableName); - return true; - } - - $this->alterationMessage( - "Table $tableName: repaired", - "repaired" - ); - return $this->runTableCheckCommand("REPAIR TABLE \"$tableName\" USE_FRM"); - } else { + if ($this->runTableCheckCommand("CHECK TABLE \"$tableName\"")) { return true; } + $this->alterationMessage( + "Table $tableName: repaired", + "repaired" + ); + return $this->runTableCheckCommand("REPAIR TABLE \"$tableName\" USE_FRM"); } /** diff --git a/ORM/DataObjectSchema.php b/ORM/DataObjectSchema.php index ba43be29d..21d5ad1e0 100644 --- a/ORM/DataObjectSchema.php +++ b/ORM/DataObjectSchema.php @@ -226,7 +226,7 @@ class DataObjectSchema { return $class; } - // If there is no class for this table, strip table modifiers (e.g. _Live / _versions) + // If there is no class for this table, strip table modifiers (e.g. _Live / _Versions) // from the end and re-attempt a search. if(preg_match('/^(?.+)(_[^_]+)$/i', $table, $matches)) { $table = $matches['class']; diff --git a/ORM/FieldType/DBClassName.php b/ORM/FieldType/DBClassName.php index b6f07b269..fdf95f976 100644 --- a/ORM/FieldType/DBClassName.php +++ b/ORM/FieldType/DBClassName.php @@ -119,7 +119,7 @@ class DBClassName extends DBEnum { */ public function getEnum() { $classNames = ClassInfo::subclassesFor($this->getBaseClass()); - unset($classNames['SilverStripe\ORM\DataObject']); + unset($classNames[DataObject::class]); return $classNames; } diff --git a/ORM/Versioning/Versioned.php b/ORM/Versioning/Versioned.php index 3b9c6daba..d3569492e 100644 --- a/ORM/Versioning/Versioned.php +++ b/ORM/Versioning/Versioned.php @@ -103,7 +103,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { /** * Additional database columns for the new - * "_versions" table. Used in {@link augmentDatabase()} + * "_Versions" table. Used in {@link augmentDatabase()} * and all Versioned calls extending or creating * SELECT statements. * @@ -135,7 +135,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { /** * Additional database indexes for the new - * "_versions" table. Used in {@link augmentDatabase()}. + * "_Versions" table. Used in {@link augmentDatabase()}. * * @var array $indexes_for_versions_table */ @@ -298,7 +298,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { } // Build query - $table = "\"{$baseTable}_versions\""; + $table = "\"{$baseTable}_Versions\""; $query = SQLSelect::create('"LastEdited"', $table) ->addWhere([ "{$table}.\"RecordID\"" => $id, @@ -428,26 +428,26 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { // Make sure join includes version as well $query->setJoinFilter( $alias, - "\"{$alias}_versions\".\"RecordID\" = \"{$baseTable}_versions\".\"RecordID\"" - . " AND \"{$alias}_versions\".\"Version\" = \"{$baseTable}_versions\".\"Version\"" + "\"{$alias}_Versions\".\"RecordID\" = \"{$baseTable}_Versions\".\"RecordID\"" + . " AND \"{$alias}_Versions\".\"Version\" = \"{$baseTable}_Versions\".\"Version\"" ); } - $query->renameTable($alias, $alias . '_versions'); + $query->renameTable($alias, $alias . '_Versions'); } - // Add all _versions columns - foreach(Config::inst()->get('SilverStripe\ORM\Versioning\Versioned', 'db_for_versions_table') as $name => $type) { - $query->selectField(sprintf('"%s_versions"."%s"', $baseTable, $name), $name); + // Add all _Versions columns + foreach(Config::inst()->get(static::class, 'db_for_versions_table') as $name => $type) { + $query->selectField(sprintf('"%s_Versions"."%s"', $baseTable, $name), $name); } // Alias the record ID as the row ID, and ensure ID filters are aliased correctly - $query->selectField("\"{$baseTable}_versions\".\"RecordID\"", "ID"); - $query->replaceText("\"{$baseTable}_versions\".\"ID\"", "\"{$baseTable}_versions\".\"RecordID\""); + $query->selectField("\"{$baseTable}_Versions\".\"RecordID\"", "ID"); + $query->replaceText("\"{$baseTable}_Versions\".\"ID\"", "\"{$baseTable}_Versions\".\"RecordID\""); // However, if doing count, undo rewrite of "ID" column $query->replaceText( - "count(DISTINCT \"{$baseTable}_versions\".\"RecordID\")", - "count(DISTINCT \"{$baseTable}_versions\".\"ID\")" + "count(DISTINCT \"{$baseTable}_Versions\".\"RecordID\")", + "count(DISTINCT \"{$baseTable}_Versions\".\"ID\")" ); // Add additional versioning filters @@ -459,16 +459,16 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { } // Link to the version archived on that date $query->addWhere([ - "\"{$baseTable}_versions\".\"Version\" IN + "\"{$baseTable}_Versions\".\"Version\" IN (SELECT LatestVersion FROM (SELECT - \"{$baseTable}_versions\".\"RecordID\", - MAX(\"{$baseTable}_versions\".\"Version\") AS LatestVersion - FROM \"{$baseTable}_versions\" - WHERE \"{$baseTable}_versions\".\"LastEdited\" <= ? - GROUP BY \"{$baseTable}_versions\".\"RecordID\" - ) AS \"{$baseTable}_versions_latest\" - WHERE \"{$baseTable}_versions_latest\".\"RecordID\" = \"{$baseTable}_versions\".\"RecordID\" + \"{$baseTable}_Versions\".\"RecordID\", + MAX(\"{$baseTable}_Versions\".\"Version\") AS LatestVersion + FROM \"{$baseTable}_Versions\" + WHERE \"{$baseTable}_Versions\".\"LastEdited\" <= ? + GROUP BY \"{$baseTable}_Versions\".\"RecordID\" + ) AS \"{$baseTable}_Versions_Latest\" + WHERE \"{$baseTable}_Versions_Latest\".\"RecordID\" = \"{$baseTable}_Versions\".\"RecordID\" )" => $date ]); break; @@ -477,15 +477,15 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { // Return latest version instances, regardless of whether they are on a particular stage // This provides "show all, including deleted" functonality $query->addWhere( - "\"{$baseTable}_versions\".\"Version\" IN + "\"{$baseTable}_Versions\".\"Version\" IN (SELECT LatestVersion FROM (SELECT - \"{$baseTable}_versions\".\"RecordID\", - MAX(\"{$baseTable}_versions\".\"Version\") AS LatestVersion - FROM \"{$baseTable}_versions\" - GROUP BY \"{$baseTable}_versions\".\"RecordID\" - ) AS \"{$baseTable}_versions_latest\" - WHERE \"{$baseTable}_versions_latest\".\"RecordID\" = \"{$baseTable}_versions\".\"RecordID\" + \"{$baseTable}_Versions\".\"RecordID\", + MAX(\"{$baseTable}_Versions\".\"Version\") AS LatestVersion + FROM \"{$baseTable}_Versions\" + GROUP BY \"{$baseTable}_Versions\".\"RecordID\" + ) AS \"{$baseTable}_Versions_Latest\" + WHERE \"{$baseTable}_Versions_Latest\".\"RecordID\" = \"{$baseTable}_Versions\".\"RecordID\" )" ); break; @@ -497,14 +497,14 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { throw new InvalidArgumentException("Invalid version"); } $query->addWhere([ - "\"{$baseTable}_versions\".\"Version\"" => $version + "\"{$baseTable}_Versions\".\"Version\"" => $version ]); break; } case 'all_versions': default: { // If all versions are requested, ensure that records are sorted by this field - $query->addOrderBy(sprintf('"%s_versions"."%s"', $baseTable, 'Version')); + $query->addOrderBy(sprintf('"%s_Versions"."%s"', $baseTable, 'Version')); break; } } @@ -556,7 +556,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { // The VersionedMode local variable ensures that this decorator only applies to // queries that have originated from the Versioned object, and have the Versioned // metadata set on the query object. This prevents regular queries from - // accidentally querying the *_versions tables. + // accidentally querying the *_Versions tables. $versionedMode = $dataObject->getSourceQueryParam('Versioned.mode'); $modesToAllowVersioning = array('all_versions', 'latest_versions', 'archive', 'version'); if( @@ -634,17 +634,17 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { DB::require_table($liveTable, $fields, $indexes, false, $options); } - // Build _versions table + // Build _Versions table //Unique indexes will not work on versioned tables, so we'll convert them to standard indexes: $nonUniqueIndexes = $this->uniqueToIndex($indexes); if($isRootClass) { // Create table for all versions $versionFields = array_merge( - Config::inst()->get('SilverStripe\ORM\Versioning\Versioned', 'db_for_versions_table'), + Config::inst()->get(static::class, 'db_for_versions_table'), (array)$fields ); $versionIndexes = array_merge( - Config::inst()->get('SilverStripe\ORM\Versioning\Versioned', 'indexes_for_versions_table'), + Config::inst()->get(static::class, 'indexes_for_versions_table'), (array)$nonUniqueIndexes ); } else { @@ -667,12 +667,12 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { } // Cleanup any orphans - $this->cleanupVersionedOrphans("{$suffixBaseTable}_versions", "{$suffixTable}_versions"); + $this->cleanupVersionedOrphans("{$suffixBaseTable}_Versions", "{$suffixTable}_Versions"); // Build versions table - DB::require_table("{$suffixTable}_versions", $versionFields, $versionIndexes, true, $options); + DB::require_table("{$suffixTable}_Versions", $versionFields, $versionIndexes, true, $options); } else { - DB::dont_require_table("{$suffixTable}_versions"); + DB::dont_require_table("{$suffixTable}_Versions"); if($this->hasStages()) { $liveTable = $this->stageTable($suffixTable, static::LIVE); DB::dont_require_table($liveTable); @@ -682,7 +682,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { } /** - * Cleanup orphaned records in the _versions table + * Cleanup orphaned records in the _Versions table * * @param string $baseTable base table to use as authoritative source of records * @param string $childTable Sub-table to clean orphans from @@ -766,7 +766,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { $baseDataClass = $schema->baseDataClass($class); $baseDataTable = $schema->tableName($baseDataClass); - // Set up a new entry in (table)_versions + // Set up a new entry in (table)_Versions $newManipulation = array( "command" => "insert", "fields" => isset($manipulation[$table]['fields']) ? $manipulation[$table]['fields'] : [], @@ -798,7 +798,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { $nextVersion = 0; if($recordID) { $nextVersion = DB::prepared_query("SELECT MAX(\"Version\") + 1 - FROM \"{$baseDataTable}_versions\" WHERE \"RecordID\" = ?", + FROM \"{$baseDataTable}_Versions\" WHERE \"RecordID\" = ?", array($recordID) )->value(); } @@ -813,9 +813,9 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { $manipulation[$table]['fields']['Version'] = $nextVersion; } - // Update _versions table manipulation + // Update _Versions table manipulation $newManipulation['fields']['Version'] = $nextVersion; - $manipulation["{$table}_versions"] = $newManipulation; + $manipulation["{$table}_Versions"] = $newManipulation; } /** @@ -1024,7 +1024,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { $lookup = array(); foreach(ClassInfo::subclassesFor('SilverStripe\ORM\DataObject') as $class) { // Ensure this class is versioned - if(!Object::has_extension($class, 'SilverStripe\ORM\Versioning\Versioned')) { + if(!Object::has_extension($class, static::class)) { continue; } @@ -1378,7 +1378,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { /** * Determine if a class is supporting the Versioned extensions (e.g. - * $table_versions does exists). + * $table_Versions does exists). * * @param string $class Class name * @return boolean @@ -1714,7 +1714,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { // Mark this version as having been published at some stage $publisherID = isset(Member::currentUser()->ID) ? Member::currentUser()->ID : 0; $extTable = $this->extendWithSuffix($baseTable); - DB::prepared_query("UPDATE \"{$extTable}_versions\" + DB::prepared_query("UPDATE \"{$extTable}_Versions\" SET \"WasPublished\" = ?, \"PublisherID\" = ? WHERE \"RecordID\" = ? AND \"Version\" = ?", array(1, $publisherID, $from->ID, $from->Version) @@ -1827,23 +1827,23 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { $baseTable = str_replace('"','',$tableJoin); } elseif(is_string($tableJoin) && substr($tableJoin,0,5) != 'INNER') { $query->setFrom(array( - $table => "LEFT JOIN \"$table\" ON \"$table\".\"RecordID\"=\"{$baseTable}_versions\".\"RecordID\"" - . " AND \"$table\".\"Version\" = \"{$baseTable}_versions\".\"Version\"" + $table => "LEFT JOIN \"$table\" ON \"$table\".\"RecordID\"=\"{$baseTable}_Versions\".\"RecordID\"" + . " AND \"$table\".\"Version\" = \"{$baseTable}_Versions\".\"Version\"" )); } - $query->renameTable($table, $table . '_versions'); + $query->renameTable($table, $table . '_Versions'); } - // Add all _versions columns - foreach(Config::inst()->get('SilverStripe\ORM\Versioning\Versioned', 'db_for_versions_table') as $name => $type) { - $query->selectField(sprintf('"%s_versions"."%s"', $baseTable, $name), $name); + // Add all _Versions columns + foreach(Config::inst()->get(static::class, 'db_for_versions_table') as $name => $type) { + $query->selectField(sprintf('"%s_Versions"."%s"', $baseTable, $name), $name); } $query->addWhere(array( - "\"{$baseTable}_versions\".\"RecordID\" = ?" => $owner->ID + "\"{$baseTable}_Versions\".\"RecordID\" = ?" => $owner->ID )); $query->setOrderBy(($sort) ? $sort - : "\"{$baseTable}_versions\".\"LastEdited\" DESC, \"{$baseTable}_versions\".\"Version\" DESC"); + : "\"{$baseTable}_Versions\".\"LastEdited\" DESC, \"{$baseTable}_Versions\".\"Version\" DESC"); $records = $query->execute(); $versions = new ArrayList(); @@ -2122,7 +2122,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { * @param array $idList */ public static function prepopulate_versionnumber_cache($class, $stage, $idList = null) { - if (!Config::inst()->get('SilverStripe\ORM\Versioning\Versioned', 'prepopulate_versionnumber_cache')) { + if (!Config::inst()->get(static::class, 'prepopulate_versionnumber_cache')) { return; } $filter = ""; @@ -2363,7 +2363,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { /** * Return the equivalent of a DataList::create() call, querying the latest - * version of each record stored in the (class)_versions tables. + * version of each record stored in the (class)_Versions tables. * * In particular, this will query deleted records as well as active ones. * @@ -2502,7 +2502,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { $itemKey = get_class($item) . '/' . $item->ID; // Write if saved, versioned, and not already added - if ($item->isInDB() && $item->has_extension('SilverStripe\ORM\Versioning\Versioned') && !isset($list[$itemKey])) { + if ($item->isInDB() && $item->has_extension(static::class) && !isset($list[$itemKey])) { $list[$itemKey] = $item; $added[$itemKey] = $item; } diff --git a/docs/en/04_Changelogs/4.0.0.md b/docs/en/04_Changelogs/4.0.0.md index 764c7e444..3223f6eb6 100644 --- a/docs/en/04_Changelogs/4.0.0.md +++ b/docs/en/04_Changelogs/4.0.0.md @@ -897,6 +897,8 @@ A very small number of methods were chosen for deprecation, and will be removed * Any writes to versioned dataobjects (in either Draft or Live) will always write to the draft (main) table as a source of truth. This means that records created in Live mode will always be available to the CMS and in draft mode. + * `_versions` suffixed tables are now renamed to `_Versions`. This fix will be automatically + applied during dev/build. * A lot of standard versioned API has been refactored from `SiteTree` into `Versioned` extension. * All versioned DataObjects have `canPublish()`, `canArchive()`, `canUnpublish()` permission checks * All versioned Dataobjects have `doPublish()`, `doArchive()`, `doPublish()`, and `doUnpublish()` actions. diff --git a/tests/model/DBClassNameTest.php b/tests/model/DBClassNameTest.php index e4bcc787b..62c54cd0a 100644 --- a/tests/model/DBClassNameTest.php +++ b/tests/model/DBClassNameTest.php @@ -86,7 +86,7 @@ class DBClassNameTest extends SapphireTest { // Implicit table $field4 = new DBClassName('MyClass'); - $field4->setTable('DBClassNameTest_ObjectSubClass_versions'); + $field4->setTable('DBClassNameTest_ObjectSubClass_Versions'); $this->assertEquals('DBClassNameTest_Object', $field4->getBaseClass()); $this->assertEquals('DBClassNameTest_Object', $field4->getDefault()); diff --git a/tests/model/DataObjectLazyLoadingTest.php b/tests/model/DataObjectLazyLoadingTest.php index e7addc38d..1b4346e41 100644 --- a/tests/model/DataObjectLazyLoadingTest.php +++ b/tests/model/DataObjectLazyLoadingTest.php @@ -376,12 +376,12 @@ class DataObjectLazyLoadingTest extends SapphireTest { // Force inconsistent state to test behaviour (shouldn't select from *_versions) DB::query(sprintf( - "UPDATE \"VersionedLazy_DataObject_versions\" SET \"PageName\" = 'versioned-value' " . + "UPDATE \"VersionedLazy_DataObject_Versions\" SET \"PageName\" = 'versioned-value' " . "WHERE \"RecordID\" = %d", $obj1ID )); DB::query(sprintf( - "UPDATE \"VersionedLazySub_DataObject_versions\" SET \"ExtraField\" = 'versioned-value' " . + "UPDATE \"VersionedLazySub_DataObject_Versions\" SET \"ExtraField\" = 'versioned-value' " . "WHERE \"RecordID\" = %d", $obj1ID )); diff --git a/tests/model/DataObjectSchemaGenerationTest.php b/tests/model/DataObjectSchemaGenerationTest.php index 8c0a3439a..b89413663 100644 --- a/tests/model/DataObjectSchemaGenerationTest.php +++ b/tests/model/DataObjectSchemaGenerationTest.php @@ -27,6 +27,39 @@ class DataObjectSchemaGenerationTest extends SapphireTest { parent::setUpOnce(); } + public function testTableCaseFixed() { + DB::quiet(); + + // Modify table case + DB::get_schema()->renameTable( + 'DataObjectSchemaGenerationTest_DO', + '__TEMP__DataOBJECTSchemaGenerationTest_do' + ); + DB::get_schema()->renameTable( + '__TEMP__DataOBJECTSchemaGenerationTest_do', + 'DataOBJECTSchemaGenerationTest_do' + ); + + // Check table + $tables = DB::table_list(); + $this->assertEquals( + 'DataOBJECTSchemaGenerationTest_do', + $tables['dataobjectschemagenerationtest_do'] + ); + + // Rebuild table + DB::get_schema()->schemaUpdate(function() { + DataObjectSchemaGenerationTest_DO::singleton()->requireTable(); + }); + + // Check table + $tables = DB::table_list(); + $this->assertEquals( + 'DataObjectSchemaGenerationTest_DO', + $tables['dataobjectschemagenerationtest_do'] + ); + } + /** * Check that once a schema has been generated, then it doesn't need any more updating */ diff --git a/tests/model/VersionedTest.php b/tests/model/VersionedTest.php index cd21d0c22..218097572 100644 --- a/tests/model/VersionedTest.php +++ b/tests/model/VersionedTest.php @@ -52,8 +52,8 @@ class VersionedTest extends SapphireTest { $tableExpectations = array( 'VersionedTest_WithIndexes' => array('value' => true, 'message' => 'Unique indexes are unique in main table'), - 'VersionedTest_WithIndexes_versions' => - array('value' => false, 'message' => 'Unique indexes are no longer unique in _versions table'), + 'VersionedTest_WithIndexes_Versions' => + array('value' => false, 'message' => 'Unique indexes are no longer unique in _Versions table'), 'VersionedTest_WithIndexes_Live' => array('value' => true, 'message' => 'Unique indexes are unique in _Live table'), ); @@ -93,22 +93,22 @@ class VersionedTest extends SapphireTest { $obj->write(); $obj->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $versions = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\"" + $versions = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_Versions\"" . " WHERE \"RecordID\" = '$obj->ID'")->value(); $this->assertGreaterThan(0, $versions, 'At least 1 version exists in the history of the page'); // Force orphaning of all versions created earlier, only on parent record. // The child versiones table should still have the correct relationship - DB::query("DELETE FROM \"VersionedTest_DataObject_versions\" WHERE \"RecordID\" = $obj->ID"); + DB::query("DELETE FROM \"VersionedTest_DataObject_Versions\" WHERE \"RecordID\" = $obj->ID"); // insert a record with no primary key (ID) - DB::query("INSERT INTO \"VersionedTest_DataObject_versions\" (\"RecordID\") VALUES ($obj->ID)"); + DB::query("INSERT INTO \"VersionedTest_DataObject_Versions\" (\"RecordID\") VALUES ($obj->ID)"); // run the script which should clean that up $obj->augmentDatabase(); - $versions = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\"" + $versions = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_Versions\"" . " WHERE \"RecordID\" = '$obj->ID'")->value(); $this->assertEquals(0, $versions, 'Orphaned versions on child tables are removed'); @@ -116,11 +116,11 @@ class VersionedTest extends SapphireTest { $obj->write(); $obj->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $count = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\"" + $count = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_Versions\"" . " WHERE \"RecordID\" = '$obj->ID'")->value(); $obj->augmentDatabase(); - $count2 = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\"" + $count2 = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_Versions\"" . " WHERE \"RecordID\" = '$obj->ID'")->value(); $this->assertEquals($count, $count2); @@ -309,15 +309,15 @@ class VersionedTest extends SapphireTest { $this->assertEquals('orig', $page1->Content, 'Copies the content from the old version'); // check db entries - $version = DB::prepared_query("SELECT MAX(\"Version\") FROM \"VersionedTest_DataObject_versions\" WHERE \"RecordID\" = ?", + $version = DB::prepared_query("SELECT MAX(\"Version\") FROM \"VersionedTest_DataObject_Versions\" WHERE \"RecordID\" = ?", array($page1->ID) )->value(); - $this->assertEquals($page1->Version, $version, 'Correct entry in VersionedTest_DataObject_versions'); + $this->assertEquals($page1->Version, $version, 'Correct entry in VersionedTest_DataObject_Versions'); - $version = DB::prepared_query("SELECT MAX(\"Version\") FROM \"VersionedTest_AnotherSubclass_versions\" WHERE \"RecordID\" = ?", + $version = DB::prepared_query("SELECT MAX(\"Version\") FROM \"VersionedTest_AnotherSubclass_Versions\" WHERE \"RecordID\" = ?", array($page1->ID) )->value(); - $this->assertEquals($page1->Version, $version, 'Correct entry in VersionedTest_AnotherSubclass_versions'); + $this->assertEquals($page1->Version, $version, 'Correct entry in VersionedTest_AnotherSubclass_Versions'); } public function testDeleteFromStage() { @@ -884,7 +884,7 @@ class VersionedTest extends SapphireTest { protected function assertRecordHasLatestVersion($record, $version) { foreach(ClassInfo::ancestry(get_class($record), true) as $table) { $versionForClass = DB::prepared_query( - $sql = "SELECT MAX(\"Version\") FROM \"{$table}_versions\" WHERE \"RecordID\" = ?", + $sql = "SELECT MAX(\"Version\") FROM \"{$table}_Versions\" WHERE \"RecordID\" = ?", array($record->ID) )->value(); $this->assertEquals($version, $versionForClass, "That the table $table has the latest version $version");