Skip to content

Commit 0045a6f

Browse files
authored
Merge pull request #5171 from ytetsuro/fix/#5164_add_dropKey_method
Add `dropKey` method to `Forge`
2 parents 87ffde8 + 00b6e32 commit 0045a6f

8 files changed

Lines changed: 117 additions & 0 deletions

File tree

system/Database/Forge.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ class Forge
167167
*/
168168
protected $dropConstraintStr;
169169

170+
/**
171+
* DROP INDEX statement
172+
*
173+
* @var string
174+
*/
175+
protected $dropIndexStr = 'DROP INDEX %s ON %s';
176+
170177
/**
171178
* Constructor.
172179
*/
@@ -421,6 +428,32 @@ public function addForeignKey($fieldName = '', string $tableName = '', $tableFie
421428
return $this;
422429
}
423430

431+
/**
432+
* Drop Key
433+
*
434+
* @throws DatabaseException
435+
*
436+
* @return bool
437+
*/
438+
public function dropKey(string $table, string $keyName)
439+
{
440+
$sql = sprintf(
441+
$this->dropIndexStr,
442+
$this->db->escapeIdentifiers($this->db->DBPrefix . $keyName),
443+
$this->db->escapeIdentifiers($this->db->DBPrefix . $table),
444+
);
445+
446+
if ($sql === '') {
447+
if ($this->db->DBDebug) {
448+
throw new DatabaseException('This feature is not available for the database you are using.');
449+
}
450+
451+
return false;
452+
}
453+
454+
return $this->db->query($sql);
455+
}
456+
424457
/**
425458
* @throws DatabaseException
426459
*

system/Database/MySQLi/Forge.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,20 @@ protected function _processIndexes(string $table): string
220220

221221
return $sql;
222222
}
223+
224+
/**
225+
* Drop Key
226+
*
227+
* @return bool
228+
*/
229+
public function dropKey(string $table, string $keyName)
230+
{
231+
$sql = sprintf(
232+
$this->dropIndexStr,
233+
$this->db->escapeIdentifiers($keyName),
234+
$this->db->escapeIdentifiers($this->db->DBPrefix . $table),
235+
);
236+
237+
return $this->db->query($sql);
238+
}
223239
}

system/Database/Postgre/Forge.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class Forge extends BaseForge
3232
*/
3333
protected $dropConstraintStr = 'ALTER TABLE %s DROP CONSTRAINT %s';
3434

35+
/**
36+
* DROP INDEX statement
37+
*
38+
* @var string
39+
*/
40+
protected $dropIndexStr = 'DROP INDEX %s';
41+
3542
/**
3643
* UNSIGNED support
3744
*

system/Database/SQLSRV/Forge.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class Forge extends BaseForge
2626
*/
2727
protected $dropConstraintStr;
2828

29+
/**
30+
* DROP INDEX statement
31+
*
32+
* @var string
33+
*/
34+
protected $dropIndexStr;
35+
2936
/**
3037
* CREATE DATABASE IF statement
3138
*
@@ -106,6 +113,7 @@ public function __construct(BaseConnection $db)
106113
$this->renameTableStr = 'EXEC sp_rename [' . $this->db->escapeIdentifiers($this->db->schema) . '.%s] , %s ;';
107114

108115
$this->dropConstraintStr = 'ALTER TABLE ' . $this->db->escapeIdentifiers($this->db->schema) . '.%s DROP CONSTRAINT %s';
116+
$this->dropIndexStr = 'DROP INDEX %s ON ' . $this->db->escapeIdentifiers($this->db->schema) . '.%s';
109117
}
110118

111119
/**

system/Database/SQLite3/Forge.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
*/
2121
class Forge extends BaseForge
2222
{
23+
/**
24+
* DROP INDEX statement
25+
*
26+
* @var string
27+
*/
28+
protected $dropIndexStr = 'DROP INDEX %s';
29+
2330
/**
2431
* @var Connection
2532
*/

tests/system/Database/Live/ForgeTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,4 +1067,38 @@ public function testDropMultipleColumnWithString()
10671067

10681068
$this->forge->dropTable('forge_test_four', true);
10691069
}
1070+
1071+
public function testDropKey()
1072+
{
1073+
$this->forge->dropTable('key_test_users', true);
1074+
$keyName = 'key_test_users_id';
1075+
1076+
$attributes = [];
1077+
1078+
if ($this->db->DBDriver === 'MySQLi') {
1079+
$keyName = 'id';
1080+
$attributes = ['ENGINE' => 'InnoDB'];
1081+
}
1082+
1083+
$this->forge->addField([
1084+
'id' => [
1085+
'type' => 'INTEGER',
1086+
'constraint' => 11,
1087+
],
1088+
'name' => [
1089+
'type' => 'VARCHAR',
1090+
'constraint' => 255,
1091+
],
1092+
]);
1093+
$this->forge->addKey('id');
1094+
$this->forge->createTable('key_test_users', true, $attributes);
1095+
1096+
$this->forge->dropKey('key_test_users', $keyName);
1097+
1098+
$foreignKeyData = $this->db->getIndexData('key_test_users');
1099+
1100+
$this->assertEmpty($foreignKeyData);
1101+
1102+
$this->forge->dropTable('key_test_users', true);
1103+
}
10701104
}

user_guide_src/source/changelogs/v4.1.5.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Enhancements:
99

1010
- Added Cache config for reserved characters
1111
- The ``addForeignKey`` function of the ``Forge`` class can now define composite foreign keys in an array
12+
- The ``dropKey`` function of the ``Forge`` class can remove key
1213

1314
Changes:
1415

user_guide_src/source/dbmgmt/forge.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ Execute a DROP FOREIGN KEY.
304304
// Produces: ALTER TABLE 'tablename' DROP FOREIGN KEY 'users_foreign'
305305
$forge->dropForeignKey('tablename','users_foreign');
306306

307+
308+
Dropping a Key
309+
======================
310+
311+
Execute a DROP KEY.
312+
313+
::
314+
315+
// Produces: DROP INDEX `users_index` ON `tablename`
316+
$forge->dropKey('tablename','users_index');
317+
307318
Renaming a table
308319
================
309320

0 commit comments

Comments
 (0)