chg: [app] Added timestamp behavior for multiple models

pull/72/head
mokaddem 2021-09-28 13:32:51 +02:00
parent 8b5d2bc48a
commit 348792c815
12 changed files with 148 additions and 2 deletions

View File

@ -0,0 +1,124 @@
<?php
declare(strict_types=1);
use Migrations\AbstractMigration;
class TimestampBehavior extends AbstractMigration
{
public function change()
{
$alignments = $this->table('alignments')
->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
])
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
$broods = $this->table('broods')
->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
])
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
$encryption_keys = $this->table('encryption_keys')
->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
])
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
$inbox = $this->table('inbox')
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
$outbox = $this->table('outbox')
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
$individuals = $this->table('individuals')
->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
])
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
$local_tools = $this->table('local_tools')
->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
])
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
$meta_templates = $this->table('meta_templates')
->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
])
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
$organisations = $this->table('organisations')
->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
])
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
$sharing_groups = $this->table('sharing_groups')
->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
])
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
$users = $this->table('users')
->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
])
->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
])
->update();
}
}

View File

@ -3,6 +3,7 @@
namespace App\Controller;
use App\Controller\AppController;
use Cake\Utility\Inflector;
use Cake\Utility\Hash;
use Cake\Utility\Text;
use \Cake\Database\Expression\QueryExpression;
@ -58,6 +59,16 @@ class InstanceController extends AppController
$this->loadModel('Phinxlog');
$status = $this->Phinxlog->mergeMigrationLogIntoStatus($migrationStatus['status']);
foreach ($status as $i => $entry) {
if (!empty($entry['plugin'])) {
$pluginTablename = sprintf('%s_phinxlog', Inflector::underscore($entry['plugin']));
$status[$i] = $this->Phinxlog->mergeMigrationLogIntoStatus([$entry], $pluginTablename)[0];
}
}
usort($status, function($a, $b) {
return strcmp($b['id'], $a['id']);
});
$this->set('status', $status);
$this->set('updateAvailables', $migrationStatus['updateAvailables']);
}

View File

@ -13,6 +13,7 @@ class AlignmentsTable extends AppTable
parent::initialize($config);
$this->belongsTo('Individuals');
$this->belongsTo('Organisations');
$this->addBehavior('Timestamp');
}
public function validationDefault(Validator $validator): Validator

View File

@ -18,6 +18,7 @@ class BroodsTable extends AppTable
{
parent::initialize($config);
$this->addBehavior('UUID');
$this->addBehavior('Timestamp');
$this->BelongsTo(
'Organisations'
);

View File

@ -14,6 +14,7 @@ class EncryptionKeysTable extends AppTable
{
parent::initialize($config);
$this->addBehavior('UUID');
$this->addBehavior('Timestamp');
$this->belongsTo(
'Individuals',
[

View File

@ -14,6 +14,7 @@ class IndividualsTable extends AppTable
{
parent::initialize($config);
$this->addBehavior('UUID');
$this->addBehavior('Timestamp');
$this->addBehavior('Tags.Tag');
$this->hasMany(
'Alignments',

View File

@ -27,7 +27,7 @@ class InstanceTable extends AppTable
public function getStatistics($days=30): array
{
$models = ['Individuals', 'Organisations', 'Alignments', 'EncryptionKeys', 'SharingGroups', 'Users', 'Tags.Tags'];
$models = ['Individuals', 'Organisations', 'Alignments', 'EncryptionKeys', 'SharingGroups', 'Users', 'Broods', 'Tags.Tags'];
foreach ($models as $model) {
$table = TableRegistry::getTableLocator()->get($model);
$statistics[$model]['amount'] = $table->find()->all()->count();

View File

@ -30,6 +30,7 @@ class LocalToolsTable extends AppTable
public function initialize(array $config): void
{
parent::initialize($config);
$this->addBehavior('Timestamp');
}
public function validationDefault(Validator $validator): Validator

View File

@ -13,6 +13,7 @@ class MetaTemplatesTable extends AppTable
public function initialize(array $config): void
{
parent::initialize($config);
$this->addBehavior('Timestamp');
$this->hasMany(
'MetaTemplateFields',
[

View File

@ -18,6 +18,7 @@ class OrganisationsTable extends AppTable
public function initialize(array $config): void
{
parent::initialize($config);
$this->addBehavior('Timestamp');
$this->addBehavior('Tags.Tag');
$this->hasMany(
'Alignments',

View File

@ -12,8 +12,11 @@ class PhinxlogTable extends AppTable
parent::initialize($config);
}
public function mergeMigrationLogIntoStatus(array $status): array
public function mergeMigrationLogIntoStatus(array $status, $table=null): array
{
if (!is_null($table)) {
$this->setTable($table);
}
$logs = $this->find('list', [
'keyField' => 'version',
'valueField' => function ($entry) {

View File

@ -14,6 +14,7 @@ class SharingGroupsTable extends AppTable
{
parent::initialize($config);
$this->addBehavior('UUID');
$this->addBehavior('Timestamp');
$this->belongsTo(
'Users'
);