Schemas.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Modules\Develop\Models;
  3. use Catch\Base\CatchModel;
  4. use Catch\Enums\Status;
  5. use Exception;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Schema as SchemaFacade;
  9. use Modules\Develop\Support\Generate\Create\Schema;
  10. class Schemas extends CatchModel
  11. {
  12. /**
  13. * @var string
  14. */
  15. protected $table = 'schemas';
  16. /**
  17. * @var string[]
  18. */
  19. protected $fillable = [
  20. 'id', 'module', 'name', 'columns', 'is_soft_delete', 'created_at', 'updated_at'
  21. ];
  22. /**
  23. * @var array|string[]
  24. */
  25. public array $searchable = ['module' => 'like', 'name' => 'like'];
  26. /**
  27. * @var string[]
  28. */
  29. protected $casts = [
  30. 'is_soft_delete' => Status::class
  31. ];
  32. /**
  33. *
  34. * @param array $data
  35. * @return boolean
  36. * @throws Exception
  37. */
  38. public function storeBy(array $data): bool
  39. {
  40. $schema = $data['schema'];
  41. $structures = $data['structures'];
  42. $schemaId = parent::storeBy([
  43. 'module' => $schema['module'],
  44. 'name' => $schema['name'],
  45. 'columns' => implode(',', array_column($structures, 'field')),
  46. 'is_soft_delete' => $schema['deleted_at'] ? Status::Enable : Status::Disable
  47. ], true);
  48. try {
  49. $schemaCreate = new Schema($schema['name'], $schema['engine'], $schema['charset'], $schema['collection'], $schema['comment']);
  50. $schemaCreate->setStructures($structures)
  51. ->setModule($schema['module'])
  52. ->setCreatedAt($schema['created_at'])
  53. ->setCreatorId($schema['creator_id'])
  54. ->setUpdatedAt($schema['updated_at'])
  55. ->setDeletedAt($schema['deleted_at'])
  56. ->create();
  57. } catch (Exception $e) {
  58. parent::deleteBy($schemaId, true);
  59. throw $e;
  60. }
  61. return true;
  62. }
  63. /**
  64. * @param $id
  65. * @return Model
  66. */
  67. public function show($id): Model
  68. {
  69. $schema = parent::firstBy($id);
  70. $columns = [];
  71. foreach (getTableColumns($schema->name) as $columnString) {
  72. $column = DB::connection()->getDoctrineColumn(DB::connection()->getTablePrefix().$schema->name, $columnString);
  73. $columns[] = [
  74. 'name' => $column->getName(),
  75. 'type' => $column->getType()->getName(),
  76. 'nullable' => ! $column->getNotnull(),
  77. 'default' => $column->getDefault(),
  78. 'comment' => $column->getComment()
  79. ];
  80. }
  81. $schema->columns = $columns;
  82. return $schema;
  83. }
  84. /**
  85. * delete
  86. *
  87. * @param $id
  88. * @param bool $force
  89. * @return bool|null
  90. */
  91. public function deleteBy($id, bool $force = false): ?bool
  92. {
  93. $schema = parent::firstBy($id);
  94. if ($schema->delete()) {
  95. SchemaFacade::dropIfExists($schema->name);
  96. }
  97. return true;
  98. }
  99. }