Optimize Groups

This commit is contained in:
Deon George 2023-05-04 10:59:31 +10:00
parent 0ac35c3d43
commit 95bb55aad8
4 changed files with 128 additions and 7 deletions

View File

@ -6,6 +6,5 @@ use Illuminate\Database\Eloquent\Model;
class AccountGroup extends Model class AccountGroup extends Model
{ {
protected $table = 'ab_account_group'; protected $table = 'account_group';
public $timestamps = FALSE;
} }

View File

@ -7,8 +7,5 @@ use Illuminate\Database\Eloquent\Model;
class Group extends Model class Group extends Model
{ {
use HasFactory; use HasFactory;
protected $table = 'ab_group';
public $timestamps = FALSE;
} }

View File

@ -2,9 +2,10 @@
namespace Database\Factories; namespace Database\Factories;
use App\Models\Group;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Group;
class GroupFactory extends Factory class GroupFactory extends Factory
{ {
/** /**

View File

@ -0,0 +1,124 @@
<?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');
}
};