Skip to content

Commit

Permalink
feat(exports): 导出数据库结构 (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaiyuxin103 committed Feb 11, 2025
1 parent 6cd6515 commit d4397c2
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 0 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ resources/views/emails
resources/views/mail
resources/views/vendor
resources/views/welcome.blade.php
storage/framework/cache/laravel-excel
storage/framework/views
composer.lock
docker-compose.yml
Expand Down
42 changes: 42 additions & 0 deletions app/Console/Commands/DatabaseStructExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;
use Throwable;

class DatabaseStructExport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:database-struct-export';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Export database structure to Excel file.';

/**
* Execute the console command.
*/
public function handle(): void
{
$this->info('Exporting database structure...');

try {
Excel::store(new \App\Exports\DatabaseStructExport(), 'database-struct.xlsx');
} catch (Throwable $th) {
$this->error('Failed to export database structure.');
}

$this->info('Database structure exported successfully.');
}
}
41 changes: 41 additions & 0 deletions app/Exports/DatabaseStructExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Exports;

use DB;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class DatabaseStructExport implements WithMultipleSheets
{
public array $tables;

public array $descriptions;

public function __construct()
{
$this->tables = array_map(function ($table) {
$key = 'Tables_in_' . Str::lower(DB::connection()->getDatabaseName());

// 获取表注释
$comment = DB::select('SHOW TABLE STATUS WHERE Name = ?', [$table->{$key}])[0]->Comment;

return [
'name' => $table->{$key},
'title' => $comment,
'description' => $comment,
];
}, DB::select('SHOW TABLES'));
}

public function sheets(): array
{
return array_merge([
new Sheets\TablesSheet($this->tables),
], array_map(function ($table) {
return new Sheets\TableSheet($table);
}, $this->tables));
}
}
59 changes: 59 additions & 0 deletions app/Exports/Sheets/TableSheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace App\Exports\Sheets;

use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class TableSheet implements FromView, ShouldAutoSize, WithColumnWidths, WithStyles, WithTitle
{
public array $columns;

public function __construct(public array $table)
{
$this->columns = DB::select('SHOW FULL COLUMNS FROM ' . data_get($this->table, 'name'));
}

public function title(): string
{
return data_get($this->table, 'name');
}

public function columnWidths(): array
{
return [
'B' => 20,
];
}

public function styles(Worksheet $sheet): void
{
$sheet->getStyle('B2:C4')->getBorders()->applyFromArray([
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
],
]);
$sheet->getStyle('B6:H' . (count($this->columns) + 6))->getBorders()->applyFromArray([
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
],
]);
}

public function view(): View
{
return view('exports.database_struct.table', [
'table' => $this->table,
'columns' => $this->columns,
]);
}
}
50 changes: 50 additions & 0 deletions app/Exports/Sheets/TablesSheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\Exports\Sheets;

use Illuminate\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class TablesSheet implements FromView, ShouldAutoSize, WithColumnWidths, WithStyles, WithTitle
{
public function __construct(public array $tables)
{
}

public function title(): string
{
return 'テーブル一覧';
}

public function columnWidths(): array
{
return [
'B' => 10,
];
}

public function styles(Worksheet $sheet): void
{
$sheet->getStyle('B2')->getFont()->setBold(true)->setSize(18);
$sheet->getStyle('B3:D57')->getBorders()->applyFromArray([
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
],
]);
}

public function view(): View
{
return view('exports.database_struct.tables', [
'tables' => $this->tables,
]);
}
}
52 changes: 52 additions & 0 deletions resources/views/exports/database_struct/table.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<table style="border: 1px solid black">
<tbody>
<tr></tr>
<tr>
<th></th>
<th style="background-color: #a1e3ff">テーブル名称</th>
<th>管理者と店舗の中間テーブル</th>
</tr>
<tr>
<th></th>
<th style="background-color: #a1e3ff">テーブル名</th>
<th>
{{ data_get($table, 'name') }}
</th>
</tr>
<tr>
<th></th>
<th style="background-color: #a1e3ff">概要</th>
<th>
{{ data_get($table, 'description') }}
</th>
</tr>
</tbody>
</table>
<table style="border: 1px solid black">
<thead>
<tr>
<th></th>
<th style="background-color: #a1e3ff">No.</th>
<th style="background-color: #a1e3ff">カラム名</th>
<th style="background-color: #a1e3ff">データ型</th>
<th style="background-color: #a1e3ff">桁数</th>
<th style="background-color: #a1e3ff">Not Null</th>
<th style="background-color: #a1e3ff">デフォルト</th>
<th style="background-color: #a1e3ff">備考</th>
</tr>
</thead>
<tbody>
@foreach ($columns as $key => $column)
<tr>
<td></td>
<td>{{ $key + 1 }}</td>
<td>{{ $column->Field }}</td>
<td>{{ $column->Type }}</td>
<td>{{ $column->Type }}</td>
<td>{{ $column->Null }}</td>
<td>{{ $column->Default }}</td>
<td>{{ $column->Comment }}</td>
</tr>
@endforeach
</tbody>
</table>
25 changes: 25 additions & 0 deletions resources/views/exports/database_struct/tables.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<table>
<tbody>
<tr></tr>
<tr>
<th></th>
<th colspan="3" style="text-align: center">テーブル一覧</th>
</tr>
<tr>
<th></th>
<th style="background-color: #a1e3ff">No.</th>
<th style="background-color: #a1e3ff">テーブル名</th>
<th style="background-color: #a1e3ff">備考</th>
</tr>
@foreach ($tables as $key => $table)
<tr>
<td></td>
<td>{{ $key + 1 }}</td>
<td>{{ data_get($table, 'name') }}</td>
<td>
{{ data_get($table, 'title') }}
</td>
</tr>
@endforeach
</tbody>
</table>

0 comments on commit d4397c2

Please sign in to comment.