Laravel Auditing

2025-04-17

パクリ39発目!!
まだまだ勢い続く

パクリ元

内容

ログに関して検索してたら出て来たのでパクリ元1つ目を確認。

準備

インストール&ファイル作成

~/example-app$ sail composer require owen-it/laravel-auditing
・・・
Using version ^14.0 for owen-it/laravel-auditing
~/example-app$ sail artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="config"
・・・
~/example-app$ sail artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="migrations"

プロバイダー追加

app/bootstrap/providers.php
<?php

return [
    App\Providers\AppServiceProvider::class,
    App\Providers\FortifyServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    OwenIt\Auditing\AuditingServiceProvider::class,
];

 マイグレーション

~/example-app$ sail artisan migrate
・・・

監査対象のモデルをセットアップ

書いてあるとおりでやってみる。

app/Models/User.php
・・・
use OwenIt\Auditing\Contracts\Auditable;
・・・
- class User extends Authenticatable implements MustVerifyEmail
+ class User extends Authenticatable implements MustVerifyEmail, Auditable
・・・
    use HasFactory, Notifiable, TwoFactorAuthenticatable, HasRoles;
    use HasFactory, Notifiable, TwoFactorAuthenticatable, HasRoles, \OwenIt\Auditing\Auditable;
・・・

確認

ログインしてダッシュボードからプロフィール更新してみる。

おお~神よ!新旧データ見れるしIPもあるしでこれ履歴保持にももってこい、サイコーだね、ログアウトしただけで remember_token が更新されて1行できるのか、、なるほど。結構大きくなりそうなこのテーブルはパーティショニングするべきだな、、

~/example-app$ sail artisan make:migration alter_audits_table --table=audits
・・・
database/migrations/yyyy_mm_dd_hhmiss_alter_audits_table.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.
     */
    public function up(): void
    {
        Schema::table('audits', function (Blueprint $table) {
            $table->datetime('created_date')->after('tags')->default(DB::raw('CURRENT_TIMESTAMP'));
        });

        DB::statement("CREATE UNIQUE INDEX audits_unique_index1 ON audits (id, created_date)");
        DB::statement("ALTER TABLE audits DROP PRIMARY KEY");
        DB::statement("ALTER TABLE audits ADD PRIMARY KEY (id, created_date)");
        
        DB::statement("
            ALTER TABLE audits PARTITION BY RANGE COLUMNS(created_date) (
                PARTITION p2025 VALUES LESS THAN ('2026-01-01 00:00:00'),
                PARTITION p2026 VALUES LESS THAN ('2027-01-01 00:00:00'),
                PARTITION p2027 VALUES LESS THAN ('2028-01-01 00:00:00'),
                PARTITION p2028 VALUES LESS THAN ('2029-01-01 00:00:00'),
                PARTITION p2029 VALUES LESS THAN ('2030-01-01 00:00:00'),
                PARTITION p2030 VALUES LESS THAN ('2031-01-01 00:00:00'),
                PARTITION p2031 VALUES LESS THAN ('2032-01-01 00:00:00'),
                PARTITION p2032 VALUES LESS THAN ('2033-01-01 00:00:00'),
                PARTITION p2033 VALUES LESS THAN ('2034-01-01 00:00:00'),
                PARTITION p2034 VALUES LESS THAN ('2035-01-01 00:00:00')
            )
        ");
        
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        DB::statement("ALTER TABLE audits REMOVE PARTITIONING;");
        DB::statement("ALTER TABLE audits DROP PRIMARY KEY");
        DB::statement("ALTER TABLE audits ADD PRIMARY KEY (id)");
        DB::statement("ALTER TABLE audits DROP INDEX audits_unique_index1");

        Schema::table('audits', function (Blueprint $table) {
            $table->dropColumn('created_date');
        });
    }
};

~/example-app$ sail artisan migrate
・・・

10年は持つな・・

雑感

今回もデータを管理できる画面まで作りたいところだが大仕事になりそうなので今回もやめとく。とりあえず laravel 側はここまでにしておいて react 側をきれいにしておこう。本業忙しくなる前に・・

laravel,PHP

Posted by ak