osb/database/migrations/2023_05_04_100244_optimize_group.php
2023-05-04 11:50:54 +10:00

125 lines
4.4 KiB
PHP

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement('ALTER TABLE ab_group RENAME TO groups');
DB::statement('ALTER TABLE groups DROP PRIMARY KEY');
DB::statement('ALTER TABLE groups MODIFY id int unsigned NOT NULL');
DB::statement('ALTER TABLE groups MODIFY site_id int unsigned NOT NULL');
DB::statement('ALTER TABLE groups MODIFY parent_id int unsigned DEFAULT NULL');
DB::statement('ALTER TABLE groups MODIFY active tinyint(1) NOT NULL');
DB::statement('ALTER TABLE groups MODIFY pricing tinyint(1) NOT NULL');
Schema::table('groups', function (Blueprint $table) {
$table->index(['id','site_id']);
});
DB::statement('ALTER TABLE groups MODIFY COLUMN id INT unsigned auto_increment');
DB::statement('UPDATE groups set id=0 where id=1 and name="All Users"');
Schema::table('groups', function (Blueprint $table) {
$table->foreign(['site_id'])->references(['site_id'])->on('sites');
$table->foreign(['parent_id','site_id'])->references(['id','site_id'])->on('groups');
$table->dateTime('created_at')->nullable()->after('id');
$table->dateTime('updated_at')->nullable()->after('created_at');
$table->dateTime('start_at')->nullable()->after('updated_at');
$table->dateTime('expire_at')->nullable()->after('start_at');
});
// Convert out dates
foreach (\App\Models\Group::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
// If we are running again
if ($o->created_at)
continue;
if ($o->date_orig)
$o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
if ($o->date_last)
$o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
if ($o->date_expire)
$o->expire_at = \Carbon\Carbon::createFromTimestamp($o->date_expire);
if ($o->date_start)
$o->start_at = \Carbon\Carbon::createFromTimestamp($o->date_start);
$o->save();
}
Schema::table('groups', function (Blueprint $table) {
$table->dropColumn(['date_orig','date_expire','date_start']);
});
// ACCOUNT GROUP
DB::statement('DELETE FROM ab_account_group where id=1 and group_id=0 and account_id=0');
Schema::table('ab_account_group', function (Blueprint $table) {
$table->dropPrimary(['id','site_id','account_id','group_id']);
$table->dropForeign('ab_account_group_site_id_foreign');
$table->dropIndex('ab_account_group_site_id_foreign');
$table->dateTime('created_at')->nullable()->after('id');
$table->dateTime('updated_at')->nullable()->after('created_at');
$table->dateTime('start_at')->nullable()->after('updated_at');
$table->dateTime('expire_at')->nullable()->after('start_at');
});
DB::statement('ALTER TABLE ab_account_group RENAME TO account_group');
Schema::table('account_group', function (Blueprint $table) {
$table->index(['id','site_id']);
});
// Convert out dates
foreach (\App\Models\AccountGroup::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
// If we are running again
if ($o->created_at)
continue;
if ($o->date_orig)
$o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
if ($o->date_last)
$o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
if ($o->date_expire)
$o->expire_at = \Carbon\Carbon::createFromTimestamp($o->date_expire);
if ($o->date_start)
$o->start_at = \Carbon\Carbon::createFromTimestamp($o->date_start);
$o->save();
}
DB::statement('ALTER TABLE account_group MODIFY group_id int unsigned NOT NULL');
DB::statement('ALTER TABLE account_group MODIFY account_id int unsigned NOT NULL');
DB::statement('ALTER TABLE account_group MODIFY active tinyint(1) NOT NULL');
Schema::table('account_group', function (Blueprint $table) {
$table->dropIndex('ab_account_group_id_site_id_index');
$table->dropColumn(['date_orig','date_expire','date_start','service_id']);
$table->unique(['account_id','site_id']);
$table->foreign(['group_id','site_id'])->references(['id','site_id'])->on('groups');
$table->foreign(['account_id','site_id'])->references(['id','site_id'])->on('accounts');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
abort(500,'Cant go back');
}
};