diff --git a/app/Models/AccountGroup.php b/app/Models/AccountGroup.php index e47c74b..d50ed01 100644 --- a/app/Models/AccountGroup.php +++ b/app/Models/AccountGroup.php @@ -6,6 +6,5 @@ use Illuminate\Database\Eloquent\Model; class AccountGroup extends Model { - protected $table = 'ab_account_group'; - public $timestamps = FALSE; + protected $table = 'account_group'; } \ No newline at end of file diff --git a/app/Models/Group.php b/app/Models/Group.php index 2e68733..0e17d02 100644 --- a/app/Models/Group.php +++ b/app/Models/Group.php @@ -7,8 +7,5 @@ use Illuminate\Database\Eloquent\Model; class Group extends Model { - use HasFactory; - - protected $table = 'ab_group'; - public $timestamps = FALSE; + use HasFactory; } diff --git a/database/factories/GroupFactory.php b/database/factories/GroupFactory.php index 35f196a..69bdb68 100644 --- a/database/factories/GroupFactory.php +++ b/database/factories/GroupFactory.php @@ -2,9 +2,10 @@ namespace Database\Factories; -use App\Models\Group; use Illuminate\Database\Eloquent\Factories\Factory; +use App\Models\Group; + class GroupFactory extends Factory { /** diff --git a/database/migrations/2023_05_04_100244_optimize_group.php b/database/migrations/2023_05_04_100244_optimize_group.php new file mode 100644 index 0000000..ddc51c7 --- /dev/null +++ b/database/migrations/2023_05_04_100244_optimize_group.php @@ -0,0 +1,124 @@ +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'); + } +};