Skip to content

Create Forge::dropPrimaryKey() #6488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,20 @@ public function dropKey(string $table, string $keyName)
return $this->db->query($sql);
}

/**
* Drop Primary Key
*/
public function dropPrimaryKey(string $table): bool
{
$sql = sprintf(
'ALTER TABLE %s DROP CONSTRAINT %s',
$this->db->escapeIdentifiers($this->db->DBPrefix . $table),
$this->db->escapeIdentifiers('pk_' . $this->db->DBPrefix . $table),
);

return $this->db->query($sql);
}

/**
* @throws DatabaseException
*
Expand Down
13 changes: 13 additions & 0 deletions system/Database/MySQLi/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,17 @@ public function dropKey(string $table, string $keyName)

return $this->db->query($sql);
}

/**
* Drop Primary Key
*/
public function dropPrimaryKey(string $table): bool
{
$sql = sprintf(
'ALTER TABLE %s DROP PRIMARY KEY',
$this->db->escapeIdentifiers($this->db->DBPrefix . $table)
);

return $this->db->query($sql);
}
}
12 changes: 12 additions & 0 deletions system/Database/SQLite3/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,16 @@ public function dropForeignKey(string $table, string $foreignName): bool
->dropForeignKey($foreignName)
->run();
}

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

return $sqlTable->fromTable($this->db->DBPrefix . $table)
->dropPrimaryKey()
->run();
}
}
14 changes: 14 additions & 0 deletions system/Database/SQLite3/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ public function modifyColumn(array $field)
return $this;
}

/**
* Drops the primary key
*/
public function dropPrimaryKey(): Table
{
$primaryIndexes = array_filter($this->keys, static fn ($index) => strtolower($index['type']) === 'primary');

foreach (array_keys($primaryIndexes) as $key) {
unset($this->keys[$key]);
}

return $this;
}

/**
* Drops a foreign key from this table so that
* it won't be recreated in the future.
Expand Down
36 changes: 36 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1372,4 +1372,40 @@ public function testAddTextColumnWithConstraint()

$this->assertFalse($this->db->fieldExists('text_with_constraint', 'user'));
}

public function testDropPrimaryKey()
{
$this->forge->dropTable('forge_test_users', true);

$this->forge->addField([
'id' => [
'type' => 'INTEGER',
'constraint' => 11,
],
'second_id' => [
'type' => 'VARCHAR',
'constraint' => 50,
],
'name' => [
'type' => 'VARCHAR',
'constraint' => 255,
],
]);
$primaryKeys = ['id', 'second_id'];
$this->forge->addPrimaryKey($primaryKeys);
$this->forge->createTable('forge_test_users', true);

$indexes = $this->db->getIndexData('forge_test_users');

$this->assertCount(1, $indexes);
$this->assertSame($primaryKeys, current($indexes)->fields);

$this->forge->dropPrimaryKey('forge_test_users');

$indexes = $this->db->getIndexData('forge_test_users');

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

$this->forge->dropTable('forge_test_users', true);
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Database
- SQLite :ref:`BaseConnection::getIndexData() <db-metadata-getindexdata>` now can return pseudo index named ``PRIMARY`` for `AUTOINCREMENT` column, and each returned index data has ``type`` property.
- SQLSRV now automatically drops ``DEFAULT`` constraint when using :ref:`Forge::dropColumn() <db-forge-dropColumn>`.
- ``BaseConnection::escape()`` now excludes the ``RawSql`` data type. This allows passing SQL strings into data.
- The new method ``Forge::dropPrimaryKey()`` allows dropping the primary key on a table.

Model
=====
Expand Down
7 changes: 7 additions & 0 deletions user_guide_src/source/dbmgmt/forge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ Execute a DROP KEY.

.. literalinclude:: forge/020.php

Dropping a Primary Key
======================

Execute a DROP PRIMARY KEY.

.. literalinclude:: forge/028.php

Renaming a Table
================

Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/dbmgmt/forge/028.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

// MySqli Produces: ALTER TABLE `tablename` DROP PRIMARY KEY
// Others Produces: ALTER TABLE `tablename` DROP CONSTRAINT `pk_tablename`
$forge->dropPrimaryKey('tablename');