mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 09:11:40 +08:00
migrating user preferences obviously works on empty table
This commit is contained in:
parent
9d46446a8f
commit
a3c6833b54
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
return [
|
||||
'up' => function (Builder $schema) {
|
||||
$schema->create('notification_preferences', function (Blueprint $table) {
|
||||
$table->integer('user_id')->unsigned();
|
||||
$table->string('type');
|
||||
$table->string('channel');
|
||||
$table->boolean('enabled')->default(false);
|
||||
|
||||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
|
||||
});
|
||||
},
|
||||
|
||||
'down' => function (Builder $schema) {
|
||||
$schema->drop('notification_preferences');
|
||||
}
|
||||
];
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
return [
|
||||
'up' => function (Builder $schema) {
|
||||
$schema->table('users', function (Blueprint $table) {
|
||||
$table->boolean('disclose_online')->default(false);
|
||||
});
|
||||
},
|
||||
|
||||
'down' => function (Builder $schema) {
|
||||
$schema->table('users', function (Blueprint $table) {
|
||||
$table->dropColumn('disclose_online');
|
||||
});
|
||||
}
|
||||
];
|
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
return [
|
||||
'up' => function (Builder $builder) {
|
||||
$db = $builder->getConnection();
|
||||
|
||||
$db->table('users')
|
||||
->whereNotNull('preferences')
|
||||
->orderBy('id')
|
||||
->chunk(50, function (Collection $users) use ($db) {
|
||||
$users->each(function ($user) use ($db) {
|
||||
collect(json_decode(Arr::get($user, 'preferences', '{}')))
|
||||
->each(function ($value, $key) use ($user, $db) {
|
||||
if ($key === 'discloses_online') {
|
||||
$db->table('users')
|
||||
->where('id', $user['id'])
|
||||
->update(['discloses_online' => (bool) $value]);
|
||||
}
|
||||
if (preg_match('/^notify_(?<type>[^_]+)_(?<channel>.*)$/', $key, $matches)) {
|
||||
$db->table('notification_preferences')
|
||||
->insert([
|
||||
'user_id' => $user['id'],
|
||||
'type' => $matches['type'],
|
||||
'channel' => $matches['channel'],
|
||||
'enabled' => (bool) $value
|
||||
]);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
'down' => function (Builder $builder) {
|
||||
$db = $builder->getConnection();
|
||||
|
||||
$db->table('notification_preferences')->truncate();
|
||||
}
|
||||
];
|
Loading…
x
Reference in New Issue
Block a user