This commit is contained in:
111
api/database/utils/MigrationUtil.php
Normal file
111
api/database/utils/MigrationUtil.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
class MigrationUtil
|
||||
{
|
||||
public static function idWithoutIncrements($table)
|
||||
{
|
||||
$table->unsignedBigInteger('id');
|
||||
|
||||
$table->primary('id');
|
||||
}
|
||||
|
||||
public static function addCommonFields($table, array $params = [])
|
||||
{
|
||||
$table->timestamps();
|
||||
|
||||
self::treatParams($params);
|
||||
|
||||
if (!$params['staticData']) {
|
||||
|
||||
self::addDefaultFields($table, $params['foreignNames']);
|
||||
}
|
||||
}
|
||||
|
||||
public static function addCommonFieldsWithSoftDeletes($table, array $params = [])
|
||||
{
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
|
||||
self::treatParams($params);
|
||||
|
||||
if (!$params['staticData']) {
|
||||
|
||||
self::addDefaultFields($table, $params['foreignNames']);
|
||||
|
||||
// Field: deleted_lotacao_id.
|
||||
|
||||
$table->unsignedBigInteger('deleted_lotacao_id')->nullable();
|
||||
|
||||
$table->foreign('deleted_lotacao_id', $params['foreignNames']['deleted_lotacao_id'] ?? null)
|
||||
->references('id')->on('cadastro_lotacao');
|
||||
|
||||
// Field: deleted_centro_custo_id.
|
||||
|
||||
$table->unsignedBigInteger('deleted_centro_custo_id')->nullable();
|
||||
|
||||
$table->foreign('deleted_centro_custo_id', $params['foreignNames']['deleted_centro_custo_id'] ?? null)
|
||||
->references('id')->on('cadastro_centro_custo');
|
||||
}
|
||||
}
|
||||
|
||||
private static function treatParams(&$params)
|
||||
{
|
||||
if (!isset($params['staticData'])) {
|
||||
|
||||
$params['staticData'] = false;
|
||||
}
|
||||
|
||||
if (!isset($params['foreignNames'])) {
|
||||
|
||||
$params['foreignNames'] = [];
|
||||
}
|
||||
}
|
||||
|
||||
private static function addDefaultFields($table, array $foreignNames)
|
||||
{
|
||||
// Field: created_lotacao_id.
|
||||
|
||||
$table->unsignedBigInteger('created_lotacao_id')->nullable();
|
||||
|
||||
$table->foreign('created_lotacao_id', $foreignNames['created_lotacao_id'] ?? null)
|
||||
->references('id')->on('cadastro_lotacao');
|
||||
|
||||
// Field: created_centro_custo_id.
|
||||
|
||||
$table->unsignedBigInteger('created_centro_custo_id')->nullable();
|
||||
|
||||
$table->foreign('created_centro_custo_id', $foreignNames['created_centro_custo_id'] ?? null)
|
||||
->references('id')->on('cadastro_centro_custo');
|
||||
|
||||
// Field: updated_lotacao_id.
|
||||
|
||||
$table->unsignedBigInteger('updated_lotacao_id')->nullable();
|
||||
|
||||
$table->foreign('updated_lotacao_id', $foreignNames['updated_lotacao_id'] ?? null)
|
||||
->references('id')->on('cadastro_lotacao');
|
||||
|
||||
// Field: updated_centro_custo_id.
|
||||
|
||||
$table->unsignedBigInteger('updated_centro_custo_id')->nullable();
|
||||
|
||||
$table->foreign('updated_centro_custo_id', $foreignNames['updated_centro_custo_id'] ?? null)
|
||||
->references('id')->on('cadastro_centro_custo');
|
||||
}
|
||||
|
||||
public static function dropColumnWithForeign($table, $field)
|
||||
{
|
||||
if (is_string($field)) {
|
||||
|
||||
$field = [$field];
|
||||
}
|
||||
|
||||
$table->dropForeign($field);
|
||||
$table->dropColumn($field);
|
||||
}
|
||||
|
||||
public static function addExternalFields($table)
|
||||
{
|
||||
$table->foreignId('externo_created_usuario_id')->nullable()->constrained('web_service_usuario');
|
||||
$table->foreignId('externo_updated_usuario_id')->nullable()->constrained('web_service_usuario');
|
||||
}
|
||||
}
|
||||
43
api/database/utils/SeederUtil.php
Normal file
43
api/database/utils/SeederUtil.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
class SeederUtil
|
||||
{
|
||||
public static function saveList($model, $items, $identifierKeys = [], $deleteIfNotPresent = false)
|
||||
{
|
||||
if (is_string($identifierKeys)) {
|
||||
|
||||
$identifierKeys = [$identifierKeys];
|
||||
}
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach ($items as $item) {
|
||||
|
||||
$identifiers = [];
|
||||
|
||||
foreach ($identifierKeys as $key) {
|
||||
|
||||
$identifiers[$key] = $item[$key];
|
||||
}
|
||||
|
||||
if (empty($identifiers)) {
|
||||
|
||||
$ids[] = $model::create($item)->id;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$ids[] = $model::updateOrCreate($identifiers, $item)->id;
|
||||
}
|
||||
|
||||
if ($deleteIfNotPresent && !empty($ids)) {
|
||||
|
||||
$idsToDelete = $model::whereNotIn('id', $ids)->pluck('id');
|
||||
|
||||
if (!empty($idsToDelete)) {
|
||||
|
||||
$model::destroy($idsToDelete);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user