feat(pargar): cap inbound mande/document logs after insert
Keep the newest 2000 success rows per inbound type so dumps stop growing from Pargar webhooks, and store compact document payloads instead of full JSON. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (! Schema::hasTable('pargar_sync_logs')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$indexNames = collect(DB::select('SHOW INDEX FROM `pargar_sync_logs`'))
|
||||
->pluck('Key_name')
|
||||
->all();
|
||||
|
||||
if (! in_array('pargar_sync_logs_type_status_index', $indexNames, true)) {
|
||||
Schema::table('pargar_sync_logs', function ($table) {
|
||||
$table->index(['type', 'sync_status'], 'pargar_sync_logs_type_status_index');
|
||||
});
|
||||
}
|
||||
|
||||
$this->dropEvents();
|
||||
|
||||
try {
|
||||
DB::statement('SET GLOBAL event_scheduler = ON');
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
|
||||
try {
|
||||
DB::unprepared(<<<'SQL'
|
||||
CREATE EVENT `khanegi_prune_pargar_inbound_docs`
|
||||
ON SCHEDULE EVERY 1 HOUR
|
||||
STARTS CURRENT_TIMESTAMP
|
||||
ON COMPLETION PRESERVE
|
||||
ENABLE
|
||||
DO
|
||||
DELETE FROM `pargar_sync_logs`
|
||||
WHERE `type` = 'document' AND `sync_status` = 'success'
|
||||
AND `id` <= (
|
||||
SELECT `cutoff` FROM (
|
||||
SELECT `id` AS `cutoff` FROM `pargar_sync_logs`
|
||||
WHERE `type` = 'document' AND `sync_status` = 'success'
|
||||
ORDER BY `id` DESC
|
||||
LIMIT 1 OFFSET 2000
|
||||
) AS `keep`
|
||||
)
|
||||
LIMIT 4000
|
||||
SQL);
|
||||
DB::unprepared(<<<'SQL'
|
||||
CREATE EVENT `khanegi_prune_pargar_inbound_mande`
|
||||
ON SCHEDULE EVERY 1 HOUR
|
||||
STARTS CURRENT_TIMESTAMP
|
||||
ON COMPLETION PRESERVE
|
||||
ENABLE
|
||||
DO
|
||||
DELETE FROM `pargar_sync_logs`
|
||||
WHERE `type` = 'mande' AND `sync_status` = 'success'
|
||||
AND `id` <= (
|
||||
SELECT `cutoff` FROM (
|
||||
SELECT `id` AS `cutoff` FROM `pargar_sync_logs`
|
||||
WHERE `type` = 'mande' AND `sync_status` = 'success'
|
||||
ORDER BY `id` DESC
|
||||
LIMIT 1 OFFSET 2000
|
||||
) AS `keep`
|
||||
)
|
||||
LIMIT 4000
|
||||
SQL);
|
||||
} catch (\Throwable) {
|
||||
// EVENT privilege / event_scheduler — پاکسازی روی INSERT لاگ ورودی میماند
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$this->dropEvents();
|
||||
}
|
||||
|
||||
private function dropEvents(): void
|
||||
{
|
||||
try {
|
||||
DB::unprepared('DROP EVENT IF EXISTS `khanegi_prune_pargar_inbound_docs`');
|
||||
DB::unprepared('DROP EVENT IF EXISTS `khanegi_prune_pargar_inbound_mande`');
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user