m20200720_143700_user_make_id_the_uuid   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 67
c 1
b 1
f 0
dl 0
loc 109
ccs 0
cts 89
cp 0
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A safeDown() 0 51 5
A safeUp() 0 51 5
1
<?php
2
3
use neon\core\db\Migration;
4
5
use \neon\user\models\User;
6
use \neon\user\services\apiTokenManager\models\UserApiToken;
7
8
class m20200720_143700_user_make_id_the_uuid extends Migration
9
{
10
	public function safeUp()
11
	{
12
		// get all the users
13
		$users = User::find()->all();
14
15
		// create a map of user int id to uuid
16
		$idToUuuid = collect($users)->pluck('uuid', 'id');
17
18
		// make the id column a uuid64 id
19
		$this->alterColumn(User::tableName(), 'id', $this->uuid64());
20
		$this->addColumn(User::tableName(), 'old_id', $this->integer());
21
		$this->alterColumn(UserApiToken::tableName(), 'user_id', $this->uuid64());
22
23
		$apiTokens = UserApiToken::find()->all();
24
		$tableApiToken = UserApiToken::tableName();
25
		foreach($apiTokens as $apiToken) {
26
			$this->execute("UPDATE $tableApiToken SET user_id = :uuid where user_id = :id", [
27
				':uuid' =>  $idToUuuid->get($apiToken->user_id),
28
				':id' =>  $apiToken->user_id
29
			]);
30
		}
31
		$tableUser = User::tableName();
32
		foreach($users as $user) {
33
			$this->execute("UPDATE $tableUser SET id = :uuid, old_id = :id where id = :id", [
34
				':uuid' => $idToUuuid->get($user->id),
35
				':id' => $user->id
36
			]);
37
		}
38
39
		$authTable = \neon\user\models\AuthAssignment::tableName();
40
		$authAssignments = \neon\user\models\AuthAssignment::find()->all();
41
		foreach($authAssignments as $auth) {
42
			$this->execute("UPDATE $authTable SET user_id = :uuid where user_id = :id", [
43
				':uuid' => $idToUuuid->get($auth->user_id),
44
				':id' => $auth->user_id
45
			]);
46
		}
47
48
		// update firefly created_by and updated_by
49
		$ffs = \neon\firefly\services\fileManager\models\FileManager::find()->all();
50
		$tableFfs = \neon\firefly\services\fileManager\models\FileManager::tableName();
51
		$this->alterColumn($tableFfs, 'created_by', $this->uuid64());
52
		$this->alterColumn($tableFfs, 'updated_by', $this->uuid64());
53
		foreach($ffs as $f) {
54
			$this->execute("UPDATE $tableFfs SET created_by = :uuid where created_by = :id", [
55
				':uuid' => $idToUuuid->get($f->created_by),
56
				':id' => $f->created_by
57
			]);
58
			$this->execute("UPDATE $tableFfs SET updated_by = :uuid where updated_by = :id", [
59
				':uuid' => $idToUuuid->get($f->updated_by),
60
				':id' => $f->updated_by
61
			]);
62
		}
63
64
	}
65
66
	public function safeDown()
67
	{
68
		// get all the users
69
		$users = User::find()->all();
70
		$tableUser = User::tableName();
71
		// create a map of user int id to uuid
72
		$uuidToId = collect($users)->pluck('old_id', 'id');
73
74
		$apiTokens = UserApiToken::find()->all();
75
		$tableApiToken = UserApiToken::tableName();
76
		foreach($apiTokens as $apiToken) {
77
			$this->execute("UPDATE $tableApiToken SET user_id = :id where user_id = :uuid", [
78
				':id' =>  $uuidToId->get($apiToken->user_id),
79
				':uuid' =>  $apiToken->user_id
80
			]);
81
		}
82
83
		foreach($users as $user) {
84
			$this->execute("UPDATE $tableUser SET id = :id where id = :uuid", [
85
				':id' => $uuidToId->get($user->id),
86
				':uuid' => $user->id
87
			]);
88
		}
89
90
		$authTable = \neon\user\models\AuthAssignment::tableName();
91
		$authAssignments = \neon\user\models\AuthAssignment::find()->all();
92
		foreach($authAssignments as $auth) {
93
			$this->execute("UPDATE $authTable SET user_id = :id where user_id = :uuid", [
94
				':id' => $uuidToId->get($auth->user_id),
95
				':uuid' => $auth->user_id
96
			]);
97
		}
98
99
		// update firefly created_by and updated_by
100
		$ffs = \neon\firefly\services\fileManager\models\FileManager::find()->all();
101
		$tableFfs = \neon\firefly\services\fileManager\models\FileManager::tableName();
102
		foreach($ffs as $f) {
103
			$this->execute("UPDATE $tableFfs SET created_by = :id where created_by = :uuid", [
104
				':id' => $uuidToId->get($f->created_by),
105
				':uuid' => $f->created_by
106
			]);
107
			$this->execute("UPDATE $tableFfs SET updated_by = :id where updated_by = :uuid", [
108
				':id' => $uuidToId->get($f->updated_by),
109
				':uuid' => $f->updated_by
110
			]);
111
		}
112
		$this->alterColumn($tableFfs, 'created_by', $this->integer());
113
		$this->alterColumn($tableFfs, 'updated_by', $this->integer());
114
		$this->alterColumn(User::tableName(), 'id', $this->integer());
115
		$this->alterColumn(UserApiToken::tableName(), 'user_id', $this->integer());
116
		$this->dropColumn(User::tableName(), 'old_id');
117
	}
118
}
119