Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/Schema/BaseColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,38 @@ public function notNull(): self
return $this->set('nullable', false);
}

/**
* @return $this
*/
public function nullable(): self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Columns are nullable by default, this is redundant.

{
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different DBs use different values for COLLATE.

{
return $this->set('collate', $value);
}

/**
* @return $this
*/
public function onUpdateCurrentTimeStamp(): self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MySQL specific.
You can implement this in most DBs using a TRIGGER.

{
return $this->set('onUpdateCurrentTimeStamp', 1);
}

/**
Expand Down
32 changes: 30 additions & 2 deletions src/Schema/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -258,7 +258,7 @@ protected function handleTypeTime(BaseColumn $column): string
*/
protected function handleTypeTimestamp(BaseColumn $column): string
{
return 'TIMESTAMP';
return ' TIMESTAMP DEFAULT CURRENT_TIMESTAMP() ';
}

/**
Expand Down Expand Up @@ -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
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MySQL only.

return $column->get('comment') ? ' COMMENT \'' . $column->get('comment') . '\'' : '';
}

/**
* @param BaseColumn $column
* @return string
*/
protected function handleModifierOnUpdateCurrentTimeStamp(BaseColumn $column): string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MySQL only.

{
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
Expand Down Expand Up @@ -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 '
Expand Down
13 changes: 8 additions & 5 deletions src/Schema/CreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breaks backward compatibility!

{
$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;
}
}