Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API rescue master-branch PR: Shorten auto-generated table names #7621 #10454

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/ORM/Connect/DBSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ abstract class DBSchemaManager
/**
* @param string $table
* @param string $class
*
* @deprecated 4.0.0:5.0.0
*/
public static function showTableNameWarning($table, $class)
{
Expand Down Expand Up @@ -436,11 +434,10 @@ public function requireTable(
if (!$table_name_info_sent) {
$this->alterationMessage(
<<<'MESSAGE'
<strong>Please note:</strong> It is strongly recommended to define a
table_name for all namespaced models. Not defining a table_name may cause generated table
names to be too long and may not be supported by your current database engine. The generated
naming scheme will also change when upgrading to SilverStripe 5.0 and potentially break.
MESSAGE
<strong>Please note:</strong> It is strongly recommended to define a
table_name for all namespaced models. Not defining a table_name may cause generated table
names to be too long and may not be supported by your current database engine.
MESSAGE
emteknetnz marked this conversation as resolved.
Show resolved Hide resolved
,
'error'
);
Expand Down
21 changes: 21 additions & 0 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3623,13 +3623,34 @@ public function requireTable()
$fields = $schema->databaseFields(static::class, false);
$indexes = $schema->databaseIndexes(static::class, false);
$extensions = self::database_extensions(static::class);
$legacyTables = $schema->getLegacyTableNames(static::class);

if (empty($table)) {
throw new LogicException(
"Class " . static::class . " not loaded by manifest, or no database table configured"
);
}

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) {
$hasAutoIncPK = get_parent_class($this ?? '') === self::class;
DB::require_table(
Expand Down
30 changes: 27 additions & 3 deletions src/ORM/DataObjectSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,25 +318,49 @@ protected function buildTableName($class)
{
$table = Config::inst()->get($class, 'table_name', Config::UNINHERITED);

// Generate default table name
// Return table name if configured
if ($table) {
return $table;
}

// Use classname directly if not namespaced
if (strpos($class ?? '', '\\') === false) {
return $class;
}

$separator = DataObjectSchema::config()->uninherited('table_namespace_separator');
$table = str_replace('\\', $separator ?? '', trim($class ?? '', '\\'));
// Attempt to generate a nice table name
$separator = DataObjectSchema::config()->uninherited('table_namespace_separator') ?? '';
$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)) {
DBSchemaManager::showTableNameWarning($table, $class);
}

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.
* "ID" will be included on every table.
Expand Down
4 changes: 2 additions & 2 deletions tests/php/ORM/DataObjectSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public function testTableName()
'DOSTWithCustomTable',
$schema->tableName(WithCustomTable::class)
);
// Default table name is FQN
// Default table name is Vendor plus base class
$this->assertEquals(
'SilverStripe_ORM_Tests_DataObjectSchemaTest_DefaultTableName',
'SilverStripe_DefaultTableName',
$schema->tableName(DefaultTableName::class)
);
}
Expand Down