add migrations table for automatic table data migration

This commit is contained in:
Dario Rekowski on RockPI 2021-05-10 14:14:48 +00:00
parent b29c391626
commit 40a9a8c2b5
11 changed files with 511 additions and 0 deletions

View File

@ -0,0 +1,5 @@
CREATE TABLE `migrations` (
`id` int UNSIGNED NOT NULL AUTO_INCREMENT,
`db_version` int DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@ -0,0 +1,106 @@
<?php
namespace App\Controller;
use App\Controller\AppController;
/**
* Migrations Controller
*
* @property \App\Model\Table\MigrationsTable $Migrations
*
* @method \App\Model\Entity\Migration[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class MigrationsController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|null
*/
public function index()
{
$migrations = $this->paginate($this->Migrations);
$this->set(compact('migrations'));
}
/**
* View method
*
* @param string|null $id Migration id.
* @return \Cake\Http\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$migration = $this->Migrations->get($id, [
'contain' => [],
]);
$this->set('migration', $migration);
}
/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
$migration = $this->Migrations->newEntity();
if ($this->request->is('post')) {
$migration = $this->Migrations->patchEntity($migration, $this->request->getData());
if ($this->Migrations->save($migration)) {
$this->Flash->success(__('The migration has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The migration could not be saved. Please, try again.'));
}
$this->set(compact('migration'));
}
/**
* Edit method
*
* @param string|null $id Migration id.
* @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$migration = $this->Migrations->get($id, [
'contain' => [],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$migration = $this->Migrations->patchEntity($migration, $this->request->getData());
if ($this->Migrations->save($migration)) {
$this->Flash->success(__('The migration has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The migration could not be saved. Please, try again.'));
}
$this->set(compact('migration'));
}
/**
* Delete method
*
* @param string|null $id Migration id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$migration = $this->Migrations->get($id);
if ($this->Migrations->delete($migration)) {
$this->Flash->success(__('The migration has been deleted.'));
} else {
$this->Flash->error(__('The migration could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* Migration Entity
*
* @property int $id
* @property int|null $db_version
*/
class Migration extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'db_version' => true,
];
}

View File

@ -0,0 +1,56 @@
<?php
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Migrations Model
*
* @method \App\Model\Entity\Migration get($primaryKey, $options = [])
* @method \App\Model\Entity\Migration newEntity($data = null, array $options = [])
* @method \App\Model\Entity\Migration[] newEntities(array $data, array $options = [])
* @method \App\Model\Entity\Migration|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\Migration saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\Migration patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\Migration[] patchEntities($entities, array $data, array $options = [])
* @method \App\Model\Entity\Migration findOrCreate($search, callable $callback = null, $options = [])
*/
class MigrationsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('migrations');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->nonNegativeInteger('id')
->allowEmptyString('id', null, 'create');
$validator
->integer('db_version')
->allowEmptyString('db_version');
return $validator;
}
}

View File

@ -0,0 +1,23 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Migration $migration
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Migrations'), ['action' => 'index']) ?></li>
</ul>
</nav>
<div class="migrations form large-9 medium-8 columns content">
<?= $this->Form->create($migration) ?>
<fieldset>
<legend><?= __('Add Migration') ?></legend>
<?php
echo $this->Form->control('db_version');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

View File

@ -0,0 +1,29 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Migration $migration
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Form->postLink(
__('Delete'),
['action' => 'delete', $migration->id],
['confirm' => __('Are you sure you want to delete # {0}?', $migration->id)]
)
?></li>
<li><?= $this->Html->link(__('List Migrations'), ['action' => 'index']) ?></li>
</ul>
</nav>
<div class="migrations form large-9 medium-8 columns content">
<?= $this->Form->create($migration) ?>
<fieldset>
<legend><?= __('Edit Migration') ?></legend>
<?php
echo $this->Form->control('db_version');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

View File

