[meta] Add value objects to represent hook_schema

Created on 14 September 2017, about 7 years ago
Updated 30 January 2023, almost 2 years ago

Problem/Motivation

At present, hook_schema is one of those APIs where we use a keyed array that is difficult to remember the keys for.
For many other APIs in core, we've replaced them with value objects and a mix of factory methods and the builder pattern.
E.g.

BaseFieldDefinition::create('string')
  ->setLabel('Foo')
  ->setRequired()

Proposed resolution

Add value objects to represent the return values in hook_schema.
Make them implement \ArrayAccess for BC sake.
e.g

use \Drupal\Core\Database\Schema as Schema;

function mymodule_schema() {
  $schema = new Schema\ModuleSchema();
  $table = Schema\Table::create('table description');
    ->addField('id', Schema\SerialColumn::create());
    ->addField('some_field', Schema\IntegerColumn::create()
      ->setDefault(0)
      ->setNotNull()
      ->setSize(Schema\IntegerColumn::SIZE_MEDIUM)
    )
    ->addIndex('some_index', ['some_field');
    ->addUniqueIndex('some_other_index', ['some', 'other', 'fields']));
  $schema->addTable('table_name', $table);
  return $schema;
}

Remaining tasks

Agree on the API before we start coding.
Patch
Tests
Reviews

User interface changes

None

API changes

API additions.

Data model changes

None

πŸ“Œ Task
Status

Needs work

Version

10.1 ✨

Component
DatabaseΒ  β†’

Last updated 3 days ago

  • Maintained by
  • πŸ‡³πŸ‡±Netherlands @daffie
Created by

πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

Production build 0.71.5 2024