You are here

public function Schema::constraintExists in Drupal 10

Helper function: check if a constraint (PK, FK, UK) exists.

Parameters

string $table: The name of the table.

string $name: The name of the constraint (typically 'pkey' or '[constraint]__key').

Return value

bool TRUE if the constraint exists, FALSE otherwise.

5 calls to Schema::constraintExists()
Schema::addField in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
Add a new field to a table.
Schema::addPrimaryKey in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
Add a primary key.
Schema::addUniqueKey in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
Add a unique key.
Schema::dropPrimaryKey in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
Drop the primary key.
Schema::dropUniqueKey in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
Drop a unique key.

File

core/modules/pgsql/src/Driver/Database/pgsql/Schema.php, line 736

Class

Schema
PostgreSQL implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\pgsql\Driver\Database\pgsql

Code

public function constraintExists($table, $name) {

  // ::ensureIdentifiersLength() expects three parameters, although not
  // explicitly stated in its signature, thus we split our constraint name in
  // a proper name and a suffix.
  if ($name == 'pkey') {
    $suffix = $name;
    $name = '';
  }
  else {
    $pos = strrpos($name, '__');
    $suffix = substr($name, $pos + 2);
    $name = substr($name, 0, $pos);
  }
  $constraint_name = $this
    ->ensureIdentifiersLength($table, $name, $suffix);

  // Remove leading and trailing quotes because the index name is in a WHERE
  // clause and not used as an identifier.
  $constraint_name = str_replace('"', '', $constraint_name);
  return (bool) $this->connection
    ->query("SELECT 1 FROM pg_constraint WHERE conname = '{$constraint_name}'")
    ->fetchField();
}