Skip to content

Commit c536c20

Browse files
committed
Issue #2883260 by kiamlaluno, yogeshmpawar, msankhala, benjifisher, alexpott, bdlangton: Replace the schema example with one actually used from a module
(cherry picked from commit b9c85bd)
1 parent b1bb427 commit c536c20

File tree

1 file changed

+96
-92
lines changed

1 file changed

+96
-92
lines changed

lib/Drupal/Core/Database/database.api.php

Lines changed: 96 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,15 @@
248248
*
249249
* The following keys are defined:
250250
* - 'description': A string in non-markup plain text describing this table
251-
* and its purpose. References to other tables should be enclosed in
252-
* curly-brackets. For example, the node_field_revision table
253-
* description field might contain "Stores per-revision title and
254-
* body data for each {node}."
251+
* and its purpose. References to other tables should be enclosed in curly
252+
* brackets.
255253
* - 'fields': An associative array ('fieldname' => specification)
256254
* that describes the table's database columns. The specification
257255
* is also an array. The following specification parameters are defined:
258256
* - 'description': A string in non-markup plain text describing this field
259-
* and its purpose. References to other tables should be enclosed in
260-
* curly-brackets. For example, the node table vid field
261-
* description might contain "Always holds the largest (most
262-
* recent) {node_field_revision}.vid value for this nid."
257+
* and its purpose. References to other tables should be enclosed in curly
258+
* brackets. For example, the users_data table 'uid' field description
259+
* might contain "The {users}.uid this record affects."
263260
* - 'type': The generic datatype: 'char', 'varchar', 'text', 'blob', 'int',
264261
* 'float', 'numeric', or 'serial'. Most types just map to the according
265262
* database engine specific data types. Use 'serial' for auto incrementing
@@ -322,64 +319,70 @@
322319
* key column specifiers (see below) that form an index on the
323320
* table.
324321
*
325-
* A key column specifier is either a string naming a column or an
326-
* array of two elements, column name and length, specifying a prefix
327-
* of the named column.
322+
* A key column specifier is either a string naming a column or an array of two
323+
* elements, column name and length, specifying a prefix of the named column.
328324
*
329-
* As an example, here is a SUBSET of the schema definition for
330-
* Drupal's 'node' table. It show four fields (nid, vid, type, and
331-
* title), the primary key on field 'nid', a unique key named 'vid' on
332-
* field 'vid', and two indexes, one named 'nid' on field 'nid' and
333-
* one named 'node_title_type' on the field 'title' and the first four
334-
* bytes of the field 'type':
325+
* As an example, this is the schema definition for the 'users_data' table. It
326+
* shows five fields ('uid', 'module', 'name', 'value', and 'serialized'), the
327+
* primary key (on the 'uid', 'module', and 'name' fields), and two indexes (the
328+
* 'module' index on the 'module' field and the 'name' index on the 'name'
329+
* field).
335330
*
336331
* @code
337-
* $schema['node'] = array(
338-
* 'description' => 'The base table for nodes.',
339-
* 'fields' => array(
340-
* 'nid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
341-
* 'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE,'default' => 0),
342-
* 'type' => array('type' => 'varchar','length' => 32,'not null' => TRUE, 'default' => ''),
343-
* 'language' => array('type' => 'varchar','length' => 12,'not null' => TRUE,'default' => ''),
344-
* 'title' => array('type' => 'varchar','length' => 255,'not null' => TRUE, 'default' => ''),
345-
* 'uid' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
346-
* 'status' => array('type' => 'int', 'not null' => TRUE, 'default' => 1),
347-
* 'created' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
348-
* 'changed' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
349-
* 'comment' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
350-
* 'promote' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
351-
* 'moderate' => array('type' => 'int', 'not null' => TRUE,'default' => 0),
352-
* 'sticky' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
353-
* 'translate' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
354-
* ),
355-
* 'indexes' => array(
356-
* 'node_changed' => array('changed'),
357-
* 'node_created' => array('created'),
358-
* 'node_moderate' => array('moderate'),
359-
* 'node_frontpage' => array('promote', 'status', 'sticky', 'created'),
360-
* 'node_status_type' => array('status', 'type', 'nid'),
361-
* 'node_title_type' => array('title', array('type', 4)),
362-
* 'node_type' => array(array('type', 4)),
363-
* 'uid' => array('uid'),
364-
* 'translate' => array('translate'),
365-
* ),
366-
* 'unique keys' => array(
367-
* 'vid' => array('vid'),
368-
* ),
332+
* $schema['users_data'] = [
333+
* 'description' => 'Stores module data as key/value pairs per user.',
334+
* 'fields' => [
335+
* 'uid' => [
336+
* 'description' => 'The {users}.uid this record affects.',
337+
* 'type' => 'int',
338+
* 'unsigned' => TRUE,
339+
* 'not null' => TRUE,
340+
* 'default' => 0,
341+
* ],
342+
* 'module' => [
343+
* 'description' => 'The name of the module declaring the variable.',
344+
* 'type' => 'varchar_ascii',
345+
* 'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
346+
* 'not null' => TRUE,
347+
* 'default' => '',
348+
* ],
349+
* 'name' => [
350+
* 'description' => 'The identifier of the data.',
351+
* 'type' => 'varchar_ascii',
352+
* 'length' => 128,
353+
* 'not null' => TRUE,
354+
* 'default' => '',
355+
* ],
356+
* 'value' => [
357+
* 'description' => 'The value.',
358+
* 'type' => 'blob',
359+
* 'not null' => FALSE,
360+
* 'size' => 'big',
361+
* ],
362+
* 'serialized' => [
363+
* 'description' => 'Whether value is serialized.',
364+
* 'type' => 'int',
365+
* 'size' => 'tiny',
366+
* 'unsigned' => TRUE,
367+
* 'default' => 0,
368+
* ],
369+
* ],
370+
* 'primary key' => ['uid', 'module', 'name'],
371+
* 'indexes' => [
372+
* 'module' => ['module'],
373+
* 'name' => ['name'],
374+
* ],
369375
* // For documentation purposes only; foreign keys are not created in the
370376
* // database.
371-
* 'foreign keys' => array(
372-
* 'node_revision' => array(
373-
* 'table' => 'node_field_revision',
374-
* 'columns' => array('vid' => 'vid'),
375-
* ),
376-
* 'node_author' => array(
377+
* 'foreign keys' => [
378+
* 'data_user' => [
377379
* 'table' => 'users',
378-
* 'columns' => array('uid' => 'uid'),
379-
* ),
380-
* ),
381-
* 'primary key' => array('nid'),
382-
* );
380+
* 'columns' => [
381+
* 'uid' => 'uid',
382+
* ],
383+
* ],
384+
* ],
385+
* ];
383386
* @endcode
384387
*
385388
* @see drupal_install_schema()
@@ -490,60 +493,61 @@ function hook_query_TAG_alter(Drupal\Core\Database\Query\AlterableInterface $que
490493
* @ingroup schemaapi
491494
*/
492495
function hook_schema() {
493-
$schema['node'] = [
494-
// Example (partial) specification for table "node".
495-
'description' => 'The base table for nodes.',
496+
$schema['users_data'] = [
497+
'description' => 'Stores module data as key/value pairs per user.',
496498
'fields' => [
497-
'nid' => [
498-
'description' => 'The primary identifier for a node.',
499-
'type' => 'serial',
500-
'unsigned' => TRUE,
501-
'not null' => TRUE,
502-
],
503-
'vid' => [
504-
'description' => 'The current {node_field_revision}.vid version identifier.',
499+
'uid' => [
500+
'description' => 'The {users}.uid this record affects.',
505501
'type' => 'int',
506502
'unsigned' => TRUE,
507503
'not null' => TRUE,
508504
'default' => 0,
509505
],
510-
'type' => [
511-
'description' => 'The type of this node.',
512-
'type' => 'varchar',
513-
'length' => 32,
506+
'module' => [
507+
'description' => 'The name of the module declaring the variable.',
508+
'type' => 'varchar_ascii',
509+
'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
514510
'not null' => TRUE,
515511
'default' => '',
516512
],
517-
'title' => [
518-
'description' => 'The node title.',
519-
'type' => 'varchar',
520-
'length' => 255,
513+
'name' => [
514+
'description' => 'The identifier of the data.',
515+
'type' => 'varchar_ascii',
516+
'length' => 128,
521517
'not null' => TRUE,
522518
'default' => '',
523519
],
520+
'value' => [
521+
'description' => 'The value.',
522+
'type' => 'blob',
523+
'not null' => FALSE,
524+
'size' => 'big',
525+
],
526+
'serialized' => [
527+
'description' => 'Whether value is serialized.',
528+
'type' => 'int',
529+
'size' => 'tiny',
530+
'unsigned' => TRUE,
531+
'default' => 0,
532+
],
524533
],
534+
'primary key' => ['uid', 'module', 'name'],
525535
'indexes' => [
526-
'node_changed' => ['changed'],
527-
'node_created' => ['created'],
528-
],
529-
'unique keys' => [
530-
'nid_vid' => ['nid', 'vid'],
531-
'vid' => ['vid'],
536+
'module' => ['module'],
537+
'name' => ['name'],
532538
],
533539
// For documentation purposes only; foreign keys are not created in the
534540
// database.
535541
'foreign keys' => [
536-
'node_revision' => [
537-
'table' => 'node_field_revision',
538-
'columns' => ['vid' => 'vid'],
539-
],
540-
'node_author' => [
542+
'data_user' => [
541543
'table' => 'users',
542-
'columns' => ['uid' => 'uid'],
544+
'columns' => [
545+
'uid' => 'uid',
546+
],
543547
],
544548
],
545-
'primary key' => ['nid'],
546549
];
550+
547551
return $schema;
548552
}
549553

0 commit comments

Comments
 (0)