9
9
use Vcian \LaravelDBAuditor \Services \AuditService ;
10
10
11
11
use function Termwind \{render };
12
+ use function Termwind \{renderUsing };
12
13
13
14
class DBConstrainCommand extends Command
14
15
{
@@ -17,7 +18,7 @@ class DBConstrainCommand extends Command
17
18
*
18
19
* @var string
19
20
*/
20
- protected $ signature = 'db:constrain {table?} ' ;
21
+ protected $ signature = 'db:constraint {table?} ' ;
21
22
22
23
/**
23
24
* The console command description.
@@ -32,64 +33,100 @@ class DBConstrainCommand extends Command
32
33
public function handle (AuditService $ auditService )
33
34
{
34
35
try {
35
- $ tableName = $ this ->argument ('table ' );
36
36
37
- $ results = Constant:: ARRAY_DECLARATION ;
38
-
39
- $ referenceTable = null ;
40
- $ referenceField = null ;
37
+ $ tableName = $ this -> components -> choice (
38
+ ' Which table whould you like to audit? ' ,
39
+ $ auditService -> getTablesList ()
40
+ ) ;
41
41
42
- $ constrains = [
43
- Constant::CONSTRAIN_PRIMARY_KEY ,
44
- Constant::CONSTRAIN_INDEX_KEY ,
45
- Constant::CONSTRAIN_UNIQUE_KEY ,
46
- Constant::CONSTRAIN_FOREIGN_KEY ,
47
- Constant::CONSTRAIN_ALL_KEY
42
+ $ data = [
43
+ "table " => $ tableName ,
44
+ "size " => $ auditService ->getTableSize ($ tableName ),
45
+ "fields " => $ auditService ->getTableFields ($ tableName ),
46
+ 'field_count ' => count ($ auditService ->getTableFields ($ tableName )),
47
+ 'constrain ' => [
48
+ 'primary ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_PRIMARY_KEY ),
49
+ 'unique ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_UNIQUE_KEY ),
50
+ 'foreign ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_FOREIGN_KEY ),
51
+ 'index ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_INDEX_KEY )
52
+ ]
48
53
];
49
54
50
- $ userInput = $ this ->choice (
51
- 'Please Select ' ,
52
- $ constrains
53
- );
55
+ render (view ('DBAuditor::constraint ' , ['data ' => $ data ]));
54
56
55
57
if ($ tableName ) {
56
- $ results = $ auditService ->checkConstrain ($ tableName , $ userInput );
57
- if (!$ results ) {
58
- return render ('<div class="w-100 px-1 p-1 bg-red-600 text-center">No Table Found</div> ' );
59
- }
60
- render (view ('DBAuditor::constraint ' , ['tables ' => $ results ]));
61
-
62
- $ checkUserHasFields = $ auditService ->getFieldByUserInput ($ tableName , $ userInput );
63
- if ($ checkUserHasFields ) {
64
- $ userSelection = $ this ->choice ('Do you want to add constrain into your table? ' , ['Yes ' , 'No ' ]);
65
- if ($ userSelection == "Yes " ) {
66
- if ($ userInput === Constant::CONSTRAIN_ALL_KEY ) {
67
- $ options = [Constant::CONSTRAIN_INDEX_KEY , Constant::CONSTRAIN_UNIQUE_KEY , Constant::CONSTRAIN_FOREIGN_KEY ];
68
- if (!$ auditService ->checkTableHasPrimaryKey ($ tableName )) {
69
- array_push ($ options , Constant::CONSTRAIN_PRIMARY_KEY );
58
+
59
+ $ continue = Constant::STATUS_TRUE ;
60
+ do {
61
+
62
+ $ noConstrainfields = $ auditService ->getNoConstrainFields ($ tableName );
63
+ $ constrainList = $ auditService ->getConstrainList ($ tableName , $ noConstrainfields );
64
+
65
+ if ($ noConstrainfields ) {
66
+
67
+ $ userInput = $ this ->confirm ("Do you want add more constrain? " );
68
+
69
+ if ($ userInput ) {
70
+
71
+ $ selectConstrain = $ this ->choice (
72
+ 'Please select constrain which you want to add ' ,
73
+ $ constrainList
74
+ );
75
+
76
+ if ($ selectConstrain === Constant::CONSTRAIN_PRIMARY_KEY || $ selectConstrain === Constant::CONSTRAIN_FOREIGN_KEY ) {
77
+ $ fields = $ noConstrainfields ['integer ' ];
78
+ } else {
79
+ $ fields = $ noConstrainfields ['mix ' ];
80
+ }
81
+
82
+ $ selectField = $ this ->choice (
83
+ 'Please select field where you want to add ' . $ selectConstrain ,
84
+ $ fields
85
+ );
86
+
87
+ if ($ selectConstrain === Constant::CONSTRAIN_FOREIGN_KEY ) {
88
+ $ tableHasValue = $ auditService ->tableHasValue ($ tableName );
89
+
90
+ if ($ tableHasValue ) {
91
+ render ('<div class="w-120 px-2 p-1 bg-red-600 text-center"> 😢 Can not apply Foreign Key | Please trancate table 😎 </div> ' );
92
+ } else {
93
+ $ referenceTable = $ this ->ask ("Please add foreign table name " );
94
+ $ referenceField = $ this ->ask ("Please add foreign table primary key name " );
95
+ $ auditService ->addConstrain ($ tableName , $ selectField , $ selectConstrain , $ referenceTable , $ referenceField );
96
+ }
70
97
}
71
- $ userInput = $ this ->choice ("Please select constraints which you want process " , $ options );
72
- }
73
- $ selectField = $ this ->choice ("Please Select Field where you want to apply " . strtolower ($ userInput ) . " key " , $ checkUserHasFields );
74
- if ($ userInput === Constant::CONSTRAIN_FOREIGN_KEY ) {
75
- $ referenceTable = $ this ->ask ("Please add reference table name " );
76
- $ referenceField = $ this ->ask ("Please add reference table primary key name " );
77
- }
78
98
79
- $ auditService ->addConstrain ($ tableName , $ selectField , $ userInput , $ referenceTable , $ referenceField );
80
- render ('<div class="w-100 px-1 p-1 bg-green-600 text-center">Run Successfully</div> ' );
99
+ $ auditService ->addConstrain ($ tableName , $ selectField , $ selectConstrain );
100
+
101
+ renderUsing ($ this ->output );
102
+
103
+ render ('<div class="w-120 px-2 p-1 bg-green-600 text-center"> 😎 Constrain Add Successfully 😎 </div> ' );
104
+
105
+ $ data = [
106
+ "table " => $ tableName ,
107
+ "size " => $ auditService ->getTableSize ($ tableName ),
108
+ "fields " => $ auditService ->getTableFields ($ tableName ),
109
+ 'field_count ' => count ($ auditService ->getTableFields ($ tableName )),
110
+ 'constrain ' => [
111
+ 'primary ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_PRIMARY_KEY ),
112
+ 'unique ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_UNIQUE_KEY ),
113
+ 'foreign ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_FOREIGN_KEY ),
114
+ 'index ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_INDEX_KEY )
115
+ ]
116
+ ];
117
+
118
+ render (view ('DBAuditor::constraint ' , ['data ' => $ data ]));
119
+ } else {
120
+ $ continue = Constant::STATUS_FALSE ;
121
+ }
122
+ } else {
123
+ $ continue = Constant::STATUS_FALSE ;
81
124
}
82
- }
83
- } else {
84
- $ results = $ auditService ->getList ($ userInput );
85
- if (!$ results ) {
86
- return render ('<div class="w-100 px-1 p-1 bg-red-600 text-center">No Table Found</div> ' );
87
- }
88
- render (view ('DBAuditor::constraint ' , ['tables ' => $ results ]));
125
+ } while ($ continue === Constant::STATUS_TRUE );
89
126
}
90
127
} catch (Exception $ exception ) {
91
128
Log::error ($ exception ->getMessage ());
92
- return render ( ' <div class="w-100 px-1 p-1 bg-red-600 text-center"> ' . $ exception ->getMessage () . ' </div> ' );
129
+ return $ exception ->getMessage ();
93
130
}
94
131
95
132
return self ::SUCCESS ;
0 commit comments