-
Notifications
You must be signed in to change notification settings - Fork 633
/
m141222_110026_update_ip_field.php
65 lines (58 loc) · 2.06 KB
/
m141222_110026_update_ip_field.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use dektrium\user\migrations\Migration;
use yii\db\Query;
class m141222_110026_update_ip_field extends Migration
{
public function up()
{
$users = (new Query())->from('{{%user}}')->select('id, registration_ip ip')->all();
$transaction = Yii::$app->db->beginTransaction();
try {
$this->alterColumn('{{%user}}', 'registration_ip', $this->string(45)->null());
foreach ($users as $user) {
if ($user['ip'] == null) {
continue;
}
Yii::$app->db->createCommand()->update('{{%user}}', [
'registration_ip' => long2ip($user['ip']),
], 'id = ' . $user['id'])->execute();
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
}
public function down()
{
$users = (new Query())->from('{{%user}}')->select('id, registration_ip ip')->all();
$transaction = Yii::$app->db->beginTransaction();
try {
foreach ($users as $user) {
if ($user['ip'] == null) {
continue;
}
Yii::$app->db->createCommand()->update('{{%user}}', [
'registration_ip' => ip2long($user['ip'])
], 'id = ' . $user['id'])->execute();
}
if ($this->dbType == 'pgsql') {
$this->alterColumn('{{%user}}', 'registration_ip', $this->bigInteger() . ' USING registration_ip::bigint');
} else {
$this->alterColumn('{{%user}}', 'registration_ip', $this->bigInteger()->null());
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
}
}