From 3bddc85bacf7fc1354887f8ebc3711cb56aa1627 Mon Sep 17 00:00:00 2001 From: sclubricants Date: Sun, 10 Jul 2022 14:10:35 -0700 Subject: [PATCH 1/5] Fix create table if not exists when indexes exist --- phpstan-baseline.neon.dist | 2 +- system/Database/Forge.php | 27 +++++----- system/Database/OCI8/Forge.php | 4 +- system/Database/SQLSRV/Forge.php | 12 +---- system/Database/SQLite3/Forge.php | 3 +- user_guide_src/source/dbmgmt/forge.rst | 4 +- user_guide_src/source/dbmgmt/forge/015.php | 2 +- .../source/installation/upgrade_422.rst | 50 +++++++++++++++++++ 8 files changed, 69 insertions(+), 35 deletions(-) create mode 100644 user_guide_src/source/installation/upgrade_422.rst diff --git a/phpstan-baseline.neon.dist b/phpstan-baseline.neon.dist index 713e54cb0142..da08082b3e08 100644 --- a/phpstan-baseline.neon.dist +++ b/phpstan-baseline.neon.dist @@ -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 - diff --git a/system/Database/Forge.php b/system/Database/Forge.php index 0ffd245fa7a0..8678022b5d19 100644 --- a/system/Database/Forge.php +++ b/system/Database/Forge.php @@ -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'; @@ -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(); @@ -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); diff --git a/system/Database/OCI8/Forge.php b/system/Database/OCI8/Forge.php index 9cd5bad945cd..5fd7c5d0b8a5 100644 --- a/system/Database/OCI8/Forge.php +++ b/system/Database/OCI8/Forge.php @@ -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; diff --git a/system/Database/SQLSRV/Forge.php b/system/Database/SQLSRV/Forge.php index 67ef057e12de..63130fd87289 100755 --- a/system/Database/SQLSRV/Forge.php +++ b/system/Database/SQLSRV/Forge.php @@ -91,9 +91,7 @@ class Forge extends BaseForge protected $createTableIfStr; /** - * CREATE TABLE statement - * - * @var string + * @deprecated This is no longer used. */ protected $createTableStr; @@ -101,14 +99,6 @@ 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 ;'; diff --git a/system/Database/SQLite3/Forge.php b/system/Database/SQLite3/Forge.php index 1f48f20bd54e..154c9f97b554 100644 --- a/system/Database/SQLite3/Forge.php +++ b/system/Database/SQLite3/Forge.php @@ -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; } } diff --git a/user_guide_src/source/dbmgmt/forge.rst b/user_guide_src/source/dbmgmt/forge.rst index 804a0e6ac9d1..6464af2f99a8 100644 --- a/user_guide_src/source/dbmgmt/forge.rst +++ b/user_guide_src/source/dbmgmt/forge.rst @@ -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 diff --git a/user_guide_src/source/dbmgmt/forge/015.php b/user_guide_src/source/dbmgmt/forge/015.php index ad478406a903..ec116bf6d00d 100644 --- a/user_guide_src/source/dbmgmt/forge/015.php +++ b/user_guide_src/source/dbmgmt/forge/015.php @@ -1,4 +1,4 @@ createTable('table_name', true); -// gives CREATE TABLE IF NOT EXISTS table_name +// creates table only if table does not exist diff --git a/user_guide_src/source/installation/upgrade_422.rst b/user_guide_src/source/installation/upgrade_422.rst new file mode 100644 index 000000000000..374f7703e97b --- /dev/null +++ b/user_guide_src/source/installation/upgrade_422.rst @@ -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 ` +- :ref:`Composer Installation Adding CodeIgniter4 to an Existing Project Upgrading ` +- :ref:`Manual Installation 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 `_. + +.. 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: + From fbf292f6c6f87a6b84fcf3028647cd482114cf74 Mon Sep 17 00:00:00 2001 From: sclubricants Date: Tue, 12 Jul 2022 11:31:03 -0700 Subject: [PATCH 2/5] Cleanup create table if not exists when indexes exist --- system/Database/Forge.php | 4 +--- system/Database/OCI8/Forge.php | 8 ++++---- system/Database/SQLSRV/Forge.php | 8 ++++---- user_guide_src/source/dbmgmt/forge.rst | 3 +-- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/system/Database/Forge.php b/system/Database/Forge.php index 8678022b5d19..a30c0f388ffb 100644 --- a/system/Database/Forge.php +++ b/system/Database/Forge.php @@ -540,8 +540,6 @@ public function createTable(string $table, bool $ifNotExists = false, array $att */ protected function _createTable(string $table, bool $ifNotExists, array $attributes) { - $sql = 'CREATE TABLE'; - $columns = $this->_processFields(true); for ($i = 0, $c = count($columns); $i < $c; $i++) { @@ -563,7 +561,7 @@ protected function _createTable(string $table, bool $ifNotExists, array $attribu return sprintf( $this->createTableStr . '%s', - $sql, + 'CREATE TABLE', $this->db->escapeIdentifiers($table), $columns, $this->_createTableAttributes($attributes) diff --git a/system/Database/OCI8/Forge.php b/system/Database/OCI8/Forge.php index 5fd7c5d0b8a5..5dfc1cf77838 100644 --- a/system/Database/OCI8/Forge.php +++ b/system/Database/OCI8/Forge.php @@ -33,14 +33,14 @@ class Forge extends BaseForge protected $createDatabaseStr = false; /** - * CREATE TABLE IF statement - * - * @var false + * @deprecated This is no longer used. */ protected $createTableIfStr = false; /** - * @deprecated This is no longer used. + * DROP TABLE IF EXISTS statement + * + * @var false */ protected $dropTableIfStr = false; diff --git a/system/Database/SQLSRV/Forge.php b/system/Database/SQLSRV/Forge.php index 63130fd87289..f83fdc869494 100755 --- a/system/Database/SQLSRV/Forge.php +++ b/system/Database/SQLSRV/Forge.php @@ -84,14 +84,14 @@ class Forge extends BaseForge ]; /** - * CREATE TABLE IF statement - * - * @var string + * @deprecated This is no longer used. */ protected $createTableIfStr; /** - * @deprecated This is no longer used. + * CREATE TABLE statement + * + * @var string */ protected $createTableStr; diff --git a/user_guide_src/source/dbmgmt/forge.rst b/user_guide_src/source/dbmgmt/forge.rst index 6464af2f99a8..2fb13c72d3e1 100644 --- a/user_guide_src/source/dbmgmt/forge.rst +++ b/user_guide_src/source/dbmgmt/forge.rst @@ -191,8 +191,7 @@ with .. literalinclude:: forge/014.php -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 +An optional second parameter set to true will create the table only if it doesn't already exist. .. literalinclude:: forge/015.php From 0e9335a0bfac45c259ece5e8673ba8bcbe83a369 Mon Sep 17 00:00:00 2001 From: sclubricants Date: Tue, 12 Jul 2022 15:09:20 -0700 Subject: [PATCH 3/5] Fix Comments --- system/Database/Forge.php | 4 ++++ system/Database/OCI8/Forge.php | 4 ++++ system/Database/SQLSRV/Forge.php | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/system/Database/Forge.php b/system/Database/Forge.php index a30c0f388ffb..6f65f676a17c 100644 --- a/system/Database/Forge.php +++ b/system/Database/Forge.php @@ -107,6 +107,10 @@ 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'; diff --git a/system/Database/OCI8/Forge.php b/system/Database/OCI8/Forge.php index 5dfc1cf77838..42393cd63aa9 100644 --- a/system/Database/OCI8/Forge.php +++ b/system/Database/OCI8/Forge.php @@ -33,6 +33,10 @@ class Forge extends BaseForge protected $createDatabaseStr = false; /** + * CREATE TABLE IF statement + * + * @var false + * * @deprecated This is no longer used. */ protected $createTableIfStr = false; diff --git a/system/Database/SQLSRV/Forge.php b/system/Database/SQLSRV/Forge.php index f83fdc869494..95ec9ff0b601 100755 --- a/system/Database/SQLSRV/Forge.php +++ b/system/Database/SQLSRV/Forge.php @@ -84,6 +84,10 @@ class Forge extends BaseForge ]; /** + * CREATE TABLE IF statement + * + * @var string + * * @deprecated This is no longer used. */ protected $createTableIfStr; From 3370dd632373f5962b7e96bcdcfef147fbe2cc59 Mon Sep 17 00:00:00 2001 From: sclubricants Date: Tue, 12 Jul 2022 15:11:25 -0700 Subject: [PATCH 4/5] Update user_guide_src/source/installation/upgrade_422.rst Co-authored-by: kenjis --- user_guide_src/source/installation/upgrade_422.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/installation/upgrade_422.rst b/user_guide_src/source/installation/upgrade_422.rst index 374f7703e97b..b16b52675d4a 100644 --- a/user_guide_src/source/installation/upgrade_422.rst +++ b/user_guide_src/source/installation/upgrade_422.rst @@ -20,7 +20,7 @@ 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. +- The second parameter ``$ifNotExists`` of ``Forge::_createTable()`` is deprecated. It is no longer used and will be removed in a future release. Breaking Enhancements ********************* From 711a4f7b5ba997bf68983a1b8f8f2dfcf12309ae Mon Sep 17 00:00:00 2001 From: sclubricants Date: Wed, 13 Jul 2022 10:14:26 -0700 Subject: [PATCH 5/5] fix _createTable() no longer returns bool --- system/Database/Forge.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/system/Database/Forge.php b/system/Database/Forge.php index 6f65f676a17c..5fd92843e926 100644 --- a/system/Database/Forge.php +++ b/system/Database/Forge.php @@ -506,19 +506,6 @@ public function createTable(string $table, bool $ifNotExists = false, array $att $sql = $this->_createTable($table, false, $attributes); - if (is_bool($sql)) { - $this->reset(); - if ($sql === false) { - if ($this->db->DBDebug) { - throw new DatabaseException('This feature is not available for the database you are using.'); - } - - return false; - } - - return true; - } - if (($result = $this->db->query($sql)) !== false) { if (isset($this->db->dataCache['table_names']) && ! in_array($table, $this->db->dataCache['table_names'], true)) { $this->db->dataCache['table_names'][] = $table; @@ -538,7 +525,7 @@ public function createTable(string $table, bool $ifNotExists = false, array $att } /** - * @return bool|string + * @return string * * @deprecated $ifNotExists is no longer used, and will be removed. */