@ -0,0 +1,47 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Migration[]|\Cake\Collection\CollectionInterface $migrations
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('New Migration'), ['action' => 'add']) ?></li>
</ul>
</nav>
<div class="migrations index large-9 medium-8 columns content">
<h3><?= __('Migrations') ?></h3>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('db_version') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($migrations as $migration): ?>
<tr>
<td><?= $this->Number->format($migration->id) ?></td>
<td><?= $this->Number->format($migration->db_version) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $migration->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $migration->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $migration->id], ['confirm' => __('Are you sure you want to delete # {0}?', $migration->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>
</div>

View File

@ -0,0 +1,28 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Migration $migration
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('Edit Migration'), ['action' => 'edit', $migration->id]) ?> </li>
<li><?= $this->Form->postLink(__('Delete Migration'), ['action' => 'delete', $migration->id], ['confirm' => __('Are you sure you want to delete # {0}?', $migration->id)]) ?> </li>
<li><?= $this->Html->link(__('List Migrations'), ['action' => 'index']) ?> </li>
<li><?= $this->Html->link(__('New Migration'), ['action' => 'add']) ?> </li>
</ul>
</nav>
<div class="migrations view large-9 medium-8 columns content">
<h3><?= h($migration->id) ?></h3>
<table class="vertical-table">
<tr>
<th scope="row"><?= __('Id') ?></th>
<td><?= $this->Number->format($migration->id) ?></td>
</tr>
<tr>
<th scope="row"><?= __('Db Version') ?></th>
<td><?= $this->Number->format($migration->db_version) ?></td>
</tr>
</table>
</div>

View File

@ -0,0 +1,44 @@
<?php
namespace App\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
/**
* MigrationsFixture
*/
class MigrationsFixture extends TestFixture
{
/**
* Fields
*
* @var array
*/
// @codingStandardsIgnoreStart
public $fields = [
'id' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null],
'db_version' => ['type' => 'integer', 'length' => 11, 'unsigned' => false, 'null' => true, 'default' => '0', 'comment' => '', 'precision' => null, 'autoIncrement' => null],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []],
],
'_options' => [
'engine' => 'InnoDB',
'collation' => 'utf8mb4_unicode_ci'
],
];
// @codingStandardsIgnoreEnd
/**
* Init method
*
* @return void
*/
public function init()
{
$this->records = [
[
'id' => 1,
'db_version' => 1,
],
];
parent::init();
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace App\Test\TestCase\Controller;
use App\Controller\MigrationsController;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* App\Controller\MigrationsController Test Case
*
* @uses \App\Controller\MigrationsController
*/
class MigrationsControllerTest extends TestCase
{
use IntegrationTestTrait;
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'app.Migrations',
];
/**
* Test index method
*
* @return void
*/
public function testIndex()
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test view method
*
* @return void
*/
public function testView()
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test add method
*
* @return void
*/
public function testAdd()
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test edit method
*
* @return void
*/
public function testEdit()
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test delete method
*
* @return void
*/
public function testDelete()
{
$this->markTestIncomplete('Not implemented yet.');
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace App\Test\TestCase\Model\Table;
use App\Model\Table\MigrationsTable;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
/**
* App\Model\Table\MigrationsTable Test Case
*/
class MigrationsTableTest extends TestCase
{
/**
* Test subject
*
* @var \App\Model\Table\MigrationsTable
*/
public $Migrations;
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'app.Migrations',
];
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$config = TableRegistry::getTableLocator()->exists('Migrations') ? [] : ['className' => MigrationsTable::class];
$this->Migrations = TableRegistry::getTableLocator()->get('Migrations', $config);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown()
{
unset($this->Migrations);
parent::tearDown();
}
/**
* Test initialize method
*
* @return void
*/
public function testInitialize()
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test validationDefault method
*
* @return void
*/
public function testValidationDefault()
{
$this->markTestIncomplete('Not implemented yet.');
}
}