From 007fef4be1259dc976a6a3472e02e39c2e67d7e1 Mon Sep 17 00:00:00 2001 From: Boris Glumpler Date: Tue, 29 Oct 2024 19:35:03 +0530 Subject: [PATCH 1/6] Allow for custom Postgres operators to be added (#53324) * Allow for custom Postgres operators to be added * formatting --------- Co-authored-by: Taylor Otwell --- Query/Grammars/PostgresGrammar.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Query/Grammars/PostgresGrammar.php b/Query/Grammars/PostgresGrammar.php index 232c824d1..4fa643bcc 100755 --- a/Query/Grammars/PostgresGrammar.php +++ b/Query/Grammars/PostgresGrammar.php @@ -22,6 +22,13 @@ class PostgresGrammar extends Grammar 'is distinct from', 'is not distinct from', ]; + /** + * The Postgres grammar specific custom operators. + * + * @var array + */ + protected static $customOperators = []; + /** * The grammar specific bitwise operators. * @@ -772,4 +779,27 @@ public function substituteBindingsIntoRawSql($sql, $bindings) return $query; } + + /** + * Get the Postgres grammar specific operators. + * + * @return array + */ + public function getOperators() + { + return array_values(array_unique(array_merge(parent::getOperators(), static::$customOperators))); + } + + /** + * Set any Postgres grammar specific custom operators. + * + * @param array $operators + * @return void + */ + public static function customOperators(array $operators) + { + static::$customOperators = array_values( + array_merge(static::$customOperators, array_filter(array_filter($operators, 'is_string'))) + ); + } } From 243be08a85fc3294bd5572be19bfe85df69ad42c Mon Sep 17 00:00:00 2001 From: AkrAm Date: Tue, 29 Oct 2024 20:29:44 +0600 Subject: [PATCH 2/6] [11.x] Support Optional Dimensions for `vector` Column Type (#53316) * Allow `vector` column dimensions to be unspecified * Update Blueprint.php --------- Co-authored-by: Taylor Otwell --- Schema/Blueprint.php | 8 +++++--- Schema/Grammars/MySqlGrammar.php | 4 +++- Schema/Grammars/PostgresGrammar.php | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Schema/Blueprint.php b/Schema/Blueprint.php index 28dc472cf..0d34270ce 100755 --- a/Schema/Blueprint.php +++ b/Schema/Blueprint.php @@ -1456,12 +1456,14 @@ public function computed($column, $expression) * Create a new vector column on the table. * * @param string $column - * @param int $dimensions + * @param int|null $dimensions * @return \Illuminate\Database\Schema\ColumnDefinition */ - public function vector($column, $dimensions) + public function vector($column, $dimensions = null) { - return $this->addColumn('vector', $column, compact('dimensions')); + $options = $dimensions ? compact('dimensions') : []; + + return $this->addColumn('vector', $column, $options); } /** diff --git a/Schema/Grammars/MySqlGrammar.php b/Schema/Grammars/MySqlGrammar.php index 667a06ebe..0bd6a4b22 100755 --- a/Schema/Grammars/MySqlGrammar.php +++ b/Schema/Grammars/MySqlGrammar.php @@ -1128,7 +1128,9 @@ protected function typeComputed(Fluent $column) */ protected function typeVector(Fluent $column) { - return "vector($column->dimensions)"; + return isset($column->dimensions) && $column->dimensions !== '' + ? "vector({$column->dimensions})" + : 'vector'; } /** diff --git a/Schema/Grammars/PostgresGrammar.php b/Schema/Grammars/PostgresGrammar.php index 85b57df62..6ec9221ff 100755 --- a/Schema/Grammars/PostgresGrammar.php +++ b/Schema/Grammars/PostgresGrammar.php @@ -1078,7 +1078,9 @@ protected function typeGeography(Fluent $column) */ protected function typeVector(Fluent $column) { - return "vector($column->dimensions)"; + return isset($column->dimensions) && $column->dimensions !== '' + ? "vector({$column->dimensions})" + : 'vector'; } /** From 8319f800b009a41bdc81a5bddebce550fe53cfb1 Mon Sep 17 00:00:00 2001 From: Lito Date: Tue, 29 Oct 2024 15:59:01 +0100 Subject: [PATCH 3/6] [11.x] Added `dropColumnsIfExists`, `dropColumnIfExists` and `dropForeignIfExists` (#53305) * Added dropColumnsIfExists and dropColumnIfExists * Update src/Illuminate/Database/Schema/Blueprint.php Thanks! Co-authored-by: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com> * Simplified dropColumnIfExists * Added `dropForeignIfExists` to complement `dropColumnIfExists` * Updated dropForeignIfExists comment * Update Blueprint.php * Update Blueprint.php * Update Blueprint.php * Update Builder.php --------- Co-authored-by: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com> Co-authored-by: Taylor Otwell --- Schema/Blueprint.php | 39 +++++++++++++++++++++++++++++++++++++++ Schema/Builder.php | 14 ++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/Schema/Blueprint.php b/Schema/Blueprint.php index 0d34270ce..1b8f3e931 100755 --- a/Schema/Blueprint.php +++ b/Schema/Blueprint.php @@ -9,6 +9,7 @@ use Illuminate\Database\Schema\Grammars\Grammar; use Illuminate\Database\Schema\Grammars\MySqlGrammar; use Illuminate\Database\Schema\Grammars\SQLiteGrammar; +use Illuminate\Support\Facades\Schema; use Illuminate\Support\Fluent; use Illuminate\Support\Traits\Macroable; @@ -427,6 +428,25 @@ public function dropColumn($columns) return $this->addCommand('dropColumn', compact('columns')); } + /** + * Indicate that the given columns should be dropped if they exist. + * + * @param array|mixed $columns + * @return \Illuminate\Support\Fluent + */ + public function dropColumnIfExists($columns) + { + $columns = is_array($columns) ? $columns : func_get_args(); + + $columns = array_intersect($columns, Schema::getColumnListing($this->getTable())); + + if (empty($columns)) { + return new Fluent; + } + + return $this->dropColumn($columns); + } + /** * Indicate that the given columns should be renamed. * @@ -505,6 +525,25 @@ public function dropForeign($index) return $this->dropIndexCommand('dropForeign', 'foreign', $index); } + /** + * Indicate that the given foreign key should be dropped if it exists. + * + * @param string|array $index + * @return \Illuminate\Support\Fluent + */ + public function dropForeignIfExists($index) + { + if (is_array($index)) { + $index = $this->createIndexName('foreign', $index); + } + + if (! in_array($index, Schema::getForeignKeys($this->getTable()))) { + return new Fluent; + } + + return $this->dropIndexCommand('dropForeign', 'foreign', $index); + } + /** * Indicate that the given column and foreign key should be dropped. * diff --git a/Schema/Builder.php b/Schema/Builder.php index 9af11e2e0..8c14a47ee 100755 --- a/Schema/Builder.php +++ b/Schema/Builder.php @@ -462,6 +462,20 @@ public function dropColumns($table, $columns) }); } + /** + * Drop columns from a table schema if they exist. + * + * @param string $table + * @param string|array $columns + * @return void + */ + public function dropColumnsIfExists($table, $columns) + { + $this->table($table, function (Blueprint $blueprint) use ($columns) { + $blueprint->dropColumnIfExists($columns); + }); + } + /** * Drop all tables from the database. * From 319d055785f956fc1054004b9f4f04141e47cb15 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 29 Oct 2024 15:19:03 -0500 Subject: [PATCH 4/6] =?UTF-8?q?Revert=20"[11.x]=20Added=20`dropColumnsIfEx?= =?UTF-8?q?ists`,=20`dropColumnIfExists`=20and=20`dropFor=E2=80=A6"=20(#53?= =?UTF-8?q?338)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit b6e478d5391b1e76257ee21230cf532d5b06ba47. --- Schema/Blueprint.php | 39 --------------------------------------- Schema/Builder.php | 14 -------------- 2 files changed, 53 deletions(-) diff --git a/Schema/Blueprint.php b/Schema/Blueprint.php index 1b8f3e931..0d34270ce 100755 --- a/Schema/Blueprint.php +++ b/Schema/Blueprint.php @@ -9,7 +9,6 @@ use Illuminate\Database\Schema\Grammars\Grammar; use Illuminate\Database\Schema\Grammars\MySqlGrammar; use Illuminate\Database\Schema\Grammars\SQLiteGrammar; -use Illuminate\Support\Facades\Schema; use Illuminate\Support\Fluent; use Illuminate\Support\Traits\Macroable; @@ -428,25 +427,6 @@ public function dropColumn($columns) return $this->addCommand('dropColumn', compact('columns')); } - /** - * Indicate that the given columns should be dropped if they exist. - * - * @param array|mixed $columns - * @return \Illuminate\Support\Fluent - */ - public function dropColumnIfExists($columns) - { - $columns = is_array($columns) ? $columns : func_get_args(); - - $columns = array_intersect($columns, Schema::getColumnListing($this->getTable())); - - if (empty($columns)) { - return new Fluent; - } - - return $this->dropColumn($columns); - } - /** * Indicate that the given columns should be renamed. * @@ -525,25 +505,6 @@ public function dropForeign($index) return $this->dropIndexCommand('dropForeign', 'foreign', $index); } - /** - * Indicate that the given foreign key should be dropped if it exists. - * - * @param string|array $index - * @return \Illuminate\Support\Fluent - */ - public function dropForeignIfExists($index) - { - if (is_array($index)) { - $index = $this->createIndexName('foreign', $index); - } - - if (! in_array($index, Schema::getForeignKeys($this->getTable()))) { - return new Fluent; - } - - return $this->dropIndexCommand('dropForeign', 'foreign', $index); - } - /** * Indicate that the given column and foreign key should be dropped. * diff --git a/Schema/Builder.php b/Schema/Builder.php index 8c14a47ee..9af11e2e0 100755 --- a/Schema/Builder.php +++ b/Schema/Builder.php @@ -462,20 +462,6 @@ public function dropColumns($table, $columns) }); } - /** - * Drop columns from a table schema if they exist. - * - * @param string $table - * @param string|array $columns - * @return void - */ - public function dropColumnsIfExists($table, $columns) - { - $this->table($table, function (Blueprint $blueprint) use ($columns) { - $blueprint->dropColumnIfExists($columns); - }); - } - /** * Drop all tables from the database. * From 1a580881138b4f4688e5992bd06515ac7cf13ca4 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:30:03 -0400 Subject: [PATCH 5/6] [11.x] Introduce `HasUniqueStringIds` (#53280) * introduce HasUniqueStringIds * formatting --------- Co-authored-by: Taylor Otwell --- Eloquent/Concerns/HasUlids.php | 68 ++--------------- Eloquent/Concerns/HasUniqueStringIds.php | 97 ++++++++++++++++++++++++ Eloquent/Concerns/HasUuids.php | 68 ++--------------- 3 files changed, 107 insertions(+), 126 deletions(-) create mode 100644 Eloquent/Concerns/HasUniqueStringIds.php diff --git a/Eloquent/Concerns/HasUlids.php b/Eloquent/Concerns/HasUlids.php index 85a810db5..7b9144209 100644 --- a/Eloquent/Concerns/HasUlids.php +++ b/Eloquent/Concerns/HasUlids.php @@ -7,28 +7,10 @@ trait HasUlids { - /** - * Initialize the trait. - * - * @return void - */ - public function initializeHasUlids() - { - $this->usesUniqueIds = true; - } + use HasUniqueStringIds; /** - * Get the columns that should receive a unique identifier. - * - * @return array - */ - public function uniqueIds() - { - return [$this->getKeyName()]; - } - - /** - * Generate a new ULID for the model. + * Generate a new unique key for the model. * * @return string */ @@ -38,53 +20,13 @@ public function newUniqueId() } /** - * Retrieve the model for a bound value. + * Determine if given key is valid. * - * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $query * @param mixed $value - * @param string|null $field - * @return \Illuminate\Contracts\Database\Eloquent\Builder - * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException - */ - public function resolveRouteBindingQuery($query, $value, $field = null) - { - if ($field && in_array($field, $this->uniqueIds()) && ! Str::isUlid($value)) { - throw (new ModelNotFoundException)->setModel(get_class($this), $value); - } - - if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! Str::isUlid($value)) { - throw (new ModelNotFoundException)->setModel(get_class($this), $value); - } - - return parent::resolveRouteBindingQuery($query, $value, $field); - } - - /** - * Get the auto-incrementing key type. - * - * @return string - */ - public function getKeyType() - { - if (in_array($this->getKeyName(), $this->uniqueIds())) { - return 'string'; - } - - return $this->keyType; - } - - /** - * Get the value indicating whether the IDs are incrementing. - * * @return bool */ - public function getIncrementing() + protected function isValidUniqueId($value): bool { - if (in_array($this->getKeyName(), $this->uniqueIds())) { - return false; - } - - return $this->incrementing; + return Str::isUlid($value); } } diff --git a/Eloquent/Concerns/HasUniqueStringIds.php b/Eloquent/Concerns/HasUniqueStringIds.php new file mode 100644 index 000000000..19728a41b --- /dev/null +++ b/Eloquent/Concerns/HasUniqueStringIds.php @@ -0,0 +1,97 @@ +usesUniqueIds = true; + } + + /** + * Get the columns that should receive a unique identifier. + * + * @return array + */ + public function uniqueIds() + { + return [$this->getKeyName()]; + } + + + /** + * Retrieve the model for a bound value. + * + * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $query + * @param mixed $value + * @param string|null $field + * @return \Illuminate\Contracts\Database\Eloquent\Builder + * + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + */ + public function resolveRouteBindingQuery($query, $value, $field = null) + { + if ($field && in_array($field, $this->uniqueIds()) && ! $this->isValidUniqueId($value)) { + throw (new ModelNotFoundException)->setModel(get_class($this), $value); + } + + if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! $this->isValidUniqueId($value)) { + throw (new ModelNotFoundException)->setModel(get_class($this), $value); + } + + return parent::resolveRouteBindingQuery($query, $value, $field); + } + + /** + * Get the auto-incrementing key type. + * + * @return string + */ + public function getKeyType() + { + if (in_array($this->getKeyName(), $this->uniqueIds())) { + return 'string'; + } + + return $this->keyType; + } + + /** + * Get the value indicating whether the IDs are incrementing. + * + * @return bool + */ + public function getIncrementing() + { + if (in_array($this->getKeyName(), $this->uniqueIds())) { + return false; + } + + return $this->incrementing; + } + +} diff --git a/Eloquent/Concerns/HasUuids.php b/Eloquent/Concerns/HasUuids.php index 55d1acfe7..dc1984054 100644 --- a/Eloquent/Concerns/HasUuids.php +++ b/Eloquent/Concerns/HasUuids.php @@ -7,28 +7,10 @@ trait HasUuids { - /** - * Initialize the trait. - * - * @return void - */ - public function initializeHasUuids() - { - $this->usesUniqueIds = true; - } + use HasUniqueStringIds; /** - * Get the columns that should receive a unique identifier. - * - * @return array - */ - public function uniqueIds() - { - return [$this->getKeyName()]; - } - - /** - * Generate a new UUID for the model. + * Generate a new unique key for the model. * * @return string */ @@ -38,53 +20,13 @@ public function newUniqueId() } /** - * Retrieve the model for a bound value. + * Determine if given key is valid. * - * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $query * @param mixed $value - * @param string|null $field - * @return \Illuminate\Contracts\Database\Eloquent\Builder - * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException - */ - public function resolveRouteBindingQuery($query, $value, $field = null) - { - if ($field && in_array($field, $this->uniqueIds()) && ! Str::isUuid($value)) { - throw (new ModelNotFoundException)->setModel(get_class($this), $value); - } - - if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! Str::isUuid($value)) { - throw (new ModelNotFoundException)->setModel(get_class($this), $value); - } - - return parent::resolveRouteBindingQuery($query, $value, $field); - } - - /** - * Get the auto-incrementing key type. - * - * @return string - */ - public function getKeyType() - { - if (in_array($this->getKeyName(), $this->uniqueIds())) { - return 'string'; - } - - return $this->keyType; - } - - /** - * Get the value indicating whether the IDs are incrementing. - * * @return bool */ - public function getIncrementing() + protected function isValidUniqueId($value): bool { - if (in_array($this->getKeyName(), $this->uniqueIds())) { - return false; - } - - return $this->incrementing; + return Str::isUuid($value); } } From 29500e97a251419a6e42aeebe4c07ccb174af5b3 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 29 Oct 2024 20:30:27 +0000 Subject: [PATCH 6/6] Apply fixes from StyleCI --- Eloquent/Concerns/HasUlids.php | 1 - Eloquent/Concerns/HasUniqueStringIds.php | 3 --- Eloquent/Concerns/HasUuids.php | 1 - 3 files changed, 5 deletions(-) diff --git a/Eloquent/Concerns/HasUlids.php b/Eloquent/Concerns/HasUlids.php index 7b9144209..344f97338 100644 --- a/Eloquent/Concerns/HasUlids.php +++ b/Eloquent/Concerns/HasUlids.php @@ -2,7 +2,6 @@ namespace Illuminate\Database\Eloquent\Concerns; -use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Str; trait HasUlids diff --git a/Eloquent/Concerns/HasUniqueStringIds.php b/Eloquent/Concerns/HasUniqueStringIds.php index 19728a41b..ae86c4304 100644 --- a/Eloquent/Concerns/HasUniqueStringIds.php +++ b/Eloquent/Concerns/HasUniqueStringIds.php @@ -3,7 +3,6 @@ namespace Illuminate\Database\Eloquent\Concerns; use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Support\Str; trait HasUniqueStringIds { @@ -42,7 +41,6 @@ public function uniqueIds() return [$this->getKeyName()]; } - /** * Retrieve the model for a bound value. * @@ -93,5 +91,4 @@ public function getIncrementing() return $this->incrementing; } - } diff --git a/Eloquent/Concerns/HasUuids.php b/Eloquent/Concerns/HasUuids.php index dc1984054..8d6c35ad8 100644 --- a/Eloquent/Concerns/HasUuids.php +++ b/Eloquent/Concerns/HasUuids.php @@ -2,7 +2,6 @@ namespace Illuminate\Database\Eloquent\Concerns; -use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Str; trait HasUuids