Skip to content

Commit e6893f3

Browse files
authored
Merge pull request #6488 from sclubricants/dropPrimaryKey
Create Forge::dropPrimaryKey()
2 parents b411094 + 6351abd commit e6893f3

File tree

8 files changed

+102
-0
lines changed

8 files changed

+102
-0
lines changed

system/Database/Forge.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,20 @@ public function dropKey(string $table, string $keyName)
456456
return $this->db->query($sql);
457457
}
458458

459+
/**
460+
* Drop Primary Key
461+
*/
462+
public function dropPrimaryKey(string $table): bool
463+
{
464+
$sql = sprintf(
465+
'ALTER TABLE %s DROP CONSTRAINT %s',
466+
$this->db->escapeIdentifiers($this->db->DBPrefix . $table),
467+
$this->db->escapeIdentifiers('pk_' . $this->db->DBPrefix . $table),
468+
);
469+
470+
return $this->db->query($sql);
471+
}
472+
459473
/**
460474
* @throws DatabaseException
461475
*

system/Database/MySQLi/Forge.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,17 @@ public function dropKey(string $table, string $keyName)
236236

237237
return $this->db->query($sql);
238238
}
239+
240+
/**
241+
* Drop Primary Key
242+
*/
243+
public function dropPrimaryKey(string $table): bool
244+
{
245+
$sql = sprintf(
246+
'ALTER TABLE %s DROP PRIMARY KEY',
247+
$this->db->escapeIdentifiers($this->db->DBPrefix . $table)
248+
);
249+
250+
return $this->db->query($sql);
251+
}
239252
}

system/Database/SQLite3/Forge.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,16 @@ public function dropForeignKey(string $table, string $foreignName): bool
250250
->dropForeignKey($foreignName)
251251
->run();
252252
}
253+
254+
/**
255+
* Drop Primary Key
256+
*/
257+
public function dropPrimaryKey(string $table): bool
258+
{
259+
$sqlTable = new Table($this->db, $this);
260+
261+
return $sqlTable->fromTable($this->db->DBPrefix . $table)
262+
->dropPrimaryKey()
263+
->run();
264+
}
253265
}

system/Database/SQLite3/Table.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ public function modifyColumn(array $field)
194194
return $this;
195195
}
196196

197+
/**
198+
* Drops the primary key
199+
*/
200+
public function dropPrimaryKey(): Table
201+
{
202+
$primaryIndexes = array_filter($this->keys, static fn ($index) => strtolower($index['type']) === 'primary');
203+
204+
foreach (array_keys($primaryIndexes) as $key) {
205+
unset($this->keys[$key]);
206+
}
207+
208+
return $this;
209+
}
210+
197211
/**
198212
* Drops a foreign key from this table so that
199213
* it won't be recreated in the future.

tests/system/Database/Live/ForgeTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,4 +1372,40 @@ public function testAddTextColumnWithConstraint()
13721372

13731373
$this->assertFalse($this->db->fieldExists('text_with_constraint', 'user'));
13741374
}
1375+
1376+
public function testDropPrimaryKey()
1377+
{
1378+
$this->forge->dropTable('forge_test_users', true);
1379+
1380+
$this->forge->addField([
1381+
'id' => [
1382+
'type' => 'INTEGER',
1383+
'constraint' => 11,
1384+
],
1385+
'second_id' => [
1386+
'type' => 'VARCHAR',
1387+
'constraint' => 50,
1388+
],
1389+
'name' => [
1390+
'type' => 'VARCHAR',
1391+
'constraint' => 255,
1392+
],
1393+
]);
1394+
$primaryKeys = ['id', 'second_id'];
1395+
$this->forge->addPrimaryKey($primaryKeys);
1396+
$this->forge->createTable('forge_test_users', true);
1397+
1398+
$indexes = $this->db->getIndexData('forge_test_users');
1399+
1400+
$this->assertCount(1, $indexes);
1401+
$this->assertSame($primaryKeys, current($indexes)->fields);
1402+
1403+
$this->forge->dropPrimaryKey('forge_test_users');
1404+
1405+
$indexes = $this->db->getIndexData('forge_test_users');
1406+
1407+
$this->assertCount(0, $indexes);
1408+
1409+
$this->forge->dropTable('forge_test_users', true);
1410+
}
13751411
}

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Database
7272
- 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.
7373
- SQLSRV now automatically drops ``DEFAULT`` constraint when using :ref:`Forge::dropColumn() <db-forge-dropColumn>`.
7474
- ``BaseConnection::escape()`` now excludes the ``RawSql`` data type. This allows passing SQL strings into data.
75+
- The new method ``Forge::dropPrimaryKey()`` allows dropping the primary key on a table.
7576

7677
Model
7778
=====

user_guide_src/source/dbmgmt/forge.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ Execute a DROP KEY.
229229

230230
.. literalinclude:: forge/020.php
231231

232+
Dropping a Primary Key
233+
======================
234+
235+
Execute a DROP PRIMARY KEY.
236+
237+
.. literalinclude:: forge/028.php
238+
232239
Renaming a Table
233240
================
234241

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// MySqli Produces: ALTER TABLE `tablename` DROP PRIMARY KEY
4+
// Others Produces: ALTER TABLE `tablename` DROP CONSTRAINT `pk_tablename`
5+
$forge->dropPrimaryKey('tablename');

0 commit comments

Comments
 (0)