mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-07 21:33:18 +01:00
- Rename the audit actor columns user_type/user_id to actor_type/actor_id - Add owner_organization_id and owner_user_id to the audits table as foreign keys with cascade on delete, so that the audits of an organization or user are deleted together with it. The indexes and foreign keys are created without blocking writes on the large table. - Fill the owner columns for new audits via CustomAuditable. Organizations and users no longer record their own deletion audit, since it would reference the already deleted owner.
52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Illuminate\Database\Migrations\Migration;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class CreateAuditsTable extends Migration
|
|
{
|
|
/**
|
|
* Run the migrations.
|
|
*/
|
|
public function up(): void
|
|
{
|
|
$connection = config('audit.drivers.database.connection', config('database.default'));
|
|
$table = config('audit.drivers.database.table', 'audits');
|
|
|
|
Schema::connection($connection)->create($table, function (Blueprint $table): void {
|
|
|
|
// Note: The morph prefix is hardcoded, since the columns are renamed in a later migration
|
|
$morphPrefix = 'user';
|
|
|
|
$table->bigIncrements('id');
|
|
$table->string($morphPrefix.'_type')->nullable();
|
|
$table->uuid($morphPrefix.'_id')->nullable();
|
|
$table->string('event');
|
|
$table->uuidMorphs('auditable');
|
|
$table->json('old_values')->nullable();
|
|
$table->json('new_values')->nullable();
|
|
$table->text('url')->nullable();
|
|
$table->ipAddress('ip_address')->nullable();
|
|
$table->string('user_agent', 1023)->nullable();
|
|
$table->string('tags')->nullable();
|
|
$table->timestamps();
|
|
|
|
$table->index([$morphPrefix.'_id', $morphPrefix.'_type']);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Reverse the migrations.
|
|
*/
|
|
public function down(): void
|
|
{
|
|
$connection = config('audit.drivers.database.connection', config('database.default'));
|
|
$table = config('audit.drivers.database.table', 'audits');
|
|
|
|
Schema::connection($connection)->drop($table);
|
|
}
|
|
}
|