From 348792c8159654e6811d6a0772e55ee12d84f43a Mon Sep 17 00:00:00 2001 From: mokaddem Date: Tue, 28 Sep 2021 13:32:51 +0200 Subject: [PATCH] chg: [app] Added timestamp behavior for multiple models --- .../20210928120006_TimestampBehavior.php | 124 ++++++++++++++++++ src/Controller/InstanceController.php | 11 ++ src/Model/Table/AlignmentsTable.php | 1 + src/Model/Table/BroodsTable.php | 1 + src/Model/Table/EncryptionKeysTable.php | 1 + src/Model/Table/IndividualsTable.php | 1 + src/Model/Table/InstanceTable.php | 2 +- src/Model/Table/LocalToolsTable.php | 1 + src/Model/Table/MetaTemplatesTable.php | 1 + src/Model/Table/OrganisationsTable.php | 1 + src/Model/Table/PhinxlogTable.php | 5 +- src/Model/Table/SharingGroupsTable.php | 1 + 12 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 config/Migrations/20210928120006_TimestampBehavior.php diff --git a/config/Migrations/20210928120006_TimestampBehavior.php b/config/Migrations/20210928120006_TimestampBehavior.php new file mode 100644 index 0000000..8908276 --- /dev/null +++ b/config/Migrations/20210928120006_TimestampBehavior.php @@ -0,0 +1,124 @@ +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(); + } +} \ No newline at end of file diff --git a/src/Controller/InstanceController.php b/src/Controller/InstanceController.php index c18a2a8..8b88e47 100644 --- a/src/Controller/InstanceController.php +++ b/src/Controller/InstanceController.php @@ -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']); } diff --git a/src/Model/Table/AlignmentsTable.php b/src/Model/Table/AlignmentsTable.php index ad7e10f..0914e99 100644 --- a/src/Model/Table/AlignmentsTable.php +++ b/src/Model/Table/AlignmentsTable.php @@ -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 diff --git a/src/Model/Table/BroodsTable.php b/src/Model/Table/BroodsTable.php index ce783f4..e1993b4 100644 --- a/src/Model/Table/BroodsTable.php +++ b/src/Model/Table/BroodsTable.php @@ -18,6 +18,7 @@ class BroodsTable extends AppTable { parent::initialize($config); $this->addBehavior('UUID'); + $this->addBehavior('Timestamp'); $this->BelongsTo( 'Organisations' ); diff --git a/src/Model/Table/EncryptionKeysTable.php b/src/Model/Table/EncryptionKeysTable.php index dc0678b..23b4867 100644 --- a/src/Model/Table/EncryptionKeysTable.php +++ b/src/Model/Table/EncryptionKeysTable.php @@ -14,6 +14,7 @@ class EncryptionKeysTable extends AppTable { parent::initialize($config); $this->addBehavior('UUID'); + $this->addBehavior('Timestamp'); $this->belongsTo( 'Individuals', [ diff --git a/src/Model/Table/IndividualsTable.php b/src/Model/Table/IndividualsTable.php index f8c41f1..f0ba07f 100644 --- a/src/Model/Table/IndividualsTable.php +++ b/src/Model/Table/IndividualsTable.php @@ -14,6 +14,7 @@ class IndividualsTable extends AppTable { parent::initialize($config); $this->addBehavior('UUID'); + $this->addBehavior('Timestamp'); $this->addBehavior('Tags.Tag'); $this->hasMany( 'Alignments', diff --git a/src/Model/Table/InstanceTable.php b/src/Model/Table/InstanceTable.php index c1870f4..dcb9741 100644 --- a/src/Model/Table/InstanceTable.php +++ b/src/Model/Table/InstanceTable.php @@ -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(); diff --git a/src/Model/Table/LocalToolsTable.php b/src/Model/Table/LocalToolsTable.php index cb64588..8f2bfea 100644 --- a/src/Model/Table/LocalToolsTable.php +++ b/src/Model/Table/LocalToolsTable.php @@ -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 diff --git a/src/Model/Table/MetaTemplatesTable.php b/src/Model/Table/MetaTemplatesTable.php index 6077e8d..9e01ccd 100644 --- a/src/Model/Table/MetaTemplatesTable.php +++ b/src/Model/Table/MetaTemplatesTable.php @@ -13,6 +13,7 @@ class MetaTemplatesTable extends AppTable public function initialize(array $config): void { parent::initialize($config); + $this->addBehavior('Timestamp'); $this->hasMany( 'MetaTemplateFields', [ diff --git a/src/Model/Table/OrganisationsTable.php b/src/Model/Table/OrganisationsTable.php index 2ac2b66..254a570 100644 --- a/src/Model/Table/OrganisationsTable.php +++ b/src/Model/Table/OrganisationsTable.php @@ -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', diff --git a/src/Model/Table/PhinxlogTable.php b/src/Model/Table/PhinxlogTable.php index 36d9222..5cdde77 100644 --- a/src/Model/Table/PhinxlogTable.php +++ b/src/Model/Table/PhinxlogTable.php @@ -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) { diff --git a/src/Model/Table/SharingGroupsTable.php b/src/Model/Table/SharingGroupsTable.php index 6f18157..ec3791e 100644 --- a/src/Model/Table/SharingGroupsTable.php +++ b/src/Model/Table/SharingGroupsTable.php @@ -14,6 +14,7 @@ class SharingGroupsTable extends AppTable { parent::initialize($config); $this->addBehavior('UUID'); + $this->addBehavior('Timestamp'); $this->belongsTo( 'Users' );