Skip to content

Commit

Permalink
Fix create table if not exists when indexes exist
Browse files Browse the repository at this point in the history
  • Loading branch information
sclubricants committed Jul 12, 2022
1 parent d75e426 commit 3bddc85
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 35 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ parameters:

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

-
Expand Down
27 changes: 12 additions & 15 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ class Forge
protected $createTableStr = "%s %s (%s\n)";

/**
* CREATE TABLE IF statement
*
* @var bool|string
* @deprecated This is no longer used.
*/
protected $createTableIfStr = 'CREATE TABLE IF NOT EXISTS';

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

$sql = $this->_createTable($table, $ifNotExists, $attributes);
// If table exists lets stop here
if ($ifNotExists === true && $this->db->tableExists($table)) {
$this->reset();

return true;
}

$sql = $this->_createTable($table, false, $attributes);

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

/**
* @return bool|string
*
* @deprecated $ifNotExists is no longer used, and will be removed.
*/
protected function _createTable(string $table, bool $ifNotExists, array $attributes)
{
// For any platforms that don't support Create If Not Exists...
if ($ifNotExists === true && $this->createTableIfStr === false) {
if ($this->db->tableExists($table)) {
return true;
}

$ifNotExists = false;
}

$sql = ($ifNotExists) ? sprintf($this->createTableIfStr, $this->db->escapeIdentifiers($table))
: 'CREATE TABLE';
$sql = 'CREATE TABLE';

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

Expand Down
4 changes: 1 addition & 3 deletions system/Database/OCI8/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class Forge extends BaseForge
protected $createTableIfStr = false;

/**
* DROP TABLE IF EXISTS statement
*
* @var false
* @deprecated This is no longer used.
*/
protected $dropTableIfStr = false;

Expand Down
12 changes: 1 addition & 11 deletions system/Database/SQLSRV/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,14 @@ class Forge extends BaseForge
protected $createTableIfStr;

/**
* CREATE TABLE statement
*
* @var string
* @deprecated This is no longer used.
*/
protected $createTableStr;

public function __construct(BaseConnection $db)
{
parent::__construct($db);

$this->createTableIfStr = 'IF NOT EXISTS'
. '(SELECT t.name, s.name as schema_name, t.type_desc '
. 'FROM sys.tables t '
. 'INNER JOIN sys.schemas s on s.schema_id = t.schema_id '
. "WHERE s.name=N'" . $this->db->schema . "' "
. "AND t.name=REPLACE(N'%s', '\"', '') "
. "AND t.type_desc='USER_TABLE')\nCREATE TABLE ";

$this->createTableStr = '%s ' . $this->db->escapeIdentifiers($this->db->schema) . ".%s (%s\n) ";
$this->renameTableStr = 'EXEC sp_rename [' . $this->db->escapeIdentifiers($this->db->schema) . '.%s] , %s ;';

Expand Down
3 changes: 1 addition & 2 deletions system/Database/SQLite3/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public function __construct(BaseConnection $db)
parent::__construct($db);

if (version_compare($this->db->getVersion(), '3.3', '<')) {
$this->createTableIfStr = false;
$this->dropTableIfStr = false;
$this->dropTableIfStr = false;
}
}

Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/dbmgmt/forge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ with

.. literalinclude:: forge/014.php

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

.. literalinclude:: forge/015.php

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/dbmgmt/forge/015.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

$forge->createTable('table_name', true);
// gives CREATE TABLE IF NOT EXISTS table_name
// creates table only if table does not exist
50 changes: 50 additions & 0 deletions user_guide_src/source/installation/upgrade_422.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#############################
Upgrading from 4.2.1 to 4.2.2
#############################

Please refer to the upgrade instructions corresponding to your installation method.

- :ref:`Composer Installation App Starter Upgrading <app-starter-upgrading>`
- :ref:`Composer Installation Adding CodeIgniter4 to an Existing Project Upgrading <adding-codeigniter4-upgrading>`
- :ref:`Manual Installation Upgrading <installing-manual-upgrading>`

.. contents::
:local:
:depth: 2

Mandatory File Changes
**********************


Breaking Changes
****************

- 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.
- The method signature of ``Forge::_createTable()`` is deprecated. The ``bool`` ``$ifNotExists`` is no longer used and will be removed in a future release.

Breaking Enhancements
*********************


Project Files
*************

Numerous files in the **project space** (root, app, public, writable) received updates. Due to
these files being outside of the **system** scope they will not be changed without your intervention.
There are some third-party CodeIgniter modules available to assist with merging changes to
the project space: `Explore on Packagist <https://packagist.org/explore/?query=codeigniter4%20updates>`_.

.. note:: Except in very rare cases for bug fixes, no changes made to files for the project space
will break your application. All changes noted here are optional until the next major version,
and any mandatory changes will be covered in the sections above.

Content Changes
===============


All Changes
===========

This is a list of all files in the **project space** that received changes;
many will be simple comments or formatting that have no effect on the runtime:

0 comments on commit 3bddc85

Please sign in to comment.