Skip to content

Commit

Permalink
Add DB migration view and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
DidierViret committed Dec 17, 2021
1 parent c9da1b7 commit 1dae73b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 4 deletions.
85 changes: 85 additions & 0 deletions orif/stock/Controllers/Migrate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Controller for database migration.
* This controller will try to delete itself after migration is done. If this does not work it is strongly recommended to manually delete it.
*
* @author Orif (ViDi,AeDa)
* @link https://github.com/OrifInformatique
* @copyright Copyright (c), Orif (https://www.orif.ch)
*/
namespace Stock\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Exception;
use Psr\Log\LoggerInterface;


class Migrate extends BaseController
{
/**
* Constructor
*/
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
// Set Access level before calling parent constructor

parent::initController($request,$response,$logger);

// Load required services
$this->validation = \Config\Services::validation();
$this->migrate = \Config\Services::migrations();

// Load required helpers
helper('form');

//get db instance
$this->db = \CodeIgniter\Database\Config::connect();
}

public function index()
{
// Display migration form
$this->display_view('\Stock\Views\migration\migration_form');
}

public function toLatest()
{
if (!empty($_POST))
{
// Check the given password before doing the migration
$validationRules = [
'password' => 'required|min_length[10]'
];

if ($this->validate($validationRules))
{
if ($_POST['password'] == 'uzdSb8U8ZUD5h24')
{
try
{
// Migrate to latest
$this->migrate->setNamespace('Stock')->latest();

// Delete migration files
unlink(ROOTPATH.'orif/stock/Views/migration/migration_form.php');
rmdir(ROOTPATH.'orif/stock/Views/migration');
unlink(ROOTPATH.'orif/stock/Controllers/Migrate.php');

// Go back to homepage
return redirect()->to(base_url());
}
catch(Exception $e)
{
// Display migration form with error message
session()->setFlashdata('migration-error', lang('migrate_lang.err_msg_migration_failed') . $e->getMessage());
return $this->display_view('\Stock\Views\migration\migration_form');
}
}
}
}

// Display migration form
return $this->display_view('\Stock\Views\migration\migration_form');
}
}
8 changes: 4 additions & 4 deletions orif/stock/Language/fr/migrate_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

return[
// Page titles
'title_migration' => 'Migration',
'title_migration' => 'Migration de la BD',

// Buttons
'btn_migrate' => 'Migrer vers CI4',
'btn_migrate' => 'Migrer la BD',

// Error messages
'err_msg_migration_failed' => 'L\'erreur suivante s\'est produite lors de la migration:<br><br>',

// Other texts
'warning' => 'La migration va modifier la base de données, afin de la rendre compatible avec ci_session, le soft delete et le module user :<br> <br>'
.'Ceci pouvant être dangereux pour l\'intégrité des données, elle est bloquée derrière un mot de passe.',
'warning' => 'La migration va modifier la base de données, afin de l\'adapter à la nouvelle version de l\'application.<br> <br>'
.'Ceci pouvant être dangereux pour l\'intégrité des données, il est vivement recommandé de faire une sauvegarde complète de la base de données avant de lancer cette opération.',

];
43 changes: 43 additions & 0 deletions orif/stock/Views/migration/migration_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
$session = \Config\Services::session();
$validation = \Config\Services::validation();
?>

<div class="container">
<?php echo form_open('stock/migrate/toLatest', 'password'); ?>
<!-- TITLE -->
<div class="row">
<div class="col">
<h1 class="title-section"><?= lang('migrate_lang.title_migration') ?></h1>
</div>
</div>

<!-- ERROR MESSAGES -->
<?php if ( ! is_null($session->getFlashdata('migration-error'))) : ?>
<div class="alert alert-danger text-justify" role="alert">
<?= $session->getFlashdata('migration-error'); ?>
</div>
<?php endif ?>

<!-- INFORMATION MESSAGE -->
<div class="col-12 alert alert-info">
<?= lang("migrate_lang.warning"); ?>
</div>

<div class="col form-group pl-0 pr-0">
<?= form_label(lang('user_lang.field_password'), 'password', ['class' => 'form-label']); ?>
<?= form_password('password', '', [
'class' => 'form-control', 'id' => 'password'
]); ?>
<span class="text-danger"><?= $validation->showError('password'); ?></span>
</div>

<!-- FORM BUTTONS -->
<div class="row">
<div class="col text-right">
<a class="btn btn-default" href="<?= base_url() ?>"><?= lang('common_lang.btn_cancel'); ?></a>
<?= form_submit('send', lang('migrate_lang.btn_migrate'), ['class' => 'btn btn-danger']); ?>
</div>
</div>
<?php form_close(); ?>
</div>

0 comments on commit 1dae73b

Please sign in to comment.