mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API Revert shorten auto-generated table names (#10482)
* Revert "BUG Fix table name test" This reverts commitb36a01a8fd
. * Revert "ENH shorten auto-generated table names" This reverts commit156f63bce3
.
This commit is contained in:
parent
e6fb9634b5
commit
1bf86cf39f
@ -3633,7 +3633,6 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
$fields = $schema->databaseFields(static::class, false);
|
$fields = $schema->databaseFields(static::class, false);
|
||||||
$indexes = $schema->databaseIndexes(static::class, false);
|
$indexes = $schema->databaseIndexes(static::class, false);
|
||||||
$extensions = self::database_extensions(static::class);
|
$extensions = self::database_extensions(static::class);
|
||||||
$legacyTables = $schema->getLegacyTableNames(static::class);
|
|
||||||
|
|
||||||
if (empty($table)) {
|
if (empty($table)) {
|
||||||
throw new LogicException(
|
throw new LogicException(
|
||||||
@ -3641,26 +3640,6 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($legacyTables) {
|
|
||||||
$ignore = Config::inst()->get(static::class, 'ignored_legacy_tables') ?: [];
|
|
||||||
$renameTables = array_diff(
|
|
||||||
array_intersect($legacyTables, DB::table_list()),
|
|
||||||
$ignore
|
|
||||||
);
|
|
||||||
if (count($renameTables) > 1) {
|
|
||||||
$class = static::class;
|
|
||||||
$legacyList = implode(', ', $renameTables);
|
|
||||||
trigger_error(
|
|
||||||
"Class $class has multiple legacy tables: $legacyList",
|
|
||||||
E_USER_NOTICE
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (count($renameTables) === 1) {
|
|
||||||
$dbSchema = DB::get_schema();
|
|
||||||
$dbSchema->renameTable($renameTables[0], $table);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($fields) {
|
if ($fields) {
|
||||||
$hasAutoIncPK = get_parent_class($this ?? '') === self::class;
|
$hasAutoIncPK = get_parent_class($this ?? '') === self::class;
|
||||||
DB::require_table(
|
DB::require_table(
|
||||||
|
@ -318,30 +318,18 @@ class DataObjectSchema
|
|||||||
{
|
{
|
||||||
$table = Config::inst()->get($class, 'table_name', Config::UNINHERITED);
|
$table = Config::inst()->get($class, 'table_name', Config::UNINHERITED);
|
||||||
|
|
||||||
// Return table name if configured
|
// Generate default table name
|
||||||
if ($table) {
|
if ($table) {
|
||||||
return $table;
|
return $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use classname directly if not namespaced
|
|
||||||
if (strpos($class ?? '', '\\') === false) {
|
if (strpos($class ?? '', '\\') === false) {
|
||||||
return $class;
|
return $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to generate a nice table name
|
$separator = DataObjectSchema::config()->uninherited('table_namespace_separator');
|
||||||
$separator = DataObjectSchema::config()->uninherited('table_namespace_separator') ?? '';
|
$table = str_replace('\\', $separator ?? '', trim($class ?? '', '\\'));
|
||||||
$parts = explode('\\', trim($class ?? '', '\\'));
|
|
||||||
$vendor = array_slice($parts, 0, 1)[0];
|
|
||||||
$base = array_slice($parts, -1, 1)[0];
|
|
||||||
if ($vendor && $base && $vendor !== $base) {
|
|
||||||
$table = "{$vendor}{$separator}{$base}";
|
|
||||||
} elseif ($base) {
|
|
||||||
$table = $base;
|
|
||||||
} else {
|
|
||||||
throw new InvalidArgumentException("Unable to build a table name for class '$class'. Define a table_name for this class.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Display a warning about namespaced classes producing long table names
|
|
||||||
if (!ClassInfo::classImplements($class, TestOnly::class) && $this->classHasTable($class)) {
|
if (!ClassInfo::classImplements($class, TestOnly::class) && $this->classHasTable($class)) {
|
||||||
DBSchemaManager::showTableNameWarning($table, $class);
|
DBSchemaManager::showTableNameWarning($table, $class);
|
||||||
}
|
}
|
||||||
@ -349,18 +337,6 @@ class DataObjectSchema
|
|||||||
return $table;
|
return $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $class
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getLegacyTableNames($class)
|
|
||||||
{
|
|
||||||
$separator = DataObjectSchema::config()->uninherited('table_namespace_separator');
|
|
||||||
$names[] = str_replace('\\', $separator, trim($class, '\\'));
|
|
||||||
|
|
||||||
return $names;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the complete map of fields to specification on this object, including fixed_fields.
|
* Return the complete map of fields to specification on this object, including fixed_fields.
|
||||||
* "ID" will be included on every table.
|
* "ID" will be included on every table.
|
||||||
|
@ -58,9 +58,9 @@ class DataObjectSchemaTest extends SapphireTest
|
|||||||
'DOSTWithCustomTable',
|
'DOSTWithCustomTable',
|
||||||
$schema->tableName(WithCustomTable::class)
|
$schema->tableName(WithCustomTable::class)
|
||||||
);
|
);
|
||||||
// Default table name is Vendor plus base class
|
// Default table name is FQN
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'SilverStripe_DefaultTableName',
|
'SilverStripe_ORM_Tests_DataObjectSchemaTest_DefaultTableName',
|
||||||
$schema->tableName(DefaultTableName::class)
|
$schema->tableName(DefaultTableName::class)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user