Skip to content

Commit 712ab9c

Browse files
committed
feat: createDatabase() and dropDatabase() supports database name with dots
1 parent 371299e commit 712ab9c

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎system/Database/Forge.php‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,14 @@ public function createDatabase(string $dbName, bool $ifNotExists = false): bool
229229
}
230230

231231
try {
232-
if (! $this->db->query(sprintf($ifNotExists ? $this->createDatabaseIfStr : $this->createDatabaseStr, $dbName, $this->db->charset, $this->db->DBCollat))) {
232+
if (! $this->db->query(
233+
sprintf(
234+
$ifNotExists ? $this->createDatabaseIfStr : $this->createDatabaseStr,
235+
$this->db->escapeIdentifier($dbName),
236+
$this->db->charset,
237+
$this->db->DBCollat
238+
)
239+
)) {
233240
// @codeCoverageIgnoreStart
234241
if ($this->db->DBDebug) {
235242
throw new DatabaseException('Unable to create the specified database.');
@@ -268,7 +275,10 @@ private function databaseExists(string $dbName): bool
268275
return false;
269276
}
270277

271-
return $this->db->query($this->checkDatabaseExistStr, $dbName)->getRow() !== null;
278+
return $this->db->query(
279+
$this->checkDatabaseExistStr,
280+
$this->db->escapeIdentifier($dbName)
281+
)->getRow() !== null;
272282
}
273283

274284
/**
@@ -286,7 +296,9 @@ public function dropDatabase(string $dbName): bool
286296
return false;
287297
}
288298

289-
if (! $this->db->query(sprintf($this->dropDatabaseStr, $dbName))) {
299+
if (! $this->db->query(
300+
sprintf($this->dropDatabaseStr, $this->db->escapeIdentifier($dbName))
301+
)) {
290302
if ($this->db->DBDebug) {
291303
throw new DatabaseException('Unable to drop the specified database.');
292304
}
@@ -295,7 +307,11 @@ public function dropDatabase(string $dbName): bool
295307
}
296308

297309
if (! empty($this->db->dataCache['db_names'])) {
298-
$key = array_search(strtolower($dbName), array_map('strtolower', $this->db->dataCache['db_names']), true);
310+
$key = array_search(
311+
strtolower($dbName),
312+
array_map('strtolower', $this->db->dataCache['db_names']),
313+
true
314+
);
299315
if ($key !== false) {
300316
unset($this->db->dataCache['db_names'][$key]);
301317
}

‎tests/system/Database/Live/ForgeTest.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ public function testCreateDatabase(): void
6060
$this->assertTrue($databaseCreated);
6161
}
6262

63+
public function testCreateDatabaseWithDots(): void
64+
{
65+
if ($this->db->DBDriver === 'OCI8') {
66+
$this->markTestSkipped('OCI8 does not support create database.');
67+
}
68+
69+
$dbName = 'test_com.sitedb.web';
70+
71+
$databaseCreated = $this->forge->createDatabase($dbName);
72+
73+
$this->assertTrue($databaseCreated);
74+
75+
if ($this->db->DBDriver !== 'SQLite3') {
76+
$this->forge->dropDatabase($dbName);
77+
}
78+
}
79+
6380
public function testCreateDatabaseIfNotExists(): void
6481
{
6582
if ($this->db->DBDriver === 'OCI8') {
@@ -95,6 +112,24 @@ public function testCreateDatabaseIfNotExistsWithDb(): void
95112
}
96113
}
97114

115+
public function testCreateDatabaseIfNotExistsWithDbWithDots(): void
116+
{
117+
if ($this->db->DBDriver === 'OCI8') {
118+
$this->markTestSkipped('OCI8 does not support create database.');
119+
}
120+
121+
$dbName = 'test_forge.database.exist';
122+
123+
$this->forge->createDatabase($dbName);
124+
$databaseExists = $this->forge->createDatabase($dbName, true);
125+
126+
$this->assertTrue($databaseExists);
127+
128+
if ($this->db->DBDriver !== 'SQLite3') {
129+
$this->forge->dropDatabase($dbName);
130+
}
131+
}
132+
98133
public function testDropDatabase(): void
99134
{
100135
if ($this->db->DBDriver === 'OCI8') {

0 commit comments

Comments
 (0)