addPrimaryKey example

$this->resetTableInformation($table);
  }

  protected function _createIndexSql($table$name$fields) {
    $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->assertFalse($this->schema->fieldExists($table_name, 'test_field_renamed'));
    $this->assertEquals([]$find_primary_key_columns->invoke($this->schema, $table_name));

    // Add the field again and make sure adding the primary key can be done at     // the same time.     $this->schema->addField($table_name, 'test_field', ['type' => 'int', 'default' => 0, '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));

    // Drop the field again and explicitly add a primary key.     $this->schema->dropField($table_name, 'test_field');
    $this->schema->addPrimaryKey($table_name['other_test_field']);
    $this->assertFalse($this->schema->fieldExists($table_name, 'test_field'));
    $this->assertEquals(['other_test_field']$find_primary_key_columns->invoke($this->schema, $table_name));

    // Test that adding a field with a primary key will work even with a     // pre-existing primary key.     $this->schema->addField($table_name, 'test_field', ['type' => 'int', 'default' => 0, '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));
  }

  /** * Provides test cases for SchemaTest::testSchemaCreateTablePrimaryKey(). * * @return array * An array of test cases for SchemaTest::testSchemaCreateTablePrimaryKey(). */
/** * @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);

    $find_primary_keys_columns = new \ReflectionMethod(get_class($this->testingFakeConnection->schema()), 'findPrimaryKeyColumns');
    $results = $find_primary_keys_columns->invoke($this->testingFakeConnection->schema(), 'faking_table');

    $this->assertCount(1, $results);
    
'constraint' => 11,
                'null'       => false,
            ],
            'batch' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
        ]);

        $forge->addPrimaryKey('id');
        $forge->createTable($this->table, true);

        $this->tableChecked = true;
    }

    /** * Handles the actual running of a migration. * * @param string $direction "up" or "down" * @param object $migration The migration to run */
    
$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);

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

    protected function _processPrimaryKeys(string $table, bool $asQuery = false): string
    {
        if ($asQuery === false) {
            return parent::_processPrimaryKeys($table$asQuery);
        }

        $sqlTable = new Table($this->db, $this);

        $sqlTable->fromTable($this->db->DBPrefix . $table)
            ->addPrimaryKey($this->primaryKeys)
            ->run();

        return '';
    }

    /** * Generates SQL to add foreign keys * * @param bool $asQuery When true recreates table with key, else partial SQL used with CREATE TABLE */
    protected function _processForeignKeys(string $table, bool $asQuery = false): array
    {
$this->keys = array_filter(
            $this->keys,
            static fn ($index) => count(array_intersect($index['fields']$fieldNames)) === count($index['fields'])
        );

        // 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;
                }
            }
        }
Home | Imprint | This part of the site doesn't use cookies.