Skip to content

Commit

Permalink
MDL-76459 xmldb: Add environmental check to verify $CFG->prefix
Browse files Browse the repository at this point in the history
While, right now, sites using long (> 10 chars) $CFG->prefix
can continue working (because we still don't have any table
> 28 chars), as soon as some new table with long name is added,
it won't work with PostgreSQL anymore (if the 63 limit is raised).

Hence, this environmental check will verify on both install and
upgrade that the $CFG->prefix is always <= 10 chars.

Sites with longer prefixes will need to rename all their tables
(and maybe other objects, depending on the dbtype) to use a shorter
prefix.
  • Loading branch information
stronk7 committed Sep 2, 2023
1 parent 9b652a2 commit 63111e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions admin/environment.xml
Expand Up @@ -4301,6 +4301,8 @@
</CUSTOM_CHECK>
<CUSTOM_CHECK file="lib/upgradelib.php" function="check_mod_assignment" level="required">
</CUSTOM_CHECK>
<CUSTOM_CHECK file="lib/upgradelib.php" function="check_db_prefix_length" level="required">
</CUSTOM_CHECK>
</CUSTOM_CHECKS>
</MOODLE>
</COMPATIBILITY_MATRIX>
1 change: 1 addition & 0 deletions lang/en/admin.php
Expand Up @@ -459,6 +459,7 @@
$string['datarootsecurityerror'] = '<p><strong>SECURITY WARNING!</strong></p><p>Your dataroot directory is in the wrong location and is exposed to the web. This means that all your private files are available to anyone in the world, and some of them could be used by a cracker to obtain unauthorised administrative access to your site!</p>
<p>You <em>must</em> move dataroot directory ({$a}) to a new location that is not within your public web directory, and update the <code>$CFG->dataroot</code> setting in your config.php accordingly.</p>';
$string['datarootsecuritywarning'] = 'Your site configuration might not be secure. Please make sure that your dataroot directory ({$a}) is not directly accessible via web.';
$string['dbprefixtoolong'] = 'Your site\'s database prefix ($CFG->prefix) is too long ({$a->current} characters). The maximum number of characters allowed is {$a->maximum}.';
$string['dbsessions'] = 'Use database for session information';
$string['debug'] = 'Debug messages';
$string['debugall'] = 'ALL: Show all reasonable PHP debug messages';
Expand Down
20 changes: 20 additions & 0 deletions lib/upgradelib.php
Expand Up @@ -2487,6 +2487,26 @@ function check_igbinary322_version(environment_results $result) {
return $result;
}

/**
* This function checks that the database prefix ($CFG->prefix) is <= 10
*
* @param environment_results $result
* @return environment_results|null updated results object, or null if the prefix check is passing ok.
*/
function check_db_prefix_length(environment_results $result) {
global $CFG;

$prefixlen = strlen($CFG->prefix) ?? 0;
if ($prefixlen > 10) {
$parameters = (object)['current' => $prefixlen, 'maximum' => 10];
$result->setFeedbackStr(['dbprefixtoolong', 'admin', $parameters]);
$result->setInfo('db prefix too long');
$result->setStatus(false);
return $result;
}
return null; // All, good. By returning null we hide the check.
}

/**
* Assert the upgrade key is provided, if it is defined.
*
Expand Down

0 comments on commit 63111e4

Please sign in to comment.