Skip to content

Commit eb40ce6

Browse files
committed
Improve audit logging robustness during migrations
Moved audit table existence checks from the service provider to the AuditService to prevent errors during migrations or installation when the audit table may not exist yet. Updated migration publishing to include the audit logs table migration.
1 parent feddba6 commit eb40ce6

File tree

3 files changed

+18
-32
lines changed

3 files changed

+18
-32
lines changed

database/migrations/create_entity_ability_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Config;
56
use Illuminate\Support\Facades\Schema;
67

78
return new class () extends Migration {

src/SimplePermissionsServiceProvider.php

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use Exception;
66
use Illuminate\Support\Facades\Blade;
77
use Illuminate\Support\Facades\Config;
8-
use Illuminate\Support\Facades\Schema;
98
use Illuminate\Support\ServiceProvider;
10-
use Squareetlabs\LaravelSimplePermissions\Exceptions\AuditTableMissingException;
119
use Squareetlabs\LaravelSimplePermissions\Support\Services\SimplePermissionsService;
1210
use Squareetlabs\LaravelSimplePermissions\Middleware\Ability as AbilityMiddleware;
1311
use Squareetlabs\LaravelSimplePermissions\Middleware\Permission as PermissionMiddleware;
@@ -61,6 +59,7 @@ protected function configurePublishing(): void
6159
__DIR__ . '/../database/migrations/create_groups_table.php' => database_path('migrations/2019_12_14_000008_create_groups_table.php'),
6260
__DIR__ . '/../database/migrations/create_group_user_table.php' => database_path('migrations/2019_12_14_000009_create_group_user_table.php'),
6361
__DIR__ . '/../database/migrations/create_entity_permission_table.php' => database_path('migrations/2019_12_14_000010_create_entity_permission_table.php'),
62+
__DIR__ . '/../database/migrations/create_audit_logs_table.php' => database_path('migrations/2019_12_14_000011_create_audit_logs_table.php'),
6463
];
6564

6665
$migrations[__DIR__ . '/../database/migrations/add_performance_indexes.php'] = database_path('migrations/2019_12_14_000014_add_performance_indexes.php');
@@ -156,38 +155,16 @@ protected function registerBladeDirectives(): void
156155
/**
157156
* Validate audit configuration.
158157
*
158+
* Note: We don't validate the table existence here to avoid blocking migrations.
159+
* The AuditService will check if the table exists before attempting to log,
160+
* and will silently skip logging if the table doesn't exist (e.g., during migrations).
161+
*
159162
* @return void
160-
* @throws AuditTableMissingException
161163
*/
162164
protected function validateAuditConfiguration(): void
163165
{
164-
if (!Config::get('simple-permissions.audit.enabled')) {
165-
return;
166-
}
167-
168-
// No validar durante migraciones o instalación
169-
// La validación real se hace en AuditService cuando se intenta usar
170-
if ($this->app->runningInConsole()) {
171-
$command = $this->app->runningUnitTests() ? null : ($_SERVER['argv'][1] ?? null);
172-
173-
// Saltar validación durante migraciones
174-
if (in_array($command, ['migrate', 'migrate:fresh', 'migrate:refresh', 'migrate:reset', 'migrate:rollback', 'migrate:status'])) {
175-
return;
176-
}
177-
}
178-
179-
// Validar que la tabla existe si la auditoría está habilitada
180-
try {
181-
if (!Schema::hasTable('audit_logs')) {
182-
throw new AuditTableMissingException();
183-
}
184-
} catch (\Exception $e) {
185-
// Si hay un error de conexión a la BD, no validar aún
186-
// (puede ser que la BD aún no esté configurada)
187-
if (str_contains($e->getMessage(), 'Connection') || str_contains($e->getMessage(), 'SQLSTATE')) {
188-
return;
189-
}
190-
throw $e;
191-
}
166+
// Validation is done at runtime in AuditService when attempting to log
167+
// This prevents blocking migrations or installation when audit is enabled
168+
// but the table hasn't been created yet
192169
}
193170
}

src/Support/Services/AuditService.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ public function log(
4646
?array $oldValues = null,
4747
?array $newValues = null
4848
): void {
49-
if (!Config::get('simple-permissions.audit.enabled', true)) {
49+
if (!Config::get('simple-permissions.audit.enabled', false)) {
50+
return;
51+
}
52+
53+
// Check if audit table exists before attempting to log
54+
// This prevents errors during migrations or if table hasn't been created yet
55+
if (!Schema::hasTable('audit_logs')) {
56+
// Silently skip logging if table doesn't exist (e.g., during migrations)
57+
// The table will be created when migrations run
5058
return;
5159
}
5260

0 commit comments

Comments
 (0)