diff --git a/app/Console/Commands/BusinessDataPlan.php b/app/Console/Commands/BusinessDataPlan.php index b29a27af..b12a5687 100644 --- a/app/Console/Commands/BusinessDataPlan.php +++ b/app/Console/Commands/BusinessDataPlan.php @@ -80,10 +80,10 @@ class BusinessDataPlan extends Command ->where('TABLE_SCHEMA', $database) ->get(['TABLE_NAME', 'TABLE_ROWS', 'DATA_LENGTH', 'INDEX_LENGTH']) ->mapWithKeys(static fn (object $row): array => [ - (string) $row->TABLE_NAME => [[ + (string) $row->TABLE_NAME => [ 'rows' => $row->TABLE_ROWS === null ? null : (int) $row->TABLE_ROWS, 'bytes' => (int) $row->DATA_LENGTH + (int) $row->INDEX_LENGTH, - ]], + ], ])->all(); } } diff --git a/tests/Unit/Console/BusinessDataPlanTest.php b/tests/Unit/Console/BusinessDataPlanTest.php new file mode 100644 index 00000000..334b7013 --- /dev/null +++ b/tests/Unit/Console/BusinessDataPlanTest.php @@ -0,0 +1,43 @@ +shouldReceive('connection')->once()->andReturn($connection); + $connection->shouldReceive('getDriverName')->once()->andReturn('mysql'); + $connection->shouldReceive('getDatabaseName')->once()->andReturn('caiwu_business_mirror'); + $connection->shouldReceive('table')->once()->with('information_schema.TABLES')->andReturn($query); + $query->shouldReceive('where')->once()->with('TABLE_SCHEMA', 'caiwu_business_mirror')->andReturnSelf(); + $query->shouldReceive('get')->once()->with(['TABLE_NAME', 'TABLE_ROWS', 'DATA_LENGTH', 'INDEX_LENGTH']) + ->andReturn(new Collection([(object) [ + 'TABLE_NAME' => 'fact_rows', + 'TABLE_ROWS' => 123, + 'DATA_LENGTH' => 4096, + 'INDEX_LENGTH' => 1024, + ]])); + + $method = new ReflectionMethod(BusinessDataPlan::class, 'tableSizes'); + $sizes = $method->invoke(new BusinessDataPlan(), $placements); + + $this->assertSame(['rows' => 123, 'bytes' => 5120], $sizes['fact_rows']); + } +}