-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cd6515
commit d4397c2
Showing
7 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |