Skip to content

Commit

Permalink
Merge pull request #5171 from ytetsuro/fix/#5164_add_dropKey_method
Browse files Browse the repository at this point in the history
Add `dropKey` method to `Forge`
  • Loading branch information
lonnieezell authored Oct 15, 2021
2 parents 87ffde8 + 00b6e32 commit 0045a6f
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 0 deletions.
33 changes: 33 additions & 0 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ class Forge
*/
protected $dropConstraintStr;

/**
* DROP INDEX statement
*
* @var string
*/
protected $dropIndexStr = 'DROP INDEX %s ON %s';

/**
* Constructor.
*/
Expand Down Expand Up @@ -421,6 +428,32 @@ public function addForeignKey($fieldName = '', string $tableName = '', $tableFie
return $this;
}

/**
* Drop Key
*
* @throws DatabaseException
*
* @return bool
*/
public function dropKey(string $table, string $keyName)
{
$sql = sprintf(
$this->dropIndexStr,
$this->db->escapeIdentifiers($this->db->DBPrefix . $keyName),
$this->db->escapeIdentifiers($this->db->DBPrefix . $table),
);

if ($sql === '') {
if ($this->db->DBDebug) {
throw new DatabaseException('This feature is not available for the database you are using.');
}

return false;
}

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

/**
* @throws DatabaseException
*
Expand Down
16 changes: 16 additions & 0 deletions system/Database/MySQLi/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,20 @@ protected function _processIndexes(string $table): string

return $sql;
}

/**
* Drop Key
*
* @return bool
*/
public function dropKey(string $table, string $keyName)
{
$sql = sprintf(
$this->dropIndexStr,
$this->db->escapeIdentifiers($keyName),
$this->db->escapeIdentifiers($this->db->DBPrefix . $table),
);

return $this->db->query($sql);
}
}
7 changes: 7 additions & 0 deletions system/Database/Postgre/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class Forge extends BaseForge
*/
protected $dropConstraintStr = 'ALTER TABLE %s DROP CONSTRAINT %s';

/**
* DROP INDEX statement
*
* @var string
*/
protected $dropIndexStr = 'DROP INDEX %s';

/**
* UNSIGNED support
*
Expand Down
8 changes: 8 additions & 0 deletions system/Database/SQLSRV/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class Forge extends BaseForge
*/
protected $dropConstraintStr;

/**
* DROP INDEX statement
*
* @var string
*/
protected $dropIndexStr;

/**
* CREATE DATABASE IF statement
*
Expand Down Expand Up @@ -106,6 +113,7 @@ public function __construct(BaseConnection $db)
$this->renameTableStr = 'EXEC sp_rename [' . $this->db->escapeIdentifiers($this->db->schema) . '.%s] , %s ;';

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

/**
Expand Down
7 changes: 7 additions & 0 deletions system/Database/SQLite3/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
*/
class Forge extends BaseForge
{
/**
* DROP INDEX statement
*
* @var string
*/
protected $dropIndexStr = 'DROP INDEX %s';

/**
* @var Connection
*/
Expand Down
34 changes: 34 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1067,4 +1067,38 @@ public function testDropMultipleColumnWithString()

$this->forge->dropTable('forge_test_four', true);
}

public function testDropKey()
{
$this->forge->dropTable('key_test_users', true);
$keyName = 'key_test_users_id';

$attributes = [];

if ($this->db->DBDriver === 'MySQLi') {
$keyName = 'id';
$attributes = ['ENGINE' => 'InnoDB'];
}

$this->forge->addField([
'id' => [
'type' => 'INTEGER',
'constraint' => 11,
],
'name' => [
'type' => 'VARCHAR',
'constraint' => 255,
],
]);
$this->forge->addKey('id');
$this->forge->createTable('key_test_users', true, $attributes);

$this->forge->dropKey('key_test_users', $keyName);

$foreignKeyData = $this->db->getIndexData('key_test_users');

$this->assertEmpty($foreignKeyData);

$this->forge->dropTable('key_test_users', true);
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Enhancements:

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

Changes:

Expand Down
11 changes: 11 additions & 0 deletions user_guide_src/source/dbmgmt/forge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ Execute a DROP FOREIGN KEY.
// Produces: ALTER TABLE 'tablename' DROP FOREIGN KEY 'users_foreign'
$forge->dropForeignKey('tablename','users_foreign');


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

Execute a DROP KEY.

::

// Produces: DROP INDEX `users_index` ON `tablename`
$forge->dropKey('tablename','users_index');

Renaming a table
================

Expand Down

0 comments on commit 0045a6f

Please sign in to comment.