Problem/Motivation

There are three ways to create tables based on Schema API:

  1. Implement hook_schema(): Schema is stored in code.
  2. Create tables via Entity API: Schema stored in key/value store, under entity.storage_schema.sql collection.
  3. Dynamically created with Database::getConnection()->schema()->createTable(): stored nowhere.

Right now if you want to inspect the schema for a table there's no way to do it in a consistent way:

  • For module schema, you can use drupal_get_module_schema()
  • For entity tables schema you query the key/value store
  • For dynamically created tables there's no way to do it.

You cannot ask: Hey, give me the schema for table {some_table_created_with_a_valid_schema} regardless of how the table has been created.

Proposed resolution

  • Register/deregister dynamic table schema on creation/deletion in the key/value store in a new collection sql_schema (proposal)
  • Create a single point of inspecting DB schema by adding 2 new methods to \Drupal\Core\Database\Schema:
    • ::getSchema($table_name)
    • ::getSchemaByModule($module, $table_name = NULL)

    Both methods should aggregate all types of table created with a valid schema (from all 3 sources). For example it will be possible to get a result when querying:

    // hook_schema() table.
    $schema = Database::getConnection()->schema()->getSchema('key_value');
    // Entity API table.
    $schema = Database::getConnection()->schema()->getSchema('node_field_data');
    // Dynamic created table.
    $schema = Database::getConnection()->schema()->getSchema('cache_bootstrap');
    

    Persistent/statically cache the results of these new methods. Dynamically created tables will be owned by 'core' module.

  • Optionally but recommended: Add an interface to \Drupal\Core\Database\Schema for existing and new methods.
  • Deprecate drupal_get_module_schema() & Co.

Remaining tasks

Discuss, fix, tests.

User interface changes

None.

API changes

  • New methods in \Drupal\Core\Database\Schema
  • Potentially new interface for \Drupal\Core\Database\Schema.

Data model changes

New key/value collection: sql_schema.

Comments

claudiu.cristea created an issue. See original summary.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

tr’s picture

drupal_get_module_schema() was removed in core 9.2 by #2908886: Split off schema management out of schema.inc

There is now currently no API to get a table schema for any table. I consider this a regression, and this issue should probably now be a bug report rather than a feature request.

Use case:

  • I maintain a table of mission critical data on my Drupal server that is exposed as a REST resource.
  • When this table is updated, it is vital that it is done in a transaction so that the update either works completely or fails completely - I CANNOT have a partial update, and I CANNOT serve data while the update is in progress.
  • The way I accomplish this is to create a temporary table, populate that table, check that table for integrity, then in a transaction replace the old table with the new. This ensures the data is always available and always valid.

There is nothing unusual or edge about this use case - just because not many modules do this does not mean it is wrong in any way. Drupal should and must have methods for dealing with non-Entity, non-Field data.

But for this use case to work, I MUST be able to create a new table with the same structure as the existing table. And for that I need the schema of the existing table. While I have \Database::getConnection()->startTransaction(), \Database::getConnection()->schema()->dropTable() and Database::getConnection()->schema()->renameTable() available for this purpose, creating the temporary table requires \Database::getConnection()->schema()->createTable() which requires a schema for the created table. There is no way to obtain that schema anymore now that drupal_get_module_schema() has been removed.

The solution suggested in #2908886: Split off schema management out of schema.inc is to use \Drupal\TestTools\Extension\SchemaInspector, which is intended only for Tests. This is not a substitute for having schema introspection in core. The only other alternative is to call <modulename>_schema() directly, which again is not a good solution.

This issue proposes a solution that would work for my use case. It requires maintainer feedback. Part of this solution is to deprecated drupal_get_module_schema(), but that has already been done, and without a replacement, so IMO the priority of this issue should now be higher because functionality has disappeared.

@claudiu.cristea, this is still assigned to you - are you intending to work on it?

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.