-
Notifications
You must be signed in to change notification settings - Fork 185
/
20181002234013_create_user_joind_in_username_column.php
73 lines (63 loc) · 2.21 KB
/
20181002234013_create_user_joind_in_username_column.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
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
/**
* Copyright (c) 2013-2018 OpenCFP
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/opencfp/opencfp
*/
use Cartalyst\Sentinel\Users\EloquentUser;
use Phinx\Migration\AbstractMigration;
class CreateUserJoindInUsernameColumn extends AbstractMigration
{
/** @var Capsule $capsule */
public $capsule;
public function bootEloquent(): void
{
$adapter = $this->getAdapter()->getAdapter();
$options = $adapter->getOptions();
$this->capsule = new Capsule();
$this->capsule->addConnection([
'driver' => 'mysql',
'database' => $options['name'],
]);
$this->capsule->getConnection()->setPdo($adapter->getConnection());
$this->capsule->bootEloquent();
$this->capsule->setAsGlobal();
}
public function up(): void
{
// Create joindin_username
$this->table('users')
->addColumn('joindin_username', 'string', ['null' => true])
->update();
// Go through each record in user, strip out (https://joind.in/user/) and copy to joindin_username
$joindInRegex = '/^https:\/\/joind\.in\/user\/(.{1,100})$/';
$users = EloquentUser::all();
foreach ($users as $user) {
if (\preg_match($joindInRegex, $user->url, $matches) === 1) {
$user->joindin_username = $matches[1];
$user->url = null;
$user->save();
}
}
}
public function down(): void
{
// Go through each record in user, update `url` to move the joindin_username to there
$users = EloquentUser::all();
foreach ($users as $user) {
$user->url = $user->joindin_username
? 'https://joind.in/user/' . $user->joindin_username
: null;
$user->joindin_username = null;
$user->save();
}
// Drop the joindin_username column
$this->table('users')
->removeColumn('joindin_username')
->update();
}
}