Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] feat: configure default datetime precision on per-grammar basis #51821

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
116 changes: 67 additions & 49 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ class Blueprint
{
use Macroable;

/**
* The database connection instance.
*/
protected Connection $connection;

/**
* The schema grammar instance.
*/
protected Grammar $grammar;

/**
* The table the blueprint describes.
*
Expand Down Expand Up @@ -94,8 +104,10 @@ class Blueprint
* @param string $prefix
* @return void
*/
public function __construct($table, ?Closure $callback = null, $prefix = '')
public function __construct(Connection $connection, $table, ?Closure $callback = null, $prefix = '')
{
$this->connection = $connection;
$this->grammar = $connection->getSchemaGrammar();
$this->table = $table;
$this->prefix = $prefix;

Expand All @@ -107,34 +119,30 @@ public function __construct($table, ?Closure $callback = null, $prefix = '')
/**
* Execute the blueprint against the database.
*
* @param \Illuminate\Database\Connection $connection
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
* @return void
*/
public function build(Connection $connection, Grammar $grammar)
public function build()
{
foreach ($this->toSql($connection, $grammar) as $statement) {
$connection->statement($statement);
foreach ($this->toSql() as $statement) {
$this->connection->statement($statement);
}
}

/**
* Get the raw SQL statements for the blueprint.
*
* @param \Illuminate\Database\Connection $connection
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
* @return array
*/
public function toSql(Connection $connection, Grammar $grammar)
public function toSql()
{
$this->addImpliedCommands($connection, $grammar);
$this->addImpliedCommands();

$statements = [];

// Each type of command has a corresponding compiler function on the schema
// grammar which is used to build the necessary SQL statements to build
// the blueprint element, so we'll just call that compilers function.
$this->ensureCommandsAreValid($connection);
$this->ensureCommandsAreValid();

foreach ($this->commands as $command) {
if ($command->shouldBeSkipped) {
Expand All @@ -143,12 +151,12 @@ public function toSql(Connection $connection, Grammar $grammar)

$method = 'compile'.ucfirst($command->name);

if (method_exists($grammar, $method) || $grammar::hasMacro($method)) {
if (method_exists($this->grammar, $method) || $this->grammar::hasMacro($method)) {
if ($this->hasState()) {
$this->state->update($command);
}

if (! is_null($sql = $grammar->$method($this, $command, $connection))) {
if (! is_null($sql = $this->grammar->$method($this, $command, $this->connection))) {
$statements = array_merge($statements, (array) $sql);
}
}
Expand All @@ -160,12 +168,11 @@ public function toSql(Connection $connection, Grammar $grammar)
/**
* Ensure the commands on the blueprint are valid for the connection type.
*
* @param \Illuminate\Database\Connection $connection
* @return void
*
* @throws \BadMethodCallException
*/
protected function ensureCommandsAreValid(Connection $connection)
protected function ensureCommandsAreValid()
{
//
}
Expand All @@ -188,15 +195,12 @@ protected function commandsNamed(array $names)
/**
* Add the commands that are implied by the blueprint's state.
*
* @param \Illuminate\Database\Connection $connection
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
* @return void
*/
protected function addImpliedCommands(Connection $connection, Grammar $grammar)
protected function addImpliedCommands()
{
$this->addFluentIndexes($connection, $grammar);

$this->addFluentCommands($connection, $grammar);
$this->addFluentIndexes();
$this->addFluentCommands();

if (! $this->creating()) {
$this->commands = array_map(
Expand All @@ -206,25 +210,23 @@ protected function addImpliedCommands(Connection $connection, Grammar $grammar)
$this->commands
);

$this->addAlterCommands($connection, $grammar);
$this->addAlterCommands();
}
}

/**
* Add the index commands fluently specified on columns.
*
* @param \Illuminate\Database\Connection $connection
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
* @return void
*/
protected function addFluentIndexes(Connection $connection, Grammar $grammar)
protected function addFluentIndexes()
{
foreach ($this->columns as $column) {
foreach (['primary', 'unique', 'index', 'fulltext', 'fullText', 'spatialIndex'] as $index) {
// If the column is supposed to be changed to an auto increment column and
// the specified index is primary, there is no need to add a command on
// MySQL, as it will be handled during the column definition instead.
if ($index === 'primary' && $column->autoIncrement && $column->change && $grammar instanceof MySqlGrammar) {
if ($index === 'primary' && $column->autoIncrement && $column->change && $this->grammar instanceof MySqlGrammar) {
continue 2;
}

Expand Down Expand Up @@ -264,14 +266,12 @@ protected function addFluentIndexes(Connection $connection, Grammar $grammar)
/**
* Add the fluent commands specified on any columns.
*
* @param \Illuminate\Database\Connection $connection
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
* @return void
*/
public function addFluentCommands(Connection $connection, Grammar $grammar)
public function addFluentCommands()
{
foreach ($this->columns as $column) {
foreach ($grammar->getFluentCommands() as $commandName) {
foreach ($this->grammar->getFluentCommands() as $commandName) {
$this->addCommand($commandName, compact('column'));
}
}
Expand All @@ -280,17 +280,15 @@ public function addFluentCommands(Connection $connection, Grammar $grammar)
/**
* Add the alter commands if whenever needed.
*
* @param \Illuminate\Database\Connection $connection
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
* @return void
*/
public function addAlterCommands(Connection $connection, Grammar $grammar)
public function addAlterCommands()
{
if (! $grammar instanceof SQLiteGrammar) {
if (! $this->grammar instanceof SQLiteGrammar) {
return;
}

$alterCommands = $grammar->getAlterCommands($connection);
$alterCommands = $this->grammar->getAlterCommands($this->connection);

[$commands, $lastCommandWasAlter, $hasAlterCommand] = [
[], false, false,
Expand All @@ -313,7 +311,7 @@ public function addAlterCommands(Connection $connection, Grammar $grammar)
}

if ($hasAlterCommand) {
$this->state = new BlueprintState($this, $connection, $grammar);
$this->state = new BlueprintState($this, $this->connection, $this->grammar);
}

$this->commands = $commands;
Expand Down Expand Up @@ -1162,8 +1160,10 @@ public function date($column)
* @param int|null $precision
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function dateTime($column, $precision = 0)
public function dateTime($column, $precision = null)
{
$precision ??= $this->defaultTimePrecision();

return $this->addColumn('dateTime', $column, compact('precision'));
}

Expand All @@ -1174,8 +1174,10 @@ public function dateTime($column, $precision = 0)
* @param int|null $precision
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function dateTimeTz($column, $precision = 0)
public function dateTimeTz($column, $precision = null)
{
$precision ??= $this->defaultTimePrecision();

return $this->addColumn('dateTimeTz', $column, compact('precision'));
}

Expand All @@ -1186,8 +1188,10 @@ public function dateTimeTz($column, $precision = 0)
* @param int|null $precision
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function time($column, $precision = 0)
public function time($column, $precision = null)
{
$precision ??= $this->defaultTimePrecision();

return $this->addColumn('time', $column, compact('precision'));
}

Expand All @@ -1198,8 +1202,10 @@ public function time($column, $precision = 0)
* @param int|null $precision
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function timeTz($column, $precision = 0)
public function timeTz($column, $precision = null)
{
$precision ??= $this->defaultTimePrecision();

return $this->addColumn('timeTz', $column, compact('precision'));
}

Expand All @@ -1210,8 +1216,10 @@ public function timeTz($column, $precision = 0)
* @param int|null $precision
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function timestamp($column, $precision = 0)
public function timestamp($column, $precision = null)
{
$precision ??= $this->defaultTimePrecision();

return $this->addColumn('timestamp', $column, compact('precision'));
}

Expand All @@ -1222,8 +1230,10 @@ public function timestamp($column, $precision = 0)
* @param int|null $precision
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function timestampTz($column, $precision = 0)
public function timestampTz($column, $precision = null)
{
$precision ??= $this->defaultTimePrecision();

return $this->addColumn('timestampTz', $column, compact('precision'));
}

Expand All @@ -1233,7 +1243,7 @@ public function timestampTz($column, $precision = 0)
* @param int|null $precision
* @return void
*/
public function timestamps($precision = 0)
public function timestamps($precision = null)
{
$this->timestamp('created_at', $precision)->nullable();

Expand All @@ -1248,7 +1258,7 @@ public function timestamps($precision = 0)
* @param int|null $precision
* @return void
*/
public function nullableTimestamps($precision = 0)
public function nullableTimestamps($precision = null)
{
$this->timestamps($precision);
}
Expand All @@ -1259,7 +1269,7 @@ public function nullableTimestamps($precision = 0)
* @param int|null $precision
* @return void
*/
public function timestampsTz($precision = 0)
public function timestampsTz($precision = null)
{
$this->timestampTz('created_at', $precision)->nullable();

Expand All @@ -1272,7 +1282,7 @@ public function timestampsTz($precision = 0)
* @param int|null $precision
* @return void
*/
public function datetimes($precision = 0)
public function datetimes($precision = null)
{
$this->datetime('created_at', $precision)->nullable();

Expand All @@ -1286,7 +1296,7 @@ public function datetimes($precision = 0)
* @param int|null $precision
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function softDeletes($column = 'deleted_at', $precision = 0)
public function softDeletes($column = 'deleted_at', $precision = null)
{
return $this->timestamp($column, $precision)->nullable();
}
Expand All @@ -1298,7 +1308,7 @@ public function softDeletes($column = 'deleted_at', $precision = 0)
* @param int|null $precision
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function softDeletesTz($column = 'deleted_at', $precision = 0)
public function softDeletesTz($column = 'deleted_at', $precision = null)
{
return $this->timestampTz($column, $precision)->nullable();
}
Expand All @@ -1310,7 +1320,7 @@ public function softDeletesTz($column = 'deleted_at', $precision = 0)
* @param int|null $precision
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function softDeletesDatetime($column = 'deleted_at', $precision = 0)
public function softDeletesDatetime($column = 'deleted_at', $precision = null)
{
return $this->datetime($column, $precision)->nullable();
}
Expand Down Expand Up @@ -1853,4 +1863,12 @@ public function getChangedColumns()
return (bool) $column->change;
});
}

/**
* Get the default time precision.
*/
protected function defaultTimePrecision(): ?int
{
return $this->connection->getSchemaBuilder()::$defaultTimePrecision;
}
}
25 changes: 19 additions & 6 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class Builder
*/
public static $defaultStringLength = 255;

/**
* The default time precision for migrations.
*/
public static ?int $defaultTimePrecision = 0;

/**
* The default relationship morph key type.
*
Expand Down Expand Up @@ -71,6 +76,14 @@ public static function defaultStringLength($length)
static::$defaultStringLength = $length;
}

/**
* Set the default time precision for migrations.
*/
public static function defaultTimePrecision(?int $precision): void
{
static::$defaultTimePrecision = $precision;
}

/**
* Set the default morph key type for migrations.
*
Expand Down Expand Up @@ -561,7 +574,7 @@ public function withoutForeignKeyConstraints(Closure $callback)
*/
protected function build(Blueprint $blueprint)
{
$blueprint->build($this->connection, $this->grammar);
$blueprint->build();
}

/**
Expand All @@ -573,15 +586,15 @@ protected function build(Blueprint $blueprint)
*/
protected function createBlueprint($table, ?Closure $callback = null)
{
$prefix = $this->connection->getConfig('prefix_indexes')
? $this->connection->getConfig('prefix')
: '';
$connection = $this->connection;

$prefix = $connection->getConfig('prefix_indexes') ? $connection->getConfig('prefix') : '';

if (isset($this->resolver)) {
return call_user_func($this->resolver, $table, $callback, $prefix);
return call_user_func($this->resolver, $connection, $table, $callback, $prefix);
}

return Container::getInstance()->make(Blueprint::class, compact('table', 'callback', 'prefix'));
return Container::getInstance()->make(Blueprint::class, compact('connection', 'table', 'callback', 'prefix'));
}

/**
Expand Down
Loading