Update Schema API to make it more usable

Created on 13 April 2009, almost 16 years ago
Updated 11 April 2023, almost 2 years ago

Right now, Schema API has a few problems.

1) The API for table creation and modification is completely different than the rest of the DB layer, because it's still wacky functions with big arrays rather than fluent method chaining.

2) The API for table alteration doesn't allow multiple alter commands in one query, which in some cases is totally nuts for performance, especially when creating or manipulating indexes. It also leads to some crazy footwork to get around not having an index in places, such as: http://drupal.org/node/159329

So the solution is to make Schema API suck less. I see two possible approaches. (Neither of these affect hook_schema(). That's a layer above all of this.)

1) Introduce DDL query builders, along these lines:

db_alter('tablename')
  ->addIndex('foo')
  ->addField('bar')
  ->addField('blob')
  ->dropField('baz')
  ->execute();

db_table('tablename')
  ->addField('bar')
  ->addIndex('baz')
  ->addField('foo')
  ->execute();

Pros:
- API is similar to the other query builders we have.
- Probably easier to implement than option 2.

Cons:
- Still a query-based approach rather than a DB-state-based approach.

2) Fully objectify Schema API with classes/objects to represent tables and fields.


$schema = $connection->schema();
$table = $schema->getTable('tablename');
$field = new DBField('newfieldname');
$field->setType('blah');
$table->addField($field)l;
$table->dropIndex('bar');
$table->save();

$table = $schema->createTable('tablename');
$table->addField($field);
$array = $table->getArrayVersionLikeHookSchemaUses();
$sql = $table->getSqlCreateStatement();
$table->save();

Pros:
- Richer, more complete API.
- More like other similar systems (I think).

Cons:
- Different than other parts of the DB API at the moment, but less different than it is now.
- More effort required to implement.

Thoughts?

πŸ“Œ Task
Status

Active

Version

10.1 ✨

Component
DatabaseΒ  β†’

Last updated about 11 hours ago

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

πŸ‡ΊπŸ‡ΈUnited States Crell

Live updates comments and jobs are added and updated live.
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.

  • πŸ‡©πŸ‡ͺGermany donquixote

    Are there still any plans to define database table schemas in yaml instead of a hook?
    It seems this would work fine for all schemas that are not dynamic, that is, that don't depend on site configuration.

    Of course contrib modules can just call Yaml::parseFile() within their hook_schema(), but then every module would have to invent its own convention how to name and where to store this yml file.

  • πŸ‡¬πŸ‡§United Kingdom catch

    @donquixote there's nothing active, but that would be a good issue to open.

Production build 0.71.5 2024