diff --git a/src/Schema/BaseColumn.php b/src/Schema/BaseColumn.php index 0ae20f8..a490bf8 100644 --- a/src/Schema/BaseColumn.php +++ b/src/Schema/BaseColumn.php @@ -126,13 +126,38 @@ public function notNull(): self return $this->set('nullable', false); } + /** + * @return $this + */ + public function nullable(): self + { + return $this->set('nullable', true); + } + /** * @param string $comment * @return $this */ public function description(string $comment): self { - return $this->set('description', $comment); + return $this->set('comment', $comment); + } + + /** + * @param string $value + * @return $this + */ + public function collate(string $value): self + { + return $this->set('collate', $value); + } + + /** + * @return $this + */ + public function onUpdateCurrentTimeStamp(): self + { + return $this->set('onUpdateCurrentTimeStamp', 1); } /** diff --git a/src/Schema/Compiler.php b/src/Schema/Compiler.php index 1fd4182..81ac5dc 100644 --- a/src/Schema/Compiler.php +++ b/src/Schema/Compiler.php @@ -31,7 +31,7 @@ class Compiler protected $params = []; /** @var string[] */ - protected $modifiers = ['unsigned', 'nullable', 'default', 'autoincrement']; + protected $modifiers = ['unsigned', 'nullable', 'default', 'autoincrement', 'comment', 'onUpdateCurrentTimeStamp', 'collate']; /** @var string[] */ protected $serials = ['tiny', 'small', 'normal', 'medium', 'big']; @@ -258,7 +258,7 @@ protected function handleTypeTime(BaseColumn $column): string */ protected function handleTypeTimestamp(BaseColumn $column): string { - return 'TIMESTAMP'; + return ' TIMESTAMP DEFAULT CURRENT_TIMESTAMP() '; } /** @@ -310,6 +310,33 @@ protected function handleModifierDefault(BaseColumn $column): string return null === $column->get('default') ? '' : 'DEFAULT ' . $this->value($column->get('default')); } + /** + * @param BaseColumn $column + * @return string + */ + protected function handleModifierComment(BaseColumn $column): string + { + return $column->get('comment') ? ' COMMENT \'' . $column->get('comment') . '\'' : ''; + } + + /** + * @param BaseColumn $column + * @return string + */ + protected function handleModifierOnUpdateCurrentTimeStamp(BaseColumn $column): string + { + return $column->get('onUpdateCurrentTimeStamp') ? ' ON UPDATE CURRENT_TIMESTAMP() ' : ''; + } + + /** + * @param BaseColumn $column + * @return string + */ + protected function handleModifierCollate(BaseColumn $column): string + { + return $column->get('collate') ? ' COLLATE ' . $column->get('collate') : ''; + } + /** * @param BaseColumn $column * @return string @@ -552,6 +579,7 @@ protected function handleAddForeign(AlterTable $table, $data): string * @param $data * @return string */ + protected function handleSetDefaultValue(AlterTable $table, $data): string { return 'ALTER TABLE ' . $this->wrap($table->getTableName()) . ' ALTER COLUMN ' diff --git a/src/Schema/CreateTable.php b/src/Schema/CreateTable.php index b160c4b..95aca85 100644 --- a/src/Schema/CreateTable.php +++ b/src/Schema/CreateTable.php @@ -366,14 +366,17 @@ public function softDelete(string $column = 'deleted_at'): self } /** - * @param string $createColumn - * @param string $updateColumn * @return $this */ - public function timestamps(string $createColumn = 'created_at', string $updateColumn = 'updated_at'): self + public function timestamps($fields = []): self { - $this->dateTime($createColumn)->notNull(); - $this->dateTime($updateColumn); + if (!is_array($fields)) + $fields = [$fields]; + foreach ($fields as $field) + $this->timestamp($field); + + $this->timestamp('created_at'); + $this->timestamp('updated_at'); return $this; } }