Skip to content

Commit 5886a81

Browse files
committed
drop table cascade and unit tests
1 parent 3b61e7a commit 5886a81

File tree

5 files changed

+131
-6
lines changed

5 files changed

+131
-6
lines changed

system/Database/BaseConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ public function fieldExists($fieldName, $tableName)
15871587
*/
15881588
public function getFieldData(string $table)
15891589
{
1590-
$fields = $this->_fieldData($this->protectIdentifiers($table, true, null, false));
1590+
$fields = $this->_fieldData($this->protectIdentifiers($table, true, false, false));
15911591

15921592
return $fields ?? false;
15931593
}

system/Database/Forge.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,11 @@ protected function _createTableAttributes($attributes)
535535
*
536536
* @param string $table_name Table name
537537
* @param bool $if_exists Whether to add an IF EXISTS condition
538+
* @param bool $cascade Whether to add an CASCADE condition
538539
*
539540
* @return bool
540541
*/
541-
public function dropTable($table_name, $if_exists = false)
542+
public function dropTable($table_name, $if_exists = false, $cascade = false)
542543
{
543544
if ($table_name === '')
544545
{
@@ -549,8 +550,8 @@ public function dropTable($table_name, $if_exists = false)
549550

550551
return false;
551552
}
552-
553-
if (($query = $this->_dropTable($this->db->DBPrefix . $table_name, $if_exists)) === true)
553+
554+
if (($query = $this->_dropTable($this->db->DBPrefix . $table_name, $if_exists, $cascade)) === true)
554555
{
555556
return true;
556557
}
@@ -579,10 +580,11 @@ public function dropTable($table_name, $if_exists = false)
579580
*
580581
* @param string $table Table name
581582
* @param bool $if_exists Whether to add an IF EXISTS condition
583+
* @param bool $cascade Whether to add an CASCADE condition
582584
*
583585
* @return string
584586
*/
585-
protected function _dropTable($table, $if_exists)
587+
protected function _dropTable($table, $if_exists, $cascade)
586588
{
587589
$sql = 'DROP TABLE';
588590

system/Database/Postgre/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ protected function _listColumns(string $table = ''): string
280280
return 'SELECT "column_name"
281281
FROM "information_schema"."columns"
282282
WHERE LOWER("table_name") = '
283-
. $this->escape(strtolower($table));
283+
. $this->escape($this->DBPrefix.strtolower($table));
284284
}
285285

286286
//--------------------------------------------------------------------

system/Database/Postgre/Forge.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,29 @@ protected function _attributeAutoIncrement(&$attributes, &$field)
191191
}
192192

193193
//--------------------------------------------------------------------
194+
195+
/**
196+
* Drop Table
197+
*
198+
* Generates a platform-specific DROP TABLE string
199+
*
200+
* @param string $table Table name
201+
* @param bool $if_exists Whether to add an IF EXISTS condition
202+
*
203+
* @return string
204+
*/
205+
protected function _dropTable($table, $if_exists, $cascade)
206+
{
207+
$sql = parent::_dropTable($table, $if_exists, $cascade);
208+
209+
if($cascade === true)
210+
{
211+
$sql .= ' CASCADE';
212+
}
213+
214+
return $sql;
215+
}
216+
217+
//--------------------------------------------------------------------
218+
194219
}

tests/system/Database/Live/ForgeTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,104 @@ public function setUp()
1414
parent::setUp();
1515
$this->forge = \Config\Database::forge($this->DBGroup);
1616
}
17+
18+
public function testCreateTable()
19+
{
20+
$this->forge->addField([
21+
'name' => [
22+
'type' => 'VARCHAR',
23+
'constraint' => 255,
24+
]
25+
]);
26+
27+
$this->forge->createTable('forge_test_table', true);
28+
29+
$exist = $this->db->tableExists('forge_test_table');
30+
31+
$this->assertTrue($exist);
32+
33+
$this->forge->dropTable('forge_test_table', true);
34+
35+
}
36+
37+
public function testAddFields()
38+
{
39+
40+
$this->forge->dropTable('forge_test_fields', true);
41+
42+
$this->forge->addField([
43+
'id' => [
44+
'type' => 'INTEGER',
45+
'constraint' => 11,
46+
'unsigned' => false,
47+
'auto_increment' => true
48+
],
49+
'username' => [
50+
'type' => 'VARCHAR',
51+
'constraint' => 255,
52+
'unique' => false
53+
],
54+
'name' => [
55+
'type' => 'VARCHAR',
56+
'constraint' => 255,
57+
],
58+
'active' => [
59+
'type' => 'INTEGER',
60+
'constraint' => 11,
61+
'default' => 0
62+
],
63+
]);
64+
65+
$this->forge->addKey('id', true);
66+
$create = $this->forge->createTable('forge_test_fields', true);
67+
68+
//Check Field names
69+
$fieldsNames = $this->db->getFieldNames('forge_test_fields');
70+
$this->assertEquals($fieldsNames, ['id', 'username', 'name', 'active']);
71+
72+
73+
$fieldsData = $this->db->getFieldData('forge_test_fields');
74+
75+
$this->assertEquals($fieldsData[0]->name, 'id');
76+
$this->assertEquals($fieldsData[1]->name, 'username');
77+
78+
$this->assertEquals($fieldsData[3]->default, 0);
79+
80+
if($this->db->DBDriver === 'MySQLi')
81+
{
82+
//Check types
83+
$this->assertEquals($fieldsData[0]->type, 'int');
84+
$this->assertEquals($fieldsData[1]->type, 'varchar');
85+
86+
$this->assertEquals($fieldsData[0]->max_length, 11);
87+
88+
$this->assertEquals($fieldsData[0]->default, NULL);
89+
$this->assertEquals($fieldsData[1]->default, NULL);
90+
91+
$this->assertEquals($fieldsData[0]->primary_key, 1);
92+
93+
$this->assertEquals($fieldsData[1]->max_length, 255);
94+
95+
} elseif ($this->db->DBDriver === 'Postgre')
96+
{
97+
//Check types
98+
$this->assertEquals($fieldsData[0]->type, 'integer');
99+
$this->assertEquals($fieldsData[1]->type, 'character varying');
100+
101+
$this->assertEquals($fieldsData[0]->max_length, 32);
102+
103+
//$this->assertEquals($fieldsData[0]->default, NULL);
104+
$this->assertEquals($fieldsData[1]->default, NULL);
105+
106+
$this->assertEquals($fieldsData[1]->max_length, 255);
107+
}else
108+
{
109+
$this->assertTrue(false, "DB Driver not supported");
110+
}
111+
112+
$this->forge->dropTable('forge_test_fields', true);
113+
114+
}
17115

18116
public function testCompositeKey()
19117
{

0 commit comments

Comments
 (0)