Commit db8fa578 authored by alexpott's avatar alexpott

Issue #2451395 by dawehner: drupal_get_schema()/drupal_get_complete_schema()...

Issue #2451395 by dawehner: drupal_get_schema()/drupal_get_complete_schema() no longer work as expected; remove them
parent a0078c51
......@@ -1524,14 +1524,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.
*
......
......@@ -417,7 +417,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());
......
......@@ -203,9 +203,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));
......
......@@ -136,15 +136,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'),
......
......@@ -425,10 +425,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,8 @@ 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.");
$schema = drupal_get_schema_unprocessed($module, $table);
$this->assertTrue($schema, "'$table' table schema found.");
// Install the module.
\Drupal::service('module_installer')->install(array($module));
......@@ -122,7 +122,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_schema_unprocessed($module, $table);
$this->assertTrue($schema, "'$table' table schema found.");
}
......@@ -154,9 +154,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_schema_unprocessed($module, $table);
$this->assertTrue($schema, "'$table' table schema found.");
// Verify that a unknown table from an enabled module throws an error.
......@@ -169,7 +167,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_schema_unprocessed($module, $table);
$this->assertFalse($schema, "'$table' table schema not found.");
// Verify that a table from a unknown module cannot be installed.
......@@ -183,14 +181,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_schema_unprocessed($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_schema_unprocessed($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_schema_unprocessed('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_schema_unprocessed('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.
......
......@@ -43,6 +43,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.
......
......@@ -28,7 +28,7 @@ function module_test_schema() {
* Implements hook_install().
*/
function module_test_install() {
$schema = drupal_get_schema('module_test');
$schema = drupal_get_schema_unprocessed('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