diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2022-11-24 10:29:24 +0000 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2022-11-24 10:29:24 +0000 |
| commit | 7dddea8ccca601bf2da28960f2e27a223fe60ea6 (patch) | |
| tree | b34ee92f9a4e0e4c1f7e4dcf5f15b396f9097d6f /db | |
| parent | 1231022837beceedef376e4bb8084ff38fbc7d93 (diff) | |
rework aux lock rules, use sorm model, deprecate old class and let name and description be translatable, fixes #1791
Closes #1791
Merge request studip/studip!1177
Diffstat (limited to 'db')
| -rw-r--r-- | db/migrations/5.3.9_convert_aux_lock_rules_json_fields.php | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/db/migrations/5.3.9_convert_aux_lock_rules_json_fields.php b/db/migrations/5.3.9_convert_aux_lock_rules_json_fields.php new file mode 100644 index 0000000..56b1c2b --- /dev/null +++ b/db/migrations/5.3.9_convert_aux_lock_rules_json_fields.php @@ -0,0 +1,56 @@ +<?php +final class ConvertAuxLockRulesJsonFields extends Migration +{ + public function description() + { + return parent::description(); // TODO: Change the autogenerated stub + } + + protected function up() + { + $query = "SELECT `lock_id`, `attributes`, `sorting` FROM `aux_lock_rules`"; + $rows = DBManager::get()->fetchAll($query); + + $query = "UPDATE `aux_lock_rules` + SET `attributes` = :attributes, + `sorting` = :sorting + WHERE `lock_id` = :id"; + $statement = DBManager::get()->prepare($query); + + foreach ($rows as $row) { + $attributes = json_decode($row['attributes'], true) ?: []; + $attributes = array_filter($attributes); + $attributes = array_keys($attributes); + + $sorting = json_decode($row['sorting'], true) ?: []; + $sorting = array_filter($sorting, function ($id) use ($attributes) { + return in_array($id, $attributes); + }, ARRAY_FILTER_USE_KEY); + + $statement->bindValue(':id', $row['lock_id']); + $statement->bindValue(':attributes', json_encode($attributes)); + $statement->bindValue(':sorting', json_encode($sorting)); + $statement->execute(); + } + } + + protected function down() + { + $query = "SELECT `lock_id`, `attributes` FROM `aux_lock_rules`"; + $rows = DBManager::get()->fetchAll($query); + + $query = "UPDATE `aux_lock_rules` + SET `attributes` = :attributes + WHERE `lock_id` = :id"; + $statement = DBManager::get()->prepare($query); + + foreach ($rows as $row) { + $attributes = json_decode($row['attributes'], true) ?: []; + $attributes = array_fill_keys($attributes, '1'); + + $statement->bindValue(':id', $row['lock_id']); + $statement->bindValue(':attributes', json_encode($attributes)); + $statement->execute(); + } + } +} |
