1 <?php
2
3 /*
4 * This file is part of the Icybee package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Icybee\Modules\Nodes;
13
14 use ICanBoogie\Updater\Update;
15
16 /**
17 * - Renames the `created` columns as `created_as`.
18 * - Renames the `modified` columns as `updated_as`.
19 *
20 * @module nodes
21 */
22 class Update20131208 extends Update
23 {
24 public function update_column_created_at()
25 {
26 $this->module->model
27 ->assert_has_column('created')
28 ->rename_column('created', 'created_at');
29 }
30
31 public function update_column_updated_at()
32 {
33 $this->module->model
34 ->assert_has_column('modified')
35 ->create_column('updated_at');
36 }
37 }
38
39 /**
40 * Adds the `uuid` column.
41 *
42 * @module nodes
43 */
44 class Update20140405 extends Update
45 {
46 public function update_column_uuid()
47 {
48 $this->module->model
49 ->assert_not_has_column('uuid')
50 ->create_column('uuid');
51
52 #
53 # Update records with UUID values.
54 #
55
56 $target = $this->module->model->target;
57 $tokens = $target->select('nid, NULL')->pairs;
58
59 foreach (array_keys($tokens) as $nid)
60 {
61 for (;;)
62 {
63 $token = \ICanBoogie\generate_v4_uuid();
64
65 if (!in_array($token, $tokens))
66 {
67 break;
68 }
69 }
70
71 $tokens[$nid] = $token;
72 }
73
74 $update = $target->prepare("UPDATE `{self}` SET `uuid` = ? WHERE `nid` = ?");
75
76 foreach ($tokens as $nid => $token)
77 {
78 $update($token, $nid);
79 }
80
81 #
82 # Create index.
83 #
84
85 $this->module->model->create_unique_index('uuid');
86 }
87 }