mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
New Command to generate Database sessions table migration file.
This commit is contained in:
parent
1d5ab0b427
commit
094f6756a8
@ -95,7 +95,7 @@ class CommandRunner extends Controller
|
||||
{
|
||||
$className = service('locator')->findQualifiedNameFromPath($file);
|
||||
|
||||
if (empty($className))
|
||||
if (empty($className) || ! class_exists($className))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
54
system/Commands/Sessions/CreateMigration.php
Normal file
54
system/Commands/Sessions/CreateMigration.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php namespace CodeIgniter\Commands\Sessions;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use Config\App;
|
||||
|
||||
class CreateMigration extends BaseCommand
|
||||
{
|
||||
protected $group = 'CodeIgniter';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'session:migration';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates the migration file for database sessions.';
|
||||
|
||||
/**
|
||||
* Creates a new migration file with the current timestamp.
|
||||
*/
|
||||
public function run(array $params=[])
|
||||
{
|
||||
$name = 'create_sessions_table';
|
||||
|
||||
$path = APPPATH.'Database/Migrations/'.date('YmdHis_').$name.'.php';
|
||||
|
||||
$config = new App();
|
||||
|
||||
$data = [
|
||||
'matchIP' => $config->sessionMatchIP ?? false,
|
||||
'tableName' => $config->sessionSavePath ?? 'ci_sessions',
|
||||
];
|
||||
|
||||
$template = view('\CodeIgniter\Commands\Sessions\Views\migration.tpl', $data);
|
||||
$template = str_replace('@php', '<?php', $template);
|
||||
|
||||
// Write the file out.
|
||||
helper('filesystem');
|
||||
if (! write_file($path, $template))
|
||||
{
|
||||
CLI::error(lang('Migrations.migWriteError'));
|
||||
return;
|
||||
}
|
||||
|
||||
CLI::write('Created file: '. CLI::color(str_replace(APPPATH, 'APPPATH/', $path), 'green'));
|
||||
}
|
||||
}
|
31
system/Commands/Sessions/Views/migration.tpl.php
Normal file
31
system/Commands/Sessions/Views/migration.tpl.php
Normal file
@ -0,0 +1,31 @@
|
||||
@php
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class Migration_create_sessions_table extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
$this->forge->addField([
|
||||
'id' => ['type' => 'INT', 'constraint' => 9, 'unsigned' => true, 'auto_increment' => true],
|
||||
'ip_address' => ['type' => 'VARCHAR', 'constraint' => 45, 'null' => false],
|
||||
'timestamp' => ['type' => 'INT', 'constraint' => 10, 'unsigned' => true, 'null' => false, 'default' => 0],
|
||||
'data' => ['type' => 'text', 'null' => false, 'default' => ''],
|
||||
]);
|
||||
|
||||
<?php if ($matchIP === true) : ?>
|
||||
$this->forge->addKey(['id', 'ip_address'], true);
|
||||
<?php else: ?>
|
||||
$this->forge->addKey('id', true);
|
||||
<?php endif ?>
|
||||
$this->forge->addKey('timestamp');
|
||||
$this->forge->createTable('<?= $tableName ?>', true);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function down()
|
||||
{
|
||||
$this->forge->dropTable('<?= $tableName ?>', true);
|
||||
}
|
||||
}
|
@ -609,6 +609,15 @@ You can choose the Database group to use by adding a new line to the
|
||||
|
||||
public $sessionDBGroup = 'groupName';
|
||||
|
||||
If you'd rather not do all of this by hand, you can use the ``session:migration`` command
|
||||
from the cli to generate a migration file for you::
|
||||
|
||||
> php ci.php session:migration
|
||||
> php ci.php migrate
|
||||
|
||||
This command will take the **sessionSavePath** and **sessionMatchIP** settings into account
|
||||
when it generates the code.
|
||||
|
||||
.. important:: Only MySQL and PostgreSQL databases are officially
|
||||
supported, due to lack of advisory locking mechanisms on other
|
||||
platforms. Using sessions without locks can cause all sorts of
|
||||
|
Loading…
x
Reference in New Issue
Block a user