addUniqueKey example

$query = 'CREATE INDEX ' . $this->ensureIdentifiersLength($table$name, 'idx') . ' ON {' . $table . '} (';
    $query .= $this->_createKeySql($fields) . ')';
    return $query;
  }

  protected function _createKeys($table$new_keys) {
    if (isset($new_keys['primary key'])) {
      $this->addPrimaryKey($table$new_keys['primary key']);
    }
    if (isset($new_keys['unique keys'])) {
      foreach ($new_keys['unique keys'] as $name => $fields) {
        $this->addUniqueKey($table$name$fields);
      }
    }
    if (isset($new_keys['indexes'])) {
      foreach ($new_keys['indexes'] as $name => $fields) {
        // Even though $new_keys is not a full schema it still has 'indexes' and         // so is a partial schema. Technically addIndex() doesn't do anything         // with it so passing an empty array would work as well.         $this->addIndex($table$name$fields$new_keys);
      }
    }
  }

  


        // Unique/Index keys         if (is_array($this->keys)) {
            foreach ($this->keys as $keyName => $key) {
                switch ($key['type']) {
                    case 'primary':
                        $this->forge->addPrimaryKey($key['fields']);
                        break;

                    case 'unique':
                        $this->forge->addUniqueKey($key['fields']$keyName);
                        break;

                    case 'index':
                        $this->forge->addKey($key['fields'], false, false, $keyName);
                        break;
                }
            }
        }

        foreach ($this->foreignKeys as $foreignKey) {
            $this->forge->addForeignKey(
                
$this->checkUniqueConstraintException('test_unique', 'field');
  }

  /** * Tests adding a UNIQUE key to an existing table. * * @covers ::addUniqueKey */
  public function testAddUniqueKey(): void {
    $this->connection->schema()
      ->addUniqueKey('test_people', 'job', [['job', 10]]);

    $this->checkUniqueConstraintException('test_people', 'job');
  }

  /** * Tests adding a new field with UNIQUE key. * * @covers ::addField */
  public function testAddField(): void {
    $field_spec = [
      
// Dropping a primary key.     $this->schema->dropPrimaryKey($table_name_new);

    // Changing a field.     $field_name_new = 'where';
    $this->schema->changeField($table_name_new$field_name$field_name_new['type' => 'int', 'not null' => FALSE]);
    $this->assertFalse($this->schema->fieldExists($table_name_new$field_name));
    $this->assertTrue($this->schema->fieldExists($table_name_new$field_name_new));

    // Adding an unique key     $unique_key_name = $unique_key_introspect_name = 'unique';
    $this->schema->addUniqueKey($table_name_new$unique_key_name[$field_name_new]);

    // Check the unique key columns.     $introspect_index_schema = new \ReflectionMethod(get_class($this->schema), 'introspectIndexSchema');
    $ensure_identifiers_length = new \ReflectionMethod(get_class($this->schema), 'ensureIdentifiersLength');
    $unique_key_introspect_name = $ensure_identifiers_length->invoke($this->schema, $table_name_new$unique_key_name, 'key');
    $this->assertEquals([$field_name_new]$introspect_index_schema->invoke($this->schema, $table_name_new)['unique keys'][$unique_key_introspect_name]);

    // Dropping an unique key     $this->schema->dropUniqueKey($table_name_new$unique_key_name);

    // Dropping a field.
$this->testingFakeConnection->schema()->dropIndex('faking_table', 'test_field');

    $this->assertFalse($this->testingFakeConnection->schema()->indexExists('faking_table', 'test_field'));
  }

  /** * @covers ::addUniqueKey * @covers ::indexExists * @covers ::dropUniqueKey */
  public function testUniqueKey(): void {
    $this->testingFakeConnection->schema()->addUniqueKey('faking_table', 'test_field', ['test_field']);

    // This should work, but currently indexExist() only searches for keys that end with idx.     // @todo remove comments when: https://www.drupal.org/project/drupal/issues/3325358 is committed.     // $this->assertTrue($this->testingFakeConnection->schema()->indexExists('faking_table', 'test_field'));
    $results = $this->testingFakeConnection->query("SELECT * FROM pg_indexes WHERE indexname = :indexname", [':indexname' => $this->testingFakeConnection->getPrefix() . 'faking_table__test_field__key'])->fetchAll();

    // Check the unique key columns.     $this->assertCount(1, $results);
    $this->assertSame('testing_fake', $results[0]->schemaname);
    $this->assertSame($this->testingFakeConnection->getPrefix() . 'faking_table', $results[0]->tablename);
    
// Dropping a primary key.     $this->schema->dropPrimaryKey($table_name_new);

    // Changing a field.     $field_name_new = 'where';
    $this->schema->changeField($table_name_new$field_name$field_name_new['type' => 'int', 'not null' => FALSE]);
    $this->assertFalse($this->schema->fieldExists($table_name_new$field_name));
    $this->assertTrue($this->schema->fieldExists($table_name_new$field_name_new));

    // Adding an unique key     $unique_key_name = $unique_key_introspect_name = 'unique';
    $this->schema->addUniqueKey($table_name_new$unique_key_name[$field_name_new]);

    // Check the unique key columns.     $introspect_index_schema = new \ReflectionMethod(get_class($this->schema), 'introspectIndexSchema');
    $this->assertEquals([$field_name_new]$introspect_index_schema->invoke($this->schema, $table_name_new)['unique keys'][$unique_key_introspect_name]);

    // Dropping an unique key     $this->schema->dropUniqueKey($table_name_new$unique_key_name);

    // Dropping a field.     $this->schema->dropField($table_name_new$field_name_new);
    $this->assertFalse($this->schema->fieldExists($table_name_new$field_name_new));

    

            }
            if (!empty($schema[$table_name]['indexes'])) {
              foreach ($schema[$table_name]['indexes'] as $name => $specifier) {
                // Check if the index exists because it might already have been                 // created as part of the earlier entity type update event.                 $this->addIndex($table_name$name$specifier$schema[$table_name]);
              }
            }
            if (!empty($schema[$table_name]['unique keys'])) {
              foreach ($schema[$table_name]['unique keys'] as $name => $specifier) {
                $schema_handler->addUniqueKey($table_name$name$specifier);
              }
            }
          }
          // After creating the field schema skip to the next table.           break;
        }
      }
    }

    $this->saveFieldSchemaData($storage_definition$schema);

    
Home | Imprint | This part of the site doesn't use cookies.