2026-05-16 17:37:29 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
use App\Services\MonthlyPartitionManager;
|
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2026-05-23 20:20:54 +03:00
|
|
|
use Tests\Concerns\SharesSupplierPdo;
|
2026-05-16 17:37:29 +03:00
|
|
|
|
2026-05-23 20:20:54 +03:00
|
|
|
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
|
|
|
|
|
// ensureMonth теперь делает CREATE через pgsql_supplier (см. MonthlyPartitionManager::DDL_CONNECTION).
|
|
|
|
|
// Без SharesSupplierPdo DDL уйдёт мимо test-транзакции и партиции протечь в test DB.
|
2026-05-16 17:37:29 +03:00
|
|
|
|
|
|
|
|
function partitionExists(string $name): bool
|
|
|
|
|
{
|
|
|
|
|
return DB::selectOne(
|
|
|
|
|
"SELECT 1 AS ok FROM pg_class WHERE relname = ? AND relkind = 'r'",
|
|
|
|
|
[$name],
|
|
|
|
|
) !== null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 15:50:37 +03:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Existing tests (deals — business table, received_at key)
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
2026-05-16 17:37:29 +03:00
|
|
|
test('ensureRange создаёт месячные партиции deals под диапазон', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
|
|
|
|
|
$created = $manager->ensureRange(
|
|
|
|
|
'deals',
|
|
|
|
|
Carbon::parse('2024-02-15'),
|
|
|
|
|
Carbon::parse('2024-04-03'),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect($created)->toBeGreaterThanOrEqual(3)
|
2026-05-23 15:50:37 +03:00
|
|
|
->and(partitionExists('deals_y2024_m02'))->toBeTrue()
|
|
|
|
|
->and(partitionExists('deals_y2024_m03'))->toBeTrue()
|
|
|
|
|
->and(partitionExists('deals_y2024_m04'))->toBeTrue();
|
2026-05-16 17:37:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('ensureRange идемпотентна — повторный вызов не падает', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
|
|
|
|
|
$manager->ensureRange('deals', Carbon::parse('2024-02-15'), Carbon::parse('2024-02-20'));
|
|
|
|
|
$secondRun = $manager->ensureRange('deals', Carbon::parse('2024-02-15'), Carbon::parse('2024-02-20'));
|
|
|
|
|
|
|
|
|
|
expect($secondRun)->toBe(0); // всё уже существует
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('ensureRange отвергает неизвестную таблицу', function (): void {
|
|
|
|
|
app(MonthlyPartitionManager::class)->ensureRange('orders', now(), now());
|
|
|
|
|
})->throws(InvalidArgumentException::class);
|
2026-05-23 15:50:37 +03:00
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Hole #2 tests: audit tables (created_at key)
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
test('ensureMonth создаёт партицию auth_log (created_at)', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
$month = Carbon::parse('2024-03-01');
|
|
|
|
|
|
|
|
|
|
$manager->ensureMonth('auth_log', $month);
|
|
|
|
|
|
|
|
|
|
expect(partitionExists('auth_log_y2024_m03'))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('ensureMonth создаёт партицию activity_log (created_at)', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
$manager->ensureMonth('activity_log', Carbon::parse('2024-03-01'));
|
|
|
|
|
|
|
|
|
|
expect(partitionExists('activity_log_y2024_m03'))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('ensureMonth создаёт партицию tenant_operations_log (created_at)', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
$manager->ensureMonth('tenant_operations_log', Carbon::parse('2024-03-01'));
|
|
|
|
|
|
|
|
|
|
expect(partitionExists('tenant_operations_log_y2024_m03'))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('ensureMonth создаёт партицию webhook_log (received_at)', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
$manager->ensureMonth('webhook_log', Carbon::parse('2024-03-01'));
|
|
|
|
|
|
|
|
|
|
expect(partitionExists('webhook_log_y2024_m03'))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('ensureMonth создаёт партицию balance_transactions (created_at)', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
$manager->ensureMonth('balance_transactions', Carbon::parse('2024-03-01'));
|
|
|
|
|
|
|
|
|
|
expect(partitionExists('balance_transactions_y2024_m03'))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('ensureMonth создаёт партицию pd_processing_log (created_at)', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
$manager->ensureMonth('pd_processing_log', Carbon::parse('2024-03-01'));
|
|
|
|
|
|
|
|
|
|
expect(partitionExists('pd_processing_log_y2024_m03'))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('ensureMonth создаёт партицию saas_admin_audit_log (created_at)', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
$manager->ensureMonth('saas_admin_audit_log', Carbon::parse('2024-03-01'));
|
|
|
|
|
|
|
|
|
|
expect(partitionExists('saas_admin_audit_log_y2024_m03'))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('partitionName возвращает правильный формат', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
|
|
|
|
|
expect($manager->partitionName('auth_log', Carbon::parse('2026-05-15')))
|
|
|
|
|
->toBe('auth_log_y2026_m05');
|
|
|
|
|
|
|
|
|
|
expect($manager->partitionName('deals', Carbon::parse('2024-01-01')))
|
|
|
|
|
->toBe('deals_y2024_m01');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('listPartitions возвращает созданные партиции', function (): void {
|
|
|
|
|
$manager = app(MonthlyPartitionManager::class);
|
|
|
|
|
$manager->ensureMonth('auth_log', Carbon::parse('2024-04-01'));
|
|
|
|
|
$manager->ensureMonth('auth_log', Carbon::parse('2024-05-01'));
|
|
|
|
|
|
|
|
|
|
$partitions = $manager->listPartitions('auth_log');
|
|
|
|
|
|
|
|
|
|
expect($partitions)->toContain('auth_log_y2024_m04')
|
|
|
|
|
->toContain('auth_log_y2024_m05');
|
|
|
|
|
});
|