From 0af4d1fa08677e7b94e263799c7a83cecd6f8440 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 3 Mar 2019 11:23:19 +0530 Subject: [PATCH 1/3] Migration and Database correction --- system/Autoloader/Autoloader.php | 8 +- system/CLI/Console.php | 6 +- system/Database/BaseConnection.php | 22 ++--- system/Database/MigrationRunner.php | 100 ++++++++++----------- system/Database/MySQLi/Utils.php | 2 + system/Database/Postgre/Forge.php | 31 ++++--- system/Database/Postgre/Utils.php | 2 + system/Database/SQLite3/Connection.php | 12 +-- system/Database/SQLite3/Forge.php | 28 +++--- system/Database/SQLite3/Result.php | 5 +- system/Database/SQLite3/Utils.php | 2 + user_guide_src/source/dbmgmt/migration.rst | 4 +- 12 files changed, 115 insertions(+), 107 deletions(-) diff --git a/system/Autoloader/Autoloader.php b/system/Autoloader/Autoloader.php index ac5e632ad2a3..5485bcb4f8ae 100644 --- a/system/Autoloader/Autoloader.php +++ b/system/Autoloader/Autoloader.php @@ -1,7 +1,5 @@ requireFile($filePath); if ($filename) @@ -422,7 +420,7 @@ protected function discoverComposerNamespaces() unset($paths['CodeIgniter\\']); } - // Composer stores paths with trailng slash. We don't. + // Composer stores paths with trailing slash. We don't. $newPaths = []; foreach ($paths as $key => $value) { diff --git a/system/CLI/Console.php b/system/CLI/Console.php index 8b596b0315ed..e60405773bef 100644 --- a/system/CLI/Console.php +++ b/system/CLI/Console.php @@ -68,7 +68,7 @@ public function __construct(CodeIgniter $app) * @param boolean $useSafeOutput * * @return \CodeIgniter\HTTP\RequestInterface|\CodeIgniter\HTTP\Response|\CodeIgniter\HTTP\ResponseInterface|mixed - * @throws \CodeIgniter\HTTP\RedirectException + * @throws \CodeIgniter\Filters\Exceptions\FilterException */ public function run(bool $useSafeOutput = false) { @@ -90,8 +90,8 @@ public function showHeader() CLI::newLine(1); CLI::write(CLI::color('CodeIgniter CLI Tool', 'green') - . ' - Version ' . CodeIgniter::CI_VERSION - . ' - Server-Time: ' . date('Y-m-d H:i:sa')); + . ' - Version ' . CodeIgniter::CI_VERSION + . ' - Server-Time: ' . date('Y-m-d H:i:sa')); CLI::newLine(1); } diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php index 477574d98d1c..af4de145c78d 100644 --- a/system/Database/BaseConnection.php +++ b/system/Database/BaseConnection.php @@ -580,11 +580,11 @@ public function addTableAlias(string $table) /** * Executes the query against the database. * - * @param $sql + * @param string $sql * * @return mixed */ - abstract protected function execute($sql); + abstract protected function execute(string $sql); //-------------------------------------------------------------------- @@ -1262,7 +1262,7 @@ public function escapeIdentifiers($item) } // Avoid breaking functions and literal values inside queries elseif (ctype_digit($item) || $item[0] === "'" || ( $this->escapeChar !== '"' && $item[0] === '"') || - strpos($item, '(') !== false + strpos($item, '(') !== false ) { return $item; @@ -1413,14 +1413,14 @@ public function escapeString($str, $like = false) if ($like === true) { return str_replace([ - $this->likeEscapeChar, - '%', - '_', - ], [ - $this->likeEscapeChar . $this->likeEscapeChar, - $this->likeEscapeChar . '%', - $this->likeEscapeChar . '_', - ], $str + $this->likeEscapeChar, + '%', + '_', + ], [ + $this->likeEscapeChar . $this->likeEscapeChar, + $this->likeEscapeChar . '%', + $this->likeEscapeChar . '_', + ], $str ); } diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index b0de8c69fe66..f0461f08b8e6 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -122,7 +122,7 @@ class MigrationRunner /** * used to return messages for CLI. * - * @var boolean + * @var array */ protected $cliMessages = []; @@ -196,7 +196,7 @@ public function __construct(BaseConfig $config, $db = null) * @param string|null $namespace * @param string|null $group * - * @return mixed TRUE if no migrations are found, current version string on success, FALSE on failure + * @return mixed TRUE if no migrations are found, current version string on success, Exception on failure * @throws ConfigException */ public function version(string $targetVersion, string $namespace = null, string $group = null) @@ -291,7 +291,7 @@ public function version(string $targetVersion, string $namespace = null, string } } - return true; + return $targetVersion; } //-------------------------------------------------------------------- @@ -608,10 +608,10 @@ public function getHistory(string $group = 'default') $this->ensureTable(); $query = $this->db->table($this->table) - ->where('group', $group) - ->where('namespace', $this->namespace) - ->orderBy('version', 'ASC') - ->get(); + ->where('group', $group) + ->where('namespace', $this->namespace) + ->orderBy('version', 'ASC') + ->get(); if (! $query) { @@ -681,11 +681,11 @@ protected function getVersion() $this->ensureTable(); $row = $this->db->table($this->table) - ->select('version') - ->where('group', $this->group) - ->where('namespace', $this->namespace) - ->orderBy('version', 'DESC') - ->get(); + ->select('version') + ->where('group', $this->group) + ->where('namespace', $this->namespace) + ->orderBy('version', 'DESC') + ->get(); return $row && ! is_null($row->getRow()) ? $row->getRow()->version : '0'; } @@ -695,7 +695,7 @@ protected function getVersion() /** * Retrieves current schema version * - * @return string Current migration version + * @return array Return messages for CLI */ public function getCliMessages() { @@ -714,13 +714,13 @@ public function getCliMessages() protected function addHistory(string $version) { $this->db->table($this->table) - ->insert([ - 'version' => $version, - 'name' => $this->name, - 'group' => $this->group, - 'namespace' => $this->namespace, - 'time' => time(), - ]); + ->insert([ + 'version' => $version, + 'name' => $this->name, + 'group' => $this->group, + 'namespace' => $this->namespace, + 'time' => time(), + ]); if (is_cli()) { $this->cliMessages[] = "\t" . CLI::color(lang('Migrations.added'), 'yellow') . "($this->namespace) " . $version . '_' . $this->name; @@ -737,10 +737,10 @@ protected function addHistory(string $version) protected function removeHistory(string $version) { $this->db->table($this->table) - ->where('version', $version) - ->where('group', $this->group) - ->where('namespace', $this->namespace) - ->delete(); + ->where('version', $version) + ->where('group', $this->group) + ->where('namespace', $this->namespace) + ->delete(); if (is_cli()) { $this->cliMessages[] = "\t" . CLI::color(lang('Migrations.removed'), 'yellow') . "($this->namespace) " . $version . '_' . $this->name; @@ -763,32 +763,32 @@ public function ensureTable() $forge = \Config\Database::forge($this->db); $forge->addField([ - 'version' => [ - 'type' => 'VARCHAR', - 'constraint' => 255, - 'null' => false, - ], - 'name' => [ - 'type' => 'VARCHAR', - 'constraint' => 255, - 'null' => false, - ], - 'group' => [ - 'type' => 'VARCHAR', - 'constraint' => 255, - 'null' => false, - ], - 'namespace' => [ - 'type' => 'VARCHAR', - 'constraint' => 255, - 'null' => false, - ], - 'time' => [ - 'type' => 'INT', - 'constraint' => 11, - 'null' => false, - ], - ]); + 'version' => [ + 'type' => 'VARCHAR', + 'constraint' => 255, + 'null' => false, + ], + 'name' => [ + 'type' => 'VARCHAR', + 'constraint' => 255, + 'null' => false, + ], + 'group' => [ + 'type' => 'VARCHAR', + 'constraint' => 255, + 'null' => false, + ], + 'namespace' => [ + 'type' => 'VARCHAR', + 'constraint' => 255, + 'null' => false, + ], + 'time' => [ + 'type' => 'INT', + 'constraint' => 11, + 'null' => false, + ], + ]); $forge->createTable($this->table, true); diff --git a/system/Database/MySQLi/Utils.php b/system/Database/MySQLi/Utils.php index 6876ec8d8f13..e82a40391c76 100644 --- a/system/Database/MySQLi/Utils.php +++ b/system/Database/MySQLi/Utils.php @@ -1,5 +1,7 @@ db->getVersion(), '8', '>=') && isset($data['type'])) { $sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($data['name']) - . " TYPE {$data['type']}{$data['length']}"; + . " TYPE {$data['type']}{$data['length']}"; } if (! empty($data['default'])) { $sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($data['name']) - . " SET DEFAULT {$data['default']}"; + . " SET DEFAULT {$data['default']}"; } if (isset($data['null'])) { $sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($data['name']) - . ($data['null'] === true ? ' DROP' : ' SET') . ' NOT NULL'; + . ($data['null'] === true ? ' DROP' : ' SET') . ' NOT NULL'; } if (! empty($data['new_name'])) { $sqls[] = $sql . ' RENAME COLUMN ' . $this->db->escapeIdentifiers($data['name']) - . ' TO ' . $this->db->escapeIdentifiers($data['new_name']); + . ' TO ' . $this->db->escapeIdentifiers($data['new_name']); } if (! empty($data['comment'])) { $sqls[] = 'COMMENT ON COLUMN' . $this->db->escapeIdentifiers($table) - . '.' . $this->db->escapeIdentifiers($data['name']) - . " IS {$data['comment']}"; + . '.' . $this->db->escapeIdentifiers($data['name']) + . " IS {$data['comment']}"; } } return $sqls; } - //-------------------------------------------------------------------- + //-------------------------------------------------------------------- /** * Process column @@ -159,11 +159,11 @@ protected function _alterTable($alter_type, $table, $field) protected function _processColumn($field) { return $this->db->escapeIdentifiers($field['name']) - . ' ' . $field['type'] . $field['length'] - . $field['default'] - . $field['null'] - . $field['auto_increment'] - . $field['unique']; + . ' ' . $field['type'] . $field['length'] + . $field['default'] + . $field['null'] + . $field['auto_increment'] + . $field['unique']; } //-------------------------------------------------------------------- @@ -190,15 +190,14 @@ protected function _attributeType(&$attributes) case 'TINYINT': $attributes['TYPE'] = 'SMALLINT'; $attributes['UNSIGNED'] = false; - return; + break; case 'MEDIUMINT': $attributes['TYPE'] = 'INTEGER'; $attributes['UNSIGNED'] = false; - return; + break; case 'DATETIME': $attributes['TYPE'] = 'TIMESTAMP'; - default: - return; + break; } } diff --git a/system/Database/Postgre/Utils.php b/system/Database/Postgre/Utils.php index cbe83c5a07c5..2573245a6059 100644 --- a/system/Database/Postgre/Utils.php +++ b/system/Database/Postgre/Utils.php @@ -1,5 +1,7 @@ isWriteType($sql) ? $this->connID->exec($sql) @@ -206,9 +206,9 @@ protected function _escapeString(string $str): string protected function _listTables($prefixLimit = false): string { return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\'' - . (($prefixLimit !== false && $this->DBPrefix !== '') + . (($prefixLimit !== false && $this->DBPrefix !== '') ? ' AND "NAME" LIKE \'' . $this->escapeLikeString($this->DBPrefix) . '%\' ' . sprintf($this->likeEscapeStr, - $this->likeEscapeChar) + $this->likeEscapeChar) : ''); } @@ -231,7 +231,7 @@ protected function _listColumns(string $table = ''): string * * @param string $table Table name * - * @return array|false + * @return array|bool * @throws DatabaseException */ public function getFieldNames($table) @@ -302,7 +302,7 @@ public function getFieldNames($table) public function _fieldData(string $table): array { if (($query = $this->query('PRAGMA TABLE_INFO(' . $this->protectIdentifiers($table, true, null, - false) . ')')) === false) + false) . ')')) === false) { throw new DatabaseException(lang('Database.failGetFieldData')); } @@ -486,6 +486,8 @@ protected function _transRollback(): bool /** * Determines if the statement is a write-type query or not. * + * @param $sql + * * @return boolean */ public function isWriteType($sql): bool diff --git a/system/Database/SQLite3/Forge.php b/system/Database/SQLite3/Forge.php index c37f5c08fad9..dca550bf5b7d 100644 --- a/system/Database/SQLite3/Forge.php +++ b/system/Database/SQLite3/Forge.php @@ -98,7 +98,7 @@ public function createDatabase($db_name): bool * @param string $db_name * * @return boolean - * @throws \CodeIgniter\DatabaseException + * @throws \CodeIgniter\Database\Exceptions\DatabaseException */ public function dropDatabase($db_name): bool { @@ -158,8 +158,8 @@ protected function _alterTable($alter_type, $table, $field) $sqlTable = new Table($this->db, $this); $sqlTable->fromTable($table) - ->dropColumn($field) - ->run(); + ->dropColumn($field) + ->run(); return ''; break; @@ -167,8 +167,8 @@ protected function _alterTable($alter_type, $table, $field) $sqlTable = new Table($this->db, $this); $sqlTable->fromTable($table) - ->modifyColumn($field) - ->run(); + ->modifyColumn($field) + ->run(); return null; break; @@ -195,11 +195,11 @@ protected function _processColumn($field) } return $this->db->escapeIdentifiers($field['name']) - . ' ' . $field['type'] - . $field['auto_increment'] - . $field['null'] - . $field['unique'] - . $field['default']; + . ' ' . $field['type'] + . $field['auto_increment'] + . $field['null'] + . $field['unique'] + . $field['default']; } //-------------------------------------------------------------------- @@ -234,14 +234,14 @@ protected function _processIndexes($table) if (in_array($i, $this->uniqueKeys)) { $sqls[] = 'CREATE UNIQUE INDEX ' . $this->db->escapeIdentifiers($table . '_' . implode('_', $this->keys[$i])) - . ' ON ' . $this->db->escapeIdentifiers($table) - . ' (' . implode(', ', $this->db->escapeIdentifiers($this->keys[$i])) . ');'; + . ' ON ' . $this->db->escapeIdentifiers($table) + . ' (' . implode(', ', $this->db->escapeIdentifiers($this->keys[$i])) . ');'; continue; } $sqls[] = 'CREATE INDEX ' . $this->db->escapeIdentifiers($table . '_' . implode('_', $this->keys[$i])) - . ' ON ' . $this->db->escapeIdentifiers($table) - . ' (' . implode(', ', $this->db->escapeIdentifiers($this->keys[$i])) . ');'; + . ' ON ' . $this->db->escapeIdentifiers($table) + . ' (' . implode(', ', $this->db->escapeIdentifiers($this->keys[$i])) . ');'; } return $sqls; diff --git a/system/Database/SQLite3/Result.php b/system/Database/SQLite3/Result.php index c32dec51a263..b3c42ff660b5 100644 --- a/system/Database/SQLite3/Result.php +++ b/system/Database/SQLite3/Result.php @@ -37,6 +37,7 @@ */ use CodeIgniter\Database\BaseResult; +use CodeIgniter\Database\Exceptions\DatabaseException; use CodeIgniter\Database\ResultInterface; /** @@ -130,7 +131,7 @@ public function freeResult() * @param integer $n * * @return mixed - * @throws \CodeIgniter\DatabaseException + * @throws \CodeIgniter\Database\Exceptions\DatabaseException */ public function dataSeek($n = 0) { @@ -168,7 +169,7 @@ protected function fetchAssoc() * * @param string $className * - * @return object + * @return bool|object */ protected function fetchObject($className = 'stdClass') { diff --git a/system/Database/SQLite3/Utils.php b/system/Database/SQLite3/Utils.php index 5de29fb742c7..267fb7f35f07 100644 --- a/system/Database/SQLite3/Utils.php +++ b/system/Database/SQLite3/Utils.php @@ -1,5 +1,7 @@ setNamespace($path) ->latest(); + .. php:method:: setGroup($group) :param string $group: database group name. @@ -372,3 +373,4 @@ Class Reference $migration->setNamespace($path) ->latest(); + From 6612453139e7c28c739bf50b2f7595fcd0a2c16e Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 3 Mar 2019 12:04:22 +0530 Subject: [PATCH 2/3] Migration and Database correction --- system/Database/MigrationRunner.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index f0461f08b8e6..7ba22e358fa8 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -223,6 +223,10 @@ public function version(string $targetVersion, string $namespace = null, string // Sequential versions need adjusting to 3 places so they can be found later. if ($this->type === 'sequential') { + if ($targetVersion == '0') + { + return true; + } $targetVersion = str_pad($targetVersion, 3, '0', STR_PAD_LEFT); } @@ -256,8 +260,7 @@ public function version(string $targetVersion, string $namespace = null, string foreach ($migrations as $version => $migration) { // Only include migrations within the scoop - if (($method === 'up' && $version > $currentVersion && $version <= $targetVersion) || ( $method === 'down' && $version <= $currentVersion && $version > $targetVersion) - ) + if (($method === 'up' && $version > $currentVersion && $version <= $targetVersion) || ( $method === 'down' && $version <= $currentVersion && $version > $targetVersion)) { include_once $migration->path; // Get namespaced class name From 214dd42985d0b4ab2c0660fc18eca6fed1035e6a Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 3 Mar 2019 12:18:48 +0530 Subject: [PATCH 3/3] Migration and Database correction --- system/Database/MigrationRunner.php | 11 ++++------- user_guide_src/source/dbmgmt/migration.rst | 4 +--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index 7ba22e358fa8..81b05c62b20d 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -196,7 +196,7 @@ public function __construct(BaseConfig $config, $db = null) * @param string|null $namespace * @param string|null $group * - * @return mixed TRUE if no migrations are found, current version string on success, Exception on failure + * @return mixed TRUE if no migrations are found, current version string on success, False on failure * @throws ConfigException */ public function version(string $targetVersion, string $namespace = null, string $group = null) @@ -223,10 +223,6 @@ public function version(string $targetVersion, string $namespace = null, string // Sequential versions need adjusting to 3 places so they can be found later. if ($this->type === 'sequential') { - if ($targetVersion == '0') - { - return true; - } $targetVersion = str_pad($targetVersion, 3, '0', STR_PAD_LEFT); } @@ -260,7 +256,8 @@ public function version(string $targetVersion, string $namespace = null, string foreach ($migrations as $version => $migration) { // Only include migrations within the scoop - if (($method === 'up' && $version > $currentVersion && $version <= $targetVersion) || ( $method === 'down' && $version <= $currentVersion && $version > $targetVersion)) + if (($method === 'up' && $version > $currentVersion && $version <= $targetVersion) || ( $method === 'down' && $version <= $currentVersion && $version > $targetVersion) + ) { include_once $migration->path; // Get namespaced class name @@ -294,7 +291,7 @@ public function version(string $targetVersion, string $namespace = null, string } } - return $targetVersion; + return true; } //-------------------------------------------------------------------- diff --git a/user_guide_src/source/dbmgmt/migration.rst b/user_guide_src/source/dbmgmt/migration.rst index bd660f66ceb7..96346fb369e3 100644 --- a/user_guide_src/source/dbmgmt/migration.rst +++ b/user_guide_src/source/dbmgmt/migration.rst @@ -343,7 +343,7 @@ Class Reference :param mixed $namespace: application namespace, if null (App) namespace will be used. :param mixed $group: database group name, if null default database group will be used. :param mixed $target_version: Migration version to process - :returns: TRUE if no migrations are found, current version string on success, Exception on failure + :returns: TRUE if no migrations are found, current version string on success, FALSE on failure :rtype: mixed Version can be used to roll back changes or step forwards programmatically to @@ -362,7 +362,6 @@ Class Reference $migration->setNamespace($path) ->latest(); - .. php:method:: setGroup($group) :param string $group: database group name. @@ -373,4 +372,3 @@ Class Reference $migration->setNamespace($path) ->latest(); -