From bcc9b88623d253553c5c50f16204b658050d5bb2 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 6 Nov 2021 15:17:03 +0100 Subject: [PATCH] Added support for upgrading and downgrading manually the database via the CLI --- application/controllers/Console.php | 2 ++ application/core/EA_Migration.php | 10 +++++++++- application/libraries/Instance.php | 24 +++++++++++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/application/controllers/Console.php b/application/controllers/Console.php index 39352036..b8b95844 100644 --- a/application/controllers/Console.php +++ b/application/controllers/Console.php @@ -167,6 +167,8 @@ class Console extends EA_Controller { '', '⇾ php index.php console migrate', '⇾ php index.php console migrate fresh', + '⇾ php index.php console migrate up', + '⇾ php index.php console migrate down', '⇾ php index.php console seed', '⇾ php index.php console install', '⇾ php index.php console backup', diff --git a/application/core/EA_Migration.php b/application/core/EA_Migration.php index b4cd7f0c..4d87956a 100644 --- a/application/core/EA_Migration.php +++ b/application/core/EA_Migration.php @@ -42,5 +42,13 @@ require_once __DIR__ . '/../../system/libraries/Migration.php'; * @property EA_URI $uri */ class EA_Migration extends CI_Migration { - // + /** + * Get the current migration version. + * + * @return int + */ + public function current_version(): int + { + return $this->_get_version(); + } } diff --git a/application/libraries/Instance.php b/application/libraries/Instance.php index 1eb090f0..21e9aa4f 100644 --- a/application/libraries/Instance.php +++ b/application/libraries/Instance.php @@ -45,10 +45,32 @@ class Instance { /** * Migrate the database to the latest state. * - * @param string $type Provide "fresh" to revert previous migrations and start from the beginning. + * @param string $type Provide "fresh" to revert previous migrations and start from the beginning or "up"/"down" to step. */ public function migrate(string $type = '') { + $current_version = $this->CI->migration->current_version(); + + if ($type === 'up') + { + if ( ! $this->CI->migration->version($current_version + 1)) + { + show_error($this->CI->migration->error_string()); + } + + return; + } + + if ($type === 'down') + { + if ( ! $this->CI->migration->version($current_version - 1)) + { + show_error($this->CI->migration->error_string()); + } + + return; + } + if ($type === 'fresh' && ! $this->CI->migration->version(0)) { show_error($this->CI->migration->error_string());