1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TobyMaxham\Helper\Database; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Schema\Blueprint; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class MigrationHelper. |
9
|
|
|
* |
10
|
|
|
* @author Tobias Maxham <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class MigrationHelper |
13
|
|
|
{ |
14
|
|
|
const REFERENTIAL_ACTION_CASCADE = 'CASCADE'; |
15
|
|
|
const REFERENTIAL_ACTION_RESTRICT = 'RESTRICT'; |
16
|
|
|
const REFERENTIAL_ACTION_SET_NULL = 'SET NULL'; |
17
|
|
|
const REFERENTIAL_ACTION_NO_ACTION = 'NO ACTION'; |
18
|
|
|
const REFERENTIAL_ACTION_SET_DEFAULT = 'SET DEFAULT'; |
19
|
|
|
|
20
|
|
|
public static function uuid(Blueprint $table, string $comment = null) |
21
|
|
|
{ |
22
|
|
|
$table->char('uuid', 36); |
23
|
|
|
/** @var \Illuminate\Support\Fluent $fluent */ |
24
|
|
|
$fluent = $table->unique('uuid', 'uuid'); |
25
|
|
|
if (! is_null($comment)) { |
26
|
|
|
$fluent->comment = $comment; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function addSoftDelete(Blueprint $table) |
31
|
|
|
{ |
32
|
|
|
$table->softDeletes(); |
33
|
|
|
$table->index(['deleted_at'], 'deleted_at'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function dropSoftDelete(Blueprint $table) |
37
|
|
|
{ |
38
|
|
|
$table->dropIndex('deleted_at'); |
39
|
|
|
$table->dropSoftDeletes(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function addChangedByUserFields(Blueprint $table, array $fields = ['created_by', 'updated_by', 'deleted_by'], string $relation = 'users', string $foreign = 'id', string $onDelete = self::REFERENTIAL_ACTION_CASCADE) |
43
|
|
|
{ |
44
|
|
|
if (! isset($fields[0]) || ! is_string($fields[0])) { |
45
|
|
|
$fields[0] = 'created_by'; |
46
|
|
|
} |
47
|
|
|
if (! isset($fields[1]) || ! is_string($fields[1])) { |
48
|
|
|
$fields[1] = 'updated_by'; |
49
|
|
|
} |
50
|
|
|
if (! isset($fields[2]) || ! is_string($fields[2])) { |
51
|
|
|
$fields[2] = 'deleted_by'; |
52
|
|
|
} |
53
|
|
|
self::addCreatedByUser($table, $fields[0], $relation, $foreign, $onDelete); |
54
|
|
|
self::addUpdatedByUser($table, $fields[1], $relation, $foreign, $onDelete); |
55
|
|
|
self::addDeletedByUser($table, $fields[2], $relation, $foreign, $onDelete); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function addCreatedByUser(Blueprint $table, string $field = 'created_by', string $relation = 'users', string $foreign = 'id', string $onDelete = self::REFERENTIAL_ACTION_CASCADE) |
59
|
|
|
{ |
60
|
|
|
self::addChangedByField($table, $field, $relation, $foreign, $onDelete); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function addUpdatedByUser(Blueprint $table, string $field = 'updated_by', string $relation = 'users', string $foreign = 'id', string $onDelete = self::REFERENTIAL_ACTION_CASCADE) |
64
|
|
|
{ |
65
|
|
|
self::addChangedByField($table, $field, $relation, $foreign, $onDelete); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function addDeletedByUser(Blueprint $table, string $field = 'deleted_by', string $relation = 'users', string $foreign = 'id', string $onDelete = self::REFERENTIAL_ACTION_CASCADE) |
69
|
|
|
{ |
70
|
|
|
self::addChangedByField($table, $field, $relation, $foreign, $onDelete); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function addChangedByField(Blueprint $table, string $field, string $relation, string $foreign, string $onDelete) |
74
|
|
|
{ |
75
|
|
|
$table->unsignedInteger($field)->nullable(); |
76
|
|
|
$table->foreign($field) |
77
|
|
|
->references($foreign)->on($relation) |
78
|
|
|
->onDelete($onDelete); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|