First Local Commit - After Clean up.
Signed-off-by: Rick Hays <rhays@haysgang.com>
This commit is contained in:
188
system/Commands/Database/CreateMigration.php
Normal file
188
system/Commands/Database/CreateMigration.php
Normal file
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use Config\Autoload;
|
||||
use Config\Migrations;
|
||||
|
||||
/**
|
||||
* Creates a new migration file.
|
||||
*
|
||||
* @package CodeIgniter\Commands
|
||||
*/
|
||||
class CreateMigration extends BaseCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:create';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Creates a new migration file.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'migrate:create [migration_name] [Options]';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'migration_name' => 'The migration file name',
|
||||
];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'-n' => 'Set migration namespace',
|
||||
];
|
||||
|
||||
/**
|
||||
* Creates a new migration file with the current timestamp.
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function run(array $params = [])
|
||||
{
|
||||
helper('inflector');
|
||||
$name = array_shift($params);
|
||||
|
||||
if (empty($name))
|
||||
{
|
||||
$name = CLI::prompt(lang('Migrations.nameMigration'));
|
||||
}
|
||||
|
||||
if (empty($name))
|
||||
{
|
||||
CLI::error(lang('Migrations.badCreateName'));
|
||||
return;
|
||||
}
|
||||
|
||||
$ns = $params['-n'] ?? CLI::getOption('n');
|
||||
$homepath = APPPATH;
|
||||
|
||||
if (! empty($ns))
|
||||
{
|
||||
// Get all namespaces from PSR4 paths.
|
||||
$config = new Autoload();
|
||||
$namespaces = $config->psr4;
|
||||
|
||||
foreach ($namespaces as $namespace => $path)
|
||||
{
|
||||
if ($namespace === $ns)
|
||||
{
|
||||
$homepath = realpath($path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$ns = 'App';
|
||||
}
|
||||
|
||||
// Always use UTC/GMT so global teams can work together
|
||||
$config = config('Migrations');
|
||||
$fileName = gmdate($config->timestampFormat) . $name;
|
||||
|
||||
// full path
|
||||
$path = $homepath . '/Database/Migrations/' . $fileName . '.php';
|
||||
|
||||
// Class name should be pascal case now (camel case with upper first letter)
|
||||
$name = pascalize($name);
|
||||
|
||||
$template = <<<EOD
|
||||
<?php namespace $ns\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class {name} extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
EOD;
|
||||
$template = str_replace('{name}', $name, $template);
|
||||
|
||||
helper('filesystem');
|
||||
if (! write_file($path, $template))
|
||||
{
|
||||
CLI::error(lang('Migrations.writeError'));
|
||||
return;
|
||||
}
|
||||
|
||||
CLI::write('Created file: ' . CLI::color(str_replace($homepath, $ns, $path), 'green'));
|
||||
}
|
||||
|
||||
}
|
||||
167
system/Commands/Database/Migrate.php
Normal file
167
system/Commands/Database/Migrate.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use Config\Services;
|
||||
|
||||
/**
|
||||
* Runs all new migrations.
|
||||
*
|
||||
* @package CodeIgniter\Commands
|
||||
*/
|
||||
class Migrate extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Locates and runs all new migrations against the database.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'migrate [options]';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'-n' => 'Set migration namespace',
|
||||
'-g' => 'Set database group',
|
||||
'-all' => 'Set for all namespaces, will ignore (-n) option',
|
||||
];
|
||||
|
||||
/**
|
||||
* Ensures that all migrations have been run.
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function run(array $params = [])
|
||||
{
|
||||
$runner = Services::migrations();
|
||||
$runner->clearCliMessages();
|
||||
|
||||
CLI::write(lang('Migrations.latest'), 'yellow');
|
||||
|
||||
$namespace = $params['-n'] ?? CLI::getOption('n');
|
||||
$group = $params['-g'] ?? CLI::getOption('g');
|
||||
|
||||
try
|
||||
{
|
||||
// Check for 'all' namespaces
|
||||
if ($this->isAllNamespace($params))
|
||||
{
|
||||
$runner->setNamespace(null);
|
||||
}
|
||||
// Check for a specified namespace
|
||||
elseif ($namespace)
|
||||
{
|
||||
$runner->setNamespace($namespace);
|
||||
}
|
||||
|
||||
if (! $runner->latest($group))
|
||||
{
|
||||
CLI::write(lang('Migrations.generalFault'), 'red');
|
||||
}
|
||||
|
||||
$messages = $runner->getCliMessages();
|
||||
foreach ($messages as $message)
|
||||
{
|
||||
CLI::write($message);
|
||||
}
|
||||
|
||||
CLI::write('Done');
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->showError($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* To migrate all namespaces to the latest migration
|
||||
*
|
||||
* Demo:
|
||||
* 1. command line: php spark migrate:latest -all
|
||||
* 2. command file: $this->call('migrate:latest', ['-g' => 'test','-all']);
|
||||
*
|
||||
* @param array $params
|
||||
* @return boolean
|
||||
*/
|
||||
private function isAllNamespace(array $params): bool
|
||||
{
|
||||
if (array_search('-all', $params) !== false)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return ! is_null(CLI::getOption('all'));
|
||||
}
|
||||
|
||||
}
|
||||
112
system/Commands/Database/MigrateRefresh.php
Normal file
112
system/Commands/Database/MigrateRefresh.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
|
||||
/**
|
||||
* Does a rollback followed by a latest to refresh the current state
|
||||
* of the database.
|
||||
*
|
||||
* @package CodeIgniter\Commands
|
||||
*/
|
||||
class MigrateRefresh extends BaseCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:refresh';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Does a rollback followed by a latest to refresh the current state of the database.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'migrate:refresh [Options]';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'-n' => 'Set migration namespace',
|
||||
'-g' => 'Set database group',
|
||||
'-all' => 'Set latest for all namespace, will ignore (-n) option',
|
||||
];
|
||||
|
||||
/**
|
||||
* Does a rollback followed by a latest to refresh the current state
|
||||
* of the database.
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function run(array $params = [])
|
||||
{
|
||||
$this->call('migrate:rollback', ['-b' => 0]);
|
||||
$this->call('migrate');
|
||||
}
|
||||
|
||||
}
|
||||
142
system/Commands/Database/MigrateRollback.php
Normal file
142
system/Commands/Database/MigrateRollback.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use Config\Services;
|
||||
use Config\Autoload;
|
||||
|
||||
/**
|
||||
* Runs all of the migrations in reverse order, until they have
|
||||
* all been un-applied.
|
||||
*
|
||||
* @package CodeIgniter\Commands
|
||||
*/
|
||||
class MigrateRollback extends BaseCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:rollback';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Runs the "down" method for all migrations in the last batch.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'migrate:rollback [Options]';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'-b' => 'Specify a batch to roll back to; e.g. "3" to return to batch #3 or "-2" to roll back twice',
|
||||
'-g' => 'Set database group',
|
||||
];
|
||||
|
||||
/**
|
||||
* Runs all of the migrations in reverse order, until they have
|
||||
* all been un-applied.
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function run(array $params = [])
|
||||
{
|
||||
$runner = Services::migrations();
|
||||
|
||||
$group = $params['-g'] ?? CLI::getOption('g');
|
||||
|
||||
if (! is_null($group))
|
||||
{
|
||||
$runner->setGroup($group);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$batch = $params['-b'] ?? CLI::getOption('b') ?? $runner->getLastBatch() - 1;
|
||||
CLI::write(lang('Migrations.rollingBack') . ' ' . $batch, 'yellow');
|
||||
|
||||
if (! $runner->regress($batch))
|
||||
{
|
||||
CLI::write(lang('Migrations.generalFault'), 'red');
|
||||
}
|
||||
|
||||
$messages = $runner->getCliMessages();
|
||||
foreach ($messages as $message)
|
||||
{
|
||||
CLI::write($message);
|
||||
}
|
||||
|
||||
CLI::write('Done');
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->showError($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
181
system/Commands/Database/MigrateStatus.php
Normal file
181
system/Commands/Database/MigrateStatus.php
Normal file
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use Config\Services;
|
||||
use Config\Autoload;
|
||||
|
||||
/**
|
||||
* Displays a list of all migrations and whether they've been run or not.
|
||||
*
|
||||
* @package CodeIgniter\Commands
|
||||
*/
|
||||
class MigrateStatus extends BaseCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:status';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Displays a list of all migrations and whether they\'ve been run or not.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'migrate:status [Options]';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'-g' => 'Set database group',
|
||||
];
|
||||
|
||||
/**
|
||||
* Namespaces to ignore when looking for migrations.
|
||||
*
|
||||
* @var type
|
||||
*/
|
||||
protected $ignoredNamespaces = [
|
||||
'CodeIgniter',
|
||||
'Config',
|
||||
'Tests\Support',
|
||||
];
|
||||
|
||||
/**
|
||||
* Displays a list of all migrations and whether they've been run or not.
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function run(array $params = [])
|
||||
{
|
||||
$runner = Services::migrations();
|
||||
|
||||
$group = $params['-g'] ?? CLI::getOption('g');
|
||||
|
||||
if (! is_null($group))
|
||||
{
|
||||
$runner->setGroup($group);
|
||||
}
|
||||
|
||||
// Get all namespaces from PSR4 paths.
|
||||
$config = new Autoload();
|
||||
$namespaces = $config->psr4;
|
||||
|
||||
// Loop for all $namespaces
|
||||
foreach ($namespaces as $namespace => $path)
|
||||
{
|
||||
if (in_array($namespace, $this->ignoredNamespaces))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$runner->setNamespace($namespace);
|
||||
$migrations = $runner->findMigrations();
|
||||
$history = $runner->getHistory();
|
||||
|
||||
CLI::write($namespace);
|
||||
|
||||
if (empty($migrations))
|
||||
{
|
||||
CLI::error(lang('Migrations.noneFound'));
|
||||
continue;
|
||||
}
|
||||
|
||||
ksort($migrations);
|
||||
|
||||
$max = 0;
|
||||
foreach ($migrations as $version => $migration)
|
||||
{
|
||||
$file = substr($migration->name, strpos($migration->name, $version . '_'));
|
||||
$migrations[$version]->name = $file;
|
||||
|
||||
$max = max($max, strlen($file));
|
||||
}
|
||||
|
||||
CLI::write(' ' . str_pad(lang('Migrations.filename'), $max + 4) . lang('Migrations.on'), 'yellow');
|
||||
|
||||
foreach ($migrations as $uid => $migration)
|
||||
{
|
||||
$date = '';
|
||||
foreach ($history as $row)
|
||||
{
|
||||
if ($runner->getObjectUid($row) !== $uid)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$date = date('Y-m-d H:i:s', $row->time);
|
||||
}
|
||||
CLI::write(str_pad(' ' . $migration->name, $max + 6) . ($date ? $date : '---'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
133
system/Commands/Database/Seed.php
Normal file
133
system/Commands/Database/Seed.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\Database\Seeder;
|
||||
|
||||
/**
|
||||
* Runs the specified Seeder file to populate the database
|
||||
* with some data.
|
||||
*
|
||||
* @package CodeIgniter\Commands
|
||||
*/
|
||||
class Seed extends BaseCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'db:seed';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Runs the specified seeder to populate known data into the database.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'db:seed [seeder_name]';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'seeder_name' => 'The seeder name to run',
|
||||
];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* Runs all of the migrations in reverse order, until they have
|
||||
* all been un-applied.
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function run(array $params = [])
|
||||
{
|
||||
$seeder = new Seeder(new \Config\Database());
|
||||
|
||||
$seedName = array_shift($params);
|
||||
|
||||
if (empty($seedName))
|
||||
{
|
||||
$seedName = CLI::prompt(lang('Migrations.migSeeder'), 'DatabaseSeeder');
|
||||
}
|
||||
|
||||
if (empty($seedName))
|
||||
{
|
||||
CLI::error(lang('Migrations.migMissingSeeder'));
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$seeder->call($seedName);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->showError($e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user