comparison core/lib/Drupal/Core/Database/Schema.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
237 /** 237 /**
238 * Check if a column exists in the given table. 238 * Check if a column exists in the given table.
239 * 239 *
240 * @param $table 240 * @param $table
241 * The name of the table in drupal (no prefixing). 241 * The name of the table in drupal (no prefixing).
242 * @param $name 242 * @param $column
243 * The name of the column. 243 * The name of the column.
244 * 244 *
245 * @return 245 * @return
246 * TRUE if the given column exists, otherwise FALSE. 246 * TRUE if the given column exists, otherwise FALSE.
247 */ 247 */
314 * @param $keys_new 314 * @param $keys_new
315 * (optional) Keys and indexes specification to be created on the 315 * (optional) Keys and indexes specification to be created on the
316 * table along with adding the field. The format is the same as a 316 * table along with adding the field. The format is the same as a
317 * table specification but without the 'fields' element. If you are 317 * table specification but without the 'fields' element. If you are
318 * adding a type 'serial' field, you MUST specify at least one key 318 * adding a type 'serial' field, you MUST specify at least one key
319 * or index including it in this array. See db_change_field() for more 319 * or index including it in this array. See ::changeField() for more
320 * explanation why. 320 * explanation why.
321 * 321 *
322 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException 322 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
323 * If the specified table doesn't exist. 323 * If the specified table doesn't exist.
324 * @throws \Drupal\Core\Database\SchemaObjectExistsException 324 * @throws \Drupal\Core\Database\SchemaObjectExistsException
350 * @param $default 350 * @param $default
351 * Default value to be set. NULL for 'default NULL'. 351 * Default value to be set. NULL for 'default NULL'.
352 * 352 *
353 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException 353 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
354 * If the specified table or field doesn't exist. 354 * If the specified table or field doesn't exist.
355 *
356 * @deprecated as of Drupal 8.7.x, will be removed in Drupal 9.0.0. Instead,
357 * call ::changeField() passing a full field specification.
358 *
359 * @see ::changeField()
355 */ 360 */
356 abstract public function fieldSetDefault($table, $field, $default); 361 abstract public function fieldSetDefault($table, $field, $default);
357 362
358 /** 363 /**
359 * Set a field to have no default value. 364 * Set a field to have no default value.
363 * @param $field 368 * @param $field
364 * The field to be altered. 369 * The field to be altered.
365 * 370 *
366 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException 371 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
367 * If the specified table or field doesn't exist. 372 * If the specified table or field doesn't exist.
373 *
374 * @deprecated as of Drupal 8.7.x, will be removed in Drupal 9.0.0. Instead,
375 * call ::changeField() passing a full field specification.
376 *
377 * @see ::changeField()
368 */ 378 */
369 abstract public function fieldSetNoDefault($table, $field); 379 abstract public function fieldSetNoDefault($table, $field);
370 380
371 /** 381 /**
372 * Checks if an index exists in the given table. 382 * Checks if an index exists in the given table.
534 * by that name to begin with. 544 * by that name to begin with.
535 */ 545 */
536 abstract public function dropIndex($table, $name); 546 abstract public function dropIndex($table, $name);
537 547
538 /** 548 /**
549 * Finds the columns for the primary key, unique keys and indexes of a table.
550 *
551 * @param string $table
552 * The name of the table.
553 *
554 * @return array
555 * A schema array with the following keys: 'primary key', 'unique keys' and
556 * 'indexes', and values as arrays of database columns.
557 *
558 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
559 * If the specified table doesn't exist.
560 * @throws \RuntimeException
561 * If the driver does not implement this method.
562 */
563 protected function introspectIndexSchema($table) {
564 if (!$this->tableExists($table)) {
565 throw new SchemaObjectDoesNotExistException("The table $table doesn't exist.");
566 }
567 throw new \RuntimeException("The '{$this->connection->driver()}' database driver does not implement " . __METHOD__);
568 }
569
570 /**
539 * Change a field definition. 571 * Change a field definition.
540 * 572 *
541 * IMPORTANT NOTE: To maintain database portability, you have to explicitly 573 * IMPORTANT NOTE: To maintain database portability, you have to explicitly
542 * recreate all indices and primary keys that are using the changed field. 574 * recreate all indices and primary keys that are using the changed field.
543 * 575 *
544 * That means that you have to drop all affected keys and indexes with 576 * That means that you have to drop all affected keys and indexes with
545 * db_drop_{primary_key,unique_key,index}() before calling db_change_field(). 577 * Schema::dropPrimaryKey(), Schema::dropUniqueKey(), or Schema::dropIndex()
578 * before calling ::changeField().
546 * To recreate the keys and indices, pass the key definitions as the 579 * To recreate the keys and indices, pass the key definitions as the
547 * optional $keys_new argument directly to db_change_field(). 580 * optional $keys_new argument directly to ::changeField().
548 * 581 *
549 * For example, suppose you have: 582 * For example, suppose you have:
550 * @code 583 * @code
551 * $schema['foo'] = array( 584 * $schema['foo'] = array(
552 * 'fields' => array( 585 * 'fields' => array(
556 * ); 589 * );
557 * @endcode 590 * @endcode
558 * and you want to change foo.bar to be type serial, leaving it as the 591 * and you want to change foo.bar to be type serial, leaving it as the
559 * primary key. The correct sequence is: 592 * primary key. The correct sequence is:
560 * @code 593 * @code
561 * db_drop_primary_key('foo'); 594 * $injected_database->schema()->dropPrimaryKey('foo');
562 * db_change_field('foo', 'bar', 'bar', 595 * $injected_database->schema()->changeField('foo', 'bar', 'bar',
563 * array('type' => 'serial', 'not null' => TRUE), 596 * array('type' => 'serial', 'not null' => TRUE),
564 * array('primary key' => array('bar'))); 597 * array('primary key' => array('bar')));
565 * @endcode 598 * @endcode
566 * 599 *
567 * The reasons for this are due to the different database engines: 600 * The reasons for this are due to the different database engines:
570 * and dropping an old one which* causes any indices, primary keys and 603 * and dropping an old one which* causes any indices, primary keys and
571 * sequences (from serial-type fields) that use the changed field to be dropped. 604 * sequences (from serial-type fields) that use the changed field to be dropped.
572 * 605 *
573 * On MySQL, all type 'serial' fields must be part of at least one key 606 * On MySQL, all type 'serial' fields must be part of at least one key
574 * or index as soon as they are created. You cannot use 607 * or index as soon as they are created. You cannot use
575 * db_add_{primary_key,unique_key,index}() for this purpose because 608 * Schema::addPrimaryKey, Schema::addUniqueKey(), or Schema::addIndex()
576 * the ALTER TABLE command will fail to add the column without a key 609 * for this purpose because the ALTER TABLE command will fail to add
577 * or index specification. The solution is to use the optional 610 * the column without a key or index specification.
578 * $keys_new argument to create the key or index at the same time as 611 * The solution is to use the optional $keys_new argument to create the key
579 * field. 612 * or index at the same time as field.
580 * 613 *
581 * You could use db_add_{primary_key,unique_key,index}() in all cases 614 * You could use Schema::addPrimaryKey, Schema::addUniqueKey(), or
582 * unless you are converting a field to be type serial. You can use 615 * Schema::addIndex() in all cases unless you are converting a field to
583 * the $keys_new argument in all cases. 616 * be type serial. You can use the $keys_new argument in all cases.
584 * 617 *
585 * @param $table 618 * @param $table
586 * Name of the table. 619 * Name of the table.
587 * @param $field 620 * @param $field
588 * Name of the field to change. 621 * Name of the field to change.