Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 29, 2024
2 parents ee0b274 + 29500e9 commit 586cf8c
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 133 deletions.
69 changes: 5 additions & 64 deletions Eloquent/Concerns/HasUlids.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,14 @@

namespace Illuminate\Database\Eloquent\Concerns;

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Str;

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
*/
Expand All @@ -38,53 +19,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);
}
}
94 changes: 94 additions & 0 deletions Eloquent/Concerns/HasUniqueStringIds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Illuminate\Database\Eloquent\Concerns;

use Illuminate\Database\Eloquent\ModelNotFoundException;

trait HasUniqueStringIds
{
/**
* Generate a new unique key for the model.
*
* @return mixed
*/
abstract public function newUniqueId();

/**
* Determine if given key is valid.
*
* @param mixed $value
* @return bool
*/
abstract protected function isValidUniqueId($value): bool;

/**
* Initialize the trait.
*
* @return void
*/
public function initializeHasUniqueStringIds()
{
$this->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;
}
}
69 changes: 5 additions & 64 deletions Eloquent/Concerns/HasUuids.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,14 @@

namespace Illuminate\Database\Eloquent\Concerns;

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Str;

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
*/
Expand All @@ -38,53 +19,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);
}
}
30 changes: 30 additions & 0 deletions Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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')))
);
}
}
8 changes: 5 additions & 3 deletions Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -1467,12 +1467,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);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down
4 changes: 3 additions & 1 deletion Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down

0 comments on commit 586cf8c

Please sign in to comment.