dropPrimaryKey example

// This function will not work due to a the fact that indexExist() does not search for keys without idx tag.     // @todo remove comments when: https://www.drupal.org/project/drupal/issues/3325358 is committed.     // $this->assertFalse($this->testingFakeConnection->schema()->indexExists('faking_table', 'test_field'));   }

  /** * @covers ::addPrimaryKey * @covers ::dropPrimaryKey */
  public function testPrimaryKey(): void {
    $this->testingFakeConnection->schema()->dropPrimaryKey('faking_table');
    $results = $this->testingFakeConnection->query("SELECT * FROM pg_indexes WHERE schemaname = 'testing_fake'")->fetchAll();

    $this->assertCount(0, $results);

    $this->testingFakeConnection->schema()->addPrimaryKey('faking_table', ['id']);
    $results = $this->testingFakeConnection->query("SELECT * FROM pg_indexes WHERE schemaname = 'testing_fake'")->fetchAll();

    $this->assertCount(1, $results);
    $this->assertSame('testing_fake', $results[0]->schemaname);
    $this->assertSame($this->testingFakeConnection->getPrefix() . 'faking_table', $results[0]->tablename);
    $this->assertStringContainsString('USING btree (id)', $results[0]->indexdef);

    
->run();
    }

    /** * Drop Primary Key */
    public function dropPrimaryKey(string $table, string $keyName = ''): bool
    {
        $sqlTable = new Table($this->db, $this);

        return $sqlTable->fromTable($this->db->DBPrefix . $table)
            ->dropPrimaryKey()
            ->run();
    }

    public function addForeignKey($fieldName = '', string $tableName = '', $tableField = '', string $onUpdate = '', string $onDelete = '', string $fkName = ''): BaseForge
    {
        if ($fkName === '') {
            return parent::addForeignKey($fieldName$tableName$tableField$onUpdate$onDelete$fkName);
        }

        throw new DatabaseException('SQLite does not support foreign key names. CodeIgniter will refer to them in the format: prefix_table_column_referencecolumn_foreign');
    }

    
    $table_name_new = 'from';
    $this->schema->renameTable($table_name$table_name_new);
    $this->assertFalse($this->schema->tableExists($table_name));
    $this->assertTrue($this->schema->tableExists($table_name_new));

    // Adding a field.     $field_name = 'delete';
    $this->schema->addField($table_name_new$field_name['type' => 'int', 'not null' => TRUE]);
    $this->assertTrue($this->schema->fieldExists($table_name_new$field_name));

    // Dropping a primary key.     $this->schema->dropPrimaryKey($table_name_new);

    // Adding a primary key.     $this->schema->addPrimaryKey($table_name_new[$field_name]);

    // Check the primary key columns.     $find_primary_key_columns = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
    $this->assertEquals([$field_name]$find_primary_key_columns->invoke($this->schema, $table_name_new));

    // Dropping a primary key.     $this->schema->dropPrimaryKey($table_name_new);

    
->fields([$field => $spec['initial']])
        ->execute();
    }
    if ($fixnull) {
      $this->connection->query("ALTER TABLE {" . $table . "} ALTER $field SET NOT NULL");
    }
    if (isset($new_keys)) {
      // Make sure to drop the existing primary key before adding a new one.       // This is only needed when adding a field because this method, unlike       // changeField(), is supposed to handle primary keys automatically.       if (isset($new_keys['primary key']) && $this->constraintExists($table, 'pkey')) {
        $this->dropPrimaryKey($table);
      }
      $this->_createKeys($table$new_keys);
    }
    // Add column comment.     if (!empty($spec['description'])) {
      $this->connection->query('COMMENT ON COLUMN {' . $table . '}.' . $field . ' IS ' . $this->prepareComment($spec['description']));
    }
    $this->resetTableInformation($table);
  }

  /** * {@inheritdoc} */
    // the type leaves the primary key in place even with existing data.     $this->connection
      ->insert($table_name)
      ->fields(['test_field' => 1, 'other_test_field' => 2])
      ->execute();
    $this->schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE]);
    $this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
    $this->assertEquals($initial_primary_key$find_primary_key_columns->invoke($this->schema, $table_name));

    // Make sure that adding the primary key can be done as part of changing     // a field, as well.     $this->schema->dropPrimaryKey($table_name);
    $this->assertEquals([]$find_primary_key_columns->invoke($this->schema, $table_name));
    $this->schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE]['primary key' => $initial_primary_key]);
    $this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
    $this->assertEquals($initial_primary_key$find_primary_key_columns->invoke($this->schema, $table_name));

    // Rename the field and make sure the primary key was updated.     $this->schema->changeField($table_name, 'test_field', 'test_field_renamed', ['type' => 'int', 'not null' => TRUE]);
    $this->assertTrue($this->schema->fieldExists($table_name, 'test_field_renamed'));
    $this->assertEquals($renamed_primary_key$find_primary_key_columns->invoke($this->schema, $table_name));

    // Drop the field and make sure the primary key was dropped, as well.


    // When dropping a field that is part of a composite primary key MySQL     // automatically removes the field from the primary key, which can leave the     // table in an invalid state. MariaDB 10.2.8 requires explicitly dropping     // the primary key first for this reason. We perform this deletion     // explicitly which also makes the behavior on both MySQL and MariaDB     // consistent with PostgreSQL.     // @see https://mariadb.com/kb/en/library/alter-table     $primary_key = $this->findPrimaryKeyColumns($table);
    if ((count($primary_key) > 1) && in_array($field$primary_key, TRUE)) {
      $this->dropPrimaryKey($table);
    }

    $this->connection->query('ALTER TABLE {' . $table . '} DROP [' . $field . ']');
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function indexExists($table$name) {
    // Returns one row for each column in the index. Result is string or FALSE.
Home | Imprint | This part of the site doesn't use cookies.