Skip to content

Commit 3bddc85

Browse files
committed
Fix create table if not exists when indexes exist
1 parent d75e426 commit 3bddc85

8 files changed

Lines changed: 69 additions & 35 deletions

File tree

phpstan-baseline.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ parameters:
337337

338338
-
339339
message: "#^Access to an undefined property CodeIgniter\\\\Database\\\\BaseConnection\\:\\:\\$schema\\.$#"
340-
count: 14
340+
count: 13
341341
path: system/Database/SQLSRV/Forge.php
342342

343343
-

system/Database/Forge.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ class Forge
107107
protected $createTableStr = "%s %s (%s\n)";
108108

109109
/**
110-
* CREATE TABLE IF statement
111-
*
112-
* @var bool|string
110+
* @deprecated This is no longer used.
113111
*/
114112
protected $createTableIfStr = 'CREATE TABLE IF NOT EXISTS';
115113

@@ -495,7 +493,14 @@ public function createTable(string $table, bool $ifNotExists = false, array $att
495493
throw new RuntimeException('Field information is required.');
496494
}
497495

498-
$sql = $this->_createTable($table, $ifNotExists, $attributes);
496+
// If table exists lets stop here
497+
if ($ifNotExists === true && $this->db->tableExists($table)) {
498+
$this->reset();
499+
500+
return true;
501+
}
502+
503+
$sql = $this->_createTable($table, false, $attributes);
499504

500505
if (is_bool($sql)) {
501506
$this->reset();
@@ -530,20 +535,12 @@ public function createTable(string $table, bool $ifNotExists = false, array $att
530535

531536
/**
532537
* @return bool|string
538+
*
539+
* @deprecated $ifNotExists is no longer used, and will be removed.
533540
*/
534541
protected function _createTable(string $table, bool $ifNotExists, array $attributes)
535542
{
536-
// For any platforms that don't support Create If Not Exists...
537-
if ($ifNotExists === true && $this->createTableIfStr === false) {
538-
if ($this->db->tableExists($table)) {
539-
return true;
540-
}
541-
542-
$ifNotExists = false;
543-
}
544-
545-
$sql = ($ifNotExists) ? sprintf($this->createTableIfStr, $this->db->escapeIdentifiers($table))
546-
: 'CREATE TABLE';
543+
$sql = 'CREATE TABLE';
547544

548545
$columns = $this->_processFields(true);
549546

system/Database/OCI8/Forge.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ class Forge extends BaseForge
4040
protected $createTableIfStr = false;
4141

4242
/**
43-
* DROP TABLE IF EXISTS statement
44-
*
45-
* @var false
43+
* @deprecated This is no longer used.
4644
*/
4745
protected $dropTableIfStr = false;
4846

system/Database/SQLSRV/Forge.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,14 @@ class Forge extends BaseForge
9191
protected $createTableIfStr;
9292

9393
/**
94-
* CREATE TABLE statement
95-
*
96-
* @var string
94+
* @deprecated This is no longer used.
9795
*/
9896
protected $createTableStr;
9997

10098
public function __construct(BaseConnection $db)
10199
{
102100
parent::__construct($db);
103101

104-
$this->createTableIfStr = 'IF NOT EXISTS'
105-
. '(SELECT t.name, s.name as schema_name, t.type_desc '
106-
. 'FROM sys.tables t '
107-
. 'INNER JOIN sys.schemas s on s.schema_id = t.schema_id '
108-
. "WHERE s.name=N'" . $this->db->schema . "' "
109-
. "AND t.name=REPLACE(N'%s', '\"', '') "
110-
. "AND t.type_desc='USER_TABLE')\nCREATE TABLE ";
111-
112102
$this->createTableStr = '%s ' . $this->db->escapeIdentifiers($this->db->schema) . ".%s (%s\n) ";
113103
$this->renameTableStr = 'EXEC sp_rename [' . $this->db->escapeIdentifiers($this->db->schema) . '.%s] , %s ;';
114104

system/Database/SQLite3/Forge.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public function __construct(BaseConnection $db)
5656
parent::__construct($db);
5757

5858
if (version_compare($this->db->getVersion(), '3.3', '<')) {
59-
$this->createTableIfStr = false;
60-
$this->dropTableIfStr = false;
59+
$this->dropTableIfStr = false;
6160
}
6261
}
6362

user_guide_src/source/dbmgmt/forge.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ with
191191

192192
.. literalinclude:: forge/014.php
193193

194-
An optional second parameter set to true adds an ``IF NOT EXISTS`` clause
195-
into the definition
194+
An optional second parameter set to true will only execute create table statement if
195+
the table name is not in the array of know tables
196196

197197
.. literalinclude:: forge/015.php
198198

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

33
$forge->createTable('table_name', true);
4-
// gives CREATE TABLE IF NOT EXISTS table_name
4+
// creates table only if table does not exist
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#############################
2+
Upgrading from 4.2.1 to 4.2.2
3+
#############################
4+
5+
Please refer to the upgrade instructions corresponding to your installation method.
6+
7+
- :ref:`Composer Installation App Starter Upgrading <app-starter-upgrading>`
8+
- :ref:`Composer Installation Adding CodeIgniter4 to an Existing Project Upgrading <adding-codeigniter4-upgrading>`
9+
- :ref:`Manual Installation Upgrading <installing-manual-upgrading>`
10+
11+
.. contents::
12+
:local:
13+
:depth: 2
14+
15+
Mandatory File Changes
16+
**********************
17+
18+
19+
Breaking Changes
20+
****************
21+
22+
- The method ``Forge::createTable()`` no longer executes a ``CREATE TABLE IF NOT EXISTS``. If table is not found in ``$db->tableExists($table)`` then ``CREATE TABLE`` is executed.
23+
- The method signature of ``Forge::_createTable()`` is deprecated. The ``bool`` ``$ifNotExists`` is no longer used and will be removed in a future release.
24+
25+
Breaking Enhancements
26+
*********************
27+
28+
29+
Project Files
30+
*************
31+
32+
Numerous files in the **project space** (root, app, public, writable) received updates. Due to
33+
these files being outside of the **system** scope they will not be changed without your intervention.
34+
There are some third-party CodeIgniter modules available to assist with merging changes to
35+
the project space: `Explore on Packagist <https://packagist.org/explore/?query=codeigniter4%20updates>`_.
36+
37+
.. note:: Except in very rare cases for bug fixes, no changes made to files for the project space
38+
will break your application. All changes noted here are optional until the next major version,
39+
and any mandatory changes will be covered in the sections above.
40+
41+
Content Changes
42+
===============
43+
44+
45+
All Changes
46+
===========
47+
48+
This is a list of all files in the **project space** that received changes;
49+
many will be simple comments or formatting that have no effect on the runtime:
50+

0 commit comments

Comments
 (0)