Commit d5d8b306 authored by alexpott's avatar alexpott

Issue #2451395 by dawehner, catch, fgm, David_Rothstein:...

Issue #2451395 by dawehner, catch, fgm, David_Rothstein: drupal_get_schema()/drupal_get_complete_schema() no longer work as expected; remove them
parent 7c62cee5
......@@ -1333,14 +1333,6 @@ function drupal_flush_all_caches() {
// actually loaded.
$module_handler->loadAll();
// Rebuild the schema and cache a fully-built schema based on new module data.
// This is necessary for any invocation of index.php, because setting cache
// table entries requires schema information and that occurs during bootstrap
// before any modules are loaded, so if there is no cached schema,
// drupal_get_schema() will try to generate one, but with no loaded modules,
// it will return nothing.
drupal_get_schema(NULL, TRUE);
// Rebuild all information based on new module data.
$module_handler->invokeAll('rebuild');
......
......@@ -5,7 +5,6 @@
* Schema API handling functions.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Database\Database;
/**
......@@ -18,83 +17,6 @@
*/
const SCHEMA_UNINSTALLED = -1;
/**
* Gets the schema definition of a table, or the whole database schema.
*
* The returned schema will include any modifications made by any
* module that implements hook_schema_alter().
*
* @param string $table
* The name of the table. If not given, the schema of all tables is returned.
* @param bool $rebuild
* If TRUE, the schema will be rebuilt instead of retrieved from the cache.
*/
function drupal_get_schema($table = NULL, $rebuild = FALSE) {
static $schema;
if ($rebuild || !isset($schema)) {
$schema = drupal_get_complete_schema($rebuild);
}
if (!isset($table)) {
return $schema;
}
if (isset($schema[$table])) {
return $schema[$table];
}
else {
return FALSE;
}
}
/**
* Gets the whole database schema.
*
* The returned schema will include any modifications made by any
* module that implements hook_schema_alter().
*
* @param bool $rebuild
* If TRUE, the schema will be rebuilt instead of retrieved from the cache.
*/
function drupal_get_complete_schema($rebuild = FALSE) {
static $schema;
if (!isset($schema) || $rebuild) {
// Try to load the schema from cache.
if (!$rebuild && $cached = \Drupal::cache()->get('schema')) {
$schema = $cached->data;
}
// Otherwise, rebuild the schema cache.
else {
$schema = array();
// Load the .install files to get hook_schema.
\Drupal::moduleHandler()->loadAllIncludes('install');
require_once __DIR__ . '/common.inc';
// Invoke hook_schema for all modules.
foreach (\Drupal::moduleHandler()->getImplementations('schema') as $module) {
// Cast the result of hook_schema() to an array, as a NULL return value
// would cause array_merge() to set the $schema variable to NULL as well.
// That would break modules which use $schema further down the line.
$current = (array) \Drupal::moduleHandler()->invoke($module, 'schema');
// Set 'module' and 'name' keys for each table, and remove descriptions,
// as they needlessly slow down \Drupal::cache()->get() for every single request.
_drupal_schema_initialize($current, $module);
$schema = array_merge($schema, $current);
}
\Drupal::moduleHandler()->alter('schema', $schema);
// If the schema is empty, avoid saving it: some database engines require
// the schema to perform queries, and this could lead to infinite loops.
if (!empty($schema)) {
\Drupal::cache()->set('schema', $schema, Cache::PERMANENT);
}
}
}
return $schema;
}
/**
* Returns an array of available schema versions for a module.
*
......@@ -191,15 +113,11 @@ function drupal_set_installed_schema_version($module, $version) {
/**
* Creates all tables defined in a module's hook_schema().
*
* Note: This function does not pass the module's schema through
* hook_schema_alter(). The module's tables will be created exactly as the
* module defines them.
*
* @param string $module
* The module for which the tables will be created.
*/
function drupal_install_schema($module) {
$schema = drupal_get_schema_unprocessed($module);
$schema = drupal_get_module_schema($module);
_drupal_schema_initialize($schema, $module, FALSE);
foreach ($schema as $name => $table) {
......@@ -210,10 +128,6 @@ function drupal_install_schema($module) {
/**
* Removes all tables defined in a module's hook_schema().
*
* Note: This function does not pass the module's schema through
* hook_schema_alter(). The module's tables will be created exactly as the
* module defines them.
*
* @param string $module
* The module for which the tables will be removed.
*
......@@ -224,7 +138,7 @@ function drupal_install_schema($module) {
* \Drupal\Component\Utility\SafeMarkup::checkPlain().
*/
function drupal_uninstall_schema($module) {
$schema = drupal_get_schema_unprocessed($module);
$schema = drupal_get_module_schema($module);
_drupal_schema_initialize($schema, $module, FALSE);
foreach ($schema as $table) {
......@@ -235,30 +149,19 @@ function drupal_uninstall_schema($module) {
}
/**
* Returns the unprocessed and unaltered version of a module's schema.
*
* Use this function only if you explicitly need the original
* specification of a schema, as it was defined in a module's
* hook_schema(). No additional default values will be set,
* hook_schema_alter() is not invoked and these unprocessed
* definitions won't be cached.
* Returns a module's schema.
*
* This function can be used to retrieve a schema specification in
* hook_schema(), so it allows you to derive your tables from existing
* specifications.
*
* It is also used by drupal_install_schema() and
* drupal_uninstall_schema() to ensure that a module's tables are
* created exactly as specified without any changes introduced by a
* module that implements hook_schema_alter().
*
* @param string $module
* The module to which the table belongs.
* @param string $table
* The name of the table. If not given, the module's complete schema
* is returned.
*/
function drupal_get_schema_unprocessed($module, $table = NULL) {
function drupal_get_module_schema($module, $table = NULL) {
// Load the .install file to get hook_schema.
module_load_install($module);
$schema = \Drupal::moduleHandler()->invoke($module, 'schema');
......
......@@ -424,7 +424,6 @@ protected function alterTable($table, $old_schema, $new_schema, array $mapping =
* Name of the table.
* @return
* An array representing the schema, from drupal_get_schema().
* @see drupal_get_schema()
*/
protected function introspectSchema($table) {
$mapped_fields = array_flip($this->getFieldTypeMap());
......
......@@ -474,8 +474,6 @@ function hook_query_TAG_alter(Drupal\Core\Database\Query\AlterableInterface $que
* array, the key is a table name and the value is a table structure
* definition.
*
* @see hook_schema_alter()
*
* @ingroup schemaapi
*/
function hook_schema() {
......@@ -534,31 +532,6 @@ function hook_schema() {
return $schema;
}
/**
* Perform alterations to existing database schemas.
*
* When a module modifies the database structure of another module (by
* changing, adding or removing fields, keys or indexes), it should
* implement hook_schema_alter() to update the default $schema to take its
* changes into account.
*
* See hook_schema() for details on the schema definition structure.
*
* @param $schema
* Nested array describing the schemas for all modules.
*
* @ingroup schemaapi
*/
function hook_schema_alter(&$schema) {
// Add field to existing schema.
$schema['users']['fields']['timezone_id'] = array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Per-user timezone configuration.',
);
}
/**
* @} End of "addtogroup hooks".
*/
......@@ -205,9 +205,6 @@ public function install(array $module_list, $enable_dependencies = TRUE) {
// Update the kernel to include it.
$this->updateKernel($module_filenames);
// Refresh the schema to include it.
drupal_get_schema(NULL, TRUE);
// Allow modules to react prior to the installation of a module.
$this->moduleHandler->invokeAll('module_preinstall', array($module));
......
......@@ -147,15 +147,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
// work during installation.
$form['#attached']['drupalSettings']['copyFieldValue']['edit-site-mail'] = ['edit-account-mail'];
// Cache a fully-built schema. This is necessary for any invocation of
// index.php because: (1) setting cache table entries requires schema
// information, (2) that occurs during bootstrap before any module are
// loaded, so (3) if there is no cached schema, drupal_get_schema() will
// try to generate one but with no loaded modules will return nothing.
//
// @todo Move this to the 'install_finished' task?
drupal_get_schema(NULL, TRUE);
$form['site_information'] = array(
'#type' => 'fieldgroup',
'#title' => $this->t('Site information'),
......
......@@ -552,7 +552,7 @@ function simpletest_clean_environment() {
*/
function simpletest_clean_database() {
$tables = db_find_tables(Database::getConnection()->prefixTables('{simpletest}') . '%');
$schema = drupal_get_schema_unprocessed('simpletest');
$schema = drupal_get_module_schema('simpletest');
$count = 0;
foreach (array_diff_key($tables, $schema) as $table) {
// Strip the prefix and skip tables without digits following "simpletest",
......
......@@ -405,7 +405,7 @@ protected function installConfig(array $modules) {
* found in the module specified.
*/
protected function installSchema($module, $tables) {
// drupal_get_schema_unprocessed() is technically able to install a schema
// drupal_get_module_schema() is technically able to install a schema
// of a non-enabled module, but its ability to load the module's .install
// file depends on many other factors. To prevent differences in test
// behavior and non-reproducible test failures, we only allow the schema of
......@@ -417,7 +417,7 @@ protected function installSchema($module, $tables) {
}
$tables = (array) $tables;
foreach ($tables as $table) {
$schema = drupal_get_schema_unprocessed($module, $table);
$schema = drupal_get_module_schema($module, $table);
if (empty($schema)) {
throw new \RuntimeException(format_string("Unknown '@table' table schema in '@module' module.", array(
'@module' => $module,
......@@ -426,10 +426,6 @@ protected function installSchema($module, $tables) {
}
$this->container->get('database')->schema()->createTable($table, $schema);
}
// We need to refresh the schema cache, as any call to drupal_get_schema()
// would not know of/return the schema otherwise.
// @todo Refactor Schema API to make this obsolete.
drupal_get_schema(NULL, TRUE);
$this->pass(format_string('Installed %module tables: %tables.', array(
'%tables' => '{' . implode('}, {', $tables) . '}',
'%module' => $module,
......
......@@ -108,8 +108,6 @@ function testEnableModulesInstall() {
$this->assertFalse(in_array($module, $list), "{$module}_hook_info() in \Drupal::moduleHandler()->getImplementations() not found.");
$this->assertFalse(db_table_exists($table), "'$table' database table not found.");
$schema = drupal_get_schema($table, TRUE);
$this->assertFalse($schema, "'$table' table schema not found.");
// Install the module.
\Drupal::service('module_installer')->install(array($module));
......@@ -122,7 +120,7 @@ function testEnableModulesInstall() {
$this->assertTrue(in_array($module, $list), "{$module}_hook_info() in \Drupal::moduleHandler()->getImplementations() found.");
$this->assertTrue(db_table_exists($table), "'$table' database table found.");
$schema = drupal_get_schema($table);
$schema = drupal_get_module_schema($module, $table);
$this->assertTrue($schema, "'$table' table schema found.");
}
......@@ -154,9 +152,7 @@ function testInstallSchema() {
$this->assertTrue(db_table_exists($table), "'$table' database table found.");
// Verify that the schema is known to Schema API.
$schema = drupal_get_schema();
$this->assertTrue($schema[$table], "'$table' table found in schema.");
$schema = drupal_get_schema($table);
$schema = drupal_get_module_schema($module, $table);
$this->assertTrue($schema, "'$table' table schema found.");
// Verify that a unknown table from an enabled module throws an error.
......@@ -169,7 +165,7 @@ function testInstallSchema() {
$this->pass('Exception for non-retrievable schema found.');
}
$this->assertFalse(db_table_exists($table), "'$table' database table not found.");
$schema = drupal_get_schema($table);
$schema = drupal_get_module_schema($module, $table);
$this->assertFalse($schema, "'$table' table schema not found.");
// Verify that a table from a unknown module cannot be installed.
......@@ -183,14 +179,14 @@ function testInstallSchema() {
$this->pass('Exception for non-retrievable schema found.');
}
$this->assertFalse(db_table_exists($table), "'$table' database table not found.");
$schema = drupal_get_schema($table);
$this->assertFalse($schema, "'$table' table schema not found.");
$schema = drupal_get_module_schema($module, $table);
$this->assertTrue($schema, "'$table' table schema found.");
// Verify that the same table can be installed after enabling the module.
$this->enableModules(array($module));
$this->installSchema($module, $table);
$this->assertTrue(db_table_exists($table), "'$table' database table found.");
$schema = drupal_get_schema($table);
$schema = drupal_get_module_schema($module, $table);
$this->assertTrue($schema, "'$table' table schema found.");
}
......
......@@ -23,7 +23,7 @@ function testDefaultInsert() {
$query = db_insert('test')->useDefaults(array('job'));
$id = $query->execute();
$schema = drupal_get_schema('test');
$schema = drupal_get_module_schema('database_test', 'test');
$job = db_query('SELECT job FROM {test} WHERE id = :id', array(':id' => $id))->fetchField();
$this->assertEqual($job, $schema['fields']['job']['default'], 'Default field value is set.');
......@@ -56,7 +56,7 @@ function testDefaultInsertWithFields() {
->useDefaults(array('job'));
$id = $query->execute();
$schema = drupal_get_schema('test');
$schema = drupal_get_module_schema('database_test', 'test');
$job = db_query('SELECT job FROM {test} WHERE id = :id', array(':id' => $id))->fetchField();
$this->assertEqual($job, $schema['fields']['job']['default'], 'Default field value is set.');
......
......@@ -58,6 +58,7 @@ protected function setUp() {
}
// Create the test field.
module_load_install('entity_test');
entity_test_install();
// Install required default configuration for filter module.
......
......@@ -61,6 +61,7 @@ protected function setUp() {
$this->installConfig(array('language'));
// Create the test field.
module_load_install('entity_test');
entity_test_install();
// Enable translations for the test entity type.
......
......@@ -45,6 +45,7 @@ protected function setUp() {
parent::setUp();
// Create the test field.
module_load_install('entity_test');
entity_test_install();
// Install required default configuration for filter module.
......
......@@ -59,7 +59,7 @@ function assertTableCount($base_table, $count = TRUE) {
* The name of the module.
*/
function assertModuleTablesExist($module) {
$tables = array_keys(drupal_get_schema_unprocessed($module));
$tables = array_keys(drupal_get_module_schema($module));
$tables_exist = TRUE;
foreach ($tables as $table) {
if (!db_table_exists($table)) {
......@@ -76,7 +76,7 @@ function assertModuleTablesExist($module) {
* The name of the module.
*/
function assertModuleTablesDoNotExist($module) {
$tables = array_keys(drupal_get_schema_unprocessed($module));
$tables = array_keys(drupal_get_module_schema($module));
$tables_exist = FALSE;
foreach ($tables as $table) {
if (db_table_exists($table)) {
......
......@@ -120,7 +120,6 @@ public function setUp() {
'config',
'cache_discovery',
'cache_bootstrap',
'cache_default',
'file_managed',
'key_value_expire',
'menu_link_content',
......
......@@ -28,7 +28,7 @@ function module_test_schema() {
* Implements hook_install().
*/
function module_test_install() {
$schema = drupal_get_schema('module_test');
$schema = drupal_get_module_schema('module_test', 'module_test');
db_insert('module_test')
->fields(array(
'data' => $schema['fields']['data']['type'],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment