-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abc1db6
commit 04f1996
Showing
11 changed files
with
214 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
.../core/database/migrations/2023_12_01_000000_add_unique_position_constraints_to_tables.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Lunar\Base\Migration; | ||
use Lunar\Models\Attribute; | ||
use Lunar\Models\AttributeGroup; | ||
use Lunar\Models\ProductOption; | ||
use Lunar\Models\ProductOptionValue; | ||
|
||
return new class extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
/** | ||
* Model's with position | ||
*/ | ||
$models = [ | ||
Attribute::class, | ||
AttributeGroup::class, | ||
ProductOption::class, | ||
ProductOptionValue::class, | ||
]; | ||
/** | ||
* Ensure that all position values are unique | ||
*/ | ||
foreach ($models as $model) { | ||
$model = app($model); | ||
Schema::table($model->getTable(), function (Blueprint $table) use ($model) { | ||
DB::table($model->getTable()) | ||
->select(array_merge( | ||
[$model->getKeyName()], | ||
$model->positionUniqueConstraints() | ||
)) | ||
->orderBy('position') | ||
->orderBy('id') | ||
->get() | ||
->groupBy(fn (stdClass $row, int $key) => | ||
collect($row) | ||
->only($model->positionUniqueConstraints()) | ||
->except('position') | ||
->join('-') | ||
)->each | ||
->each(fn(stdClass $row, int $key) => $row->position = $key + 1); | ||
}); | ||
} | ||
/** | ||
* Add unique position index under consideration of | ||
* the model's position constraints | ||
*/ | ||
foreach ($models as $model) { | ||
$model = app($model); | ||
Schema::table($model->getTable(), function (Blueprint $table) use ($model) { | ||
$schema = Schema::getConnection() | ||
->getDoctrineSchemaManager() | ||
->introspectTable($model->getTable()); | ||
$uniqueIndex = $this->prefix . $table->getTable() . '_unique_position'; | ||
$uniqueConstraints = array_merge($model->positionUniqueConstraints(), ['position']); | ||
$table->unsignedBigInteger('position')->default(null)->index()->change(); | ||
if (!$schema->hasIndex($uniqueIndex)) { | ||
$table->unique($uniqueConstraints, $uniqueIndex); | ||
} | ||
}); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
namespace Lunar\Base\Traits; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Str; | ||
|
||
trait HasPosition | ||
{ | ||
|
||
final public static function initializeHasPosition(): void | ||
{ | ||
static::saving(function (Model $model) { | ||
if ( | ||
$model->isDirty($model->positionUniqueConstraints()) | ||
|| !(intval($model->position) > 0) | ||
|| $model->query() | ||
->where($model->getKeyName(), '!=', $model->getKey()) | ||
->wherePosition( | ||
$model->position, | ||
$model->getAttributes() | ||
) | ||
->exists() | ||
) { | ||
$model->position = $model->query() | ||
->where($model->getKeyName(), '!=', $model->getKey()) | ||
->wherePositionUniqueConstraints( | ||
$model->getAttributes() | ||
) | ||
->max('position') + 1; | ||
} | ||
}); | ||
} | ||
|
||
final public function positionUniqueConstraints(): array | ||
{ | ||
$constraints = ['position']; | ||
|
||
if (!property_exists($this, 'positionUniqueConstraints') | ||
|| !is_array($this->positionUniqueConstraints)) | ||
{ | ||
return $constraints; | ||
} | ||
|
||
return array_merge($this->positionUniqueConstraints, $constraints); | ||
} | ||
|
||
final public function scopeWherePosition(Builder $query, int $position, array|Collection $constraints = []): void | ||
{ | ||
$query | ||
->where('position', $position) | ||
->wherePositionUniqueConstraints($constraints); | ||
} | ||
|
||
final public function scopeWherePositionUniqueConstraints(Builder $query, array|Collection $constraints = []): void | ||
{ | ||
$constraints = collect($constraints)->except('position'); | ||
$modelConstraints = collect($this->positionUniqueConstraints())->reject('position'); | ||
|
||
if (count($modelConstraints) && !$constraints->hasAny($modelConstraints->toArray())) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'Position constraints "%s" for "%s" not defined!', | ||
$modelConstraints->diff($constraints)->join('", "', '" and "'), | ||
get_class($this) | ||
) | ||
); | ||
} | ||
|
||
$modelConstraints->each( | ||
function ($attribute) use ($query, $constraints) { | ||
if (method_exists($query, Str::camel('scope_' . $attribute))) { | ||
$method = Str::camel($attribute); | ||
} else { | ||
$method = Str::camel('where_' . $attribute); | ||
} | ||
$query->{$method}($constraints[$attribute]); | ||
} | ||
); | ||
} | ||
|
||
public function scopePosition(Builder $query, int $position, ...$constraints): void | ||
{ | ||
$query->wherePosition($position, $constraints); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters