3
3
use Carbon \Carbon ;
4
4
use Illuminate \Database \Eloquent \Model ;
5
5
use Illuminate \Database \Eloquent \SoftDeletes ;
6
+ use Illuminate \Support \Facades \Validator ;
6
7
use Illuminate \Support \Str ;
8
+ use VaahCms \Modules \Saas \Libraries \DatabaseManagers \DatabaseManager ;
7
9
use WebReinvent \VaahCms \Traits \CrudWithUuidObservantTrait ;
8
10
use WebReinvent \VaahCms \Entities \User ;
9
11
@@ -17,7 +19,7 @@ class Tenant extends Model {
17
19
//-------------------------------------------------
18
20
protected $ dates = [
19
21
'is_database_created_at ' ,
20
- 'is_active_at ' ,
22
+ 'activated_at ' ,
21
23
'is_deactivated_at ' ,
22
24
'created_at ' ,
23
25
'updated_at ' ,
@@ -39,7 +41,8 @@ class Tenant extends Model {
39
41
'database_charset ' ,
40
42
'database_collation ' ,
41
43
'is_database_created_at ' ,
42
- 'is_active_at ' ,
44
+ 'activated_at ' ,
45
+ 'is_active ' ,
43
46
'is_deactivated_at ' ,
44
47
'notes ' ,
45
48
@@ -413,6 +416,7 @@ public static function validation($inputs)
413
416
$ rules = array (
414
417
'name ' => 'required|max:150 ' ,
415
418
'slug ' => 'required|max:150 ' ,
419
+ 'database_name ' => 'required|alpha_dash|max:20 ' ,
416
420
);
417
421
418
422
$ validator = \Validator::make ( $ inputs , $ rules );
@@ -424,8 +428,225 @@ public static function validation($inputs)
424
428
return $ response ;
425
429
}
426
430
431
+ $ exist = static ::withTrashed ()->where ('database_name ' , $ inputs ['database_name ' ])
432
+ ->first ();
433
+
434
+
435
+ if ($ exist )
436
+ {
437
+ $ response ['status ' ] = 'failed ' ;
438
+ $ response ['errors ' ][] = 'Database name already exist ' ;
439
+ return $ response ;
440
+ }
441
+
442
+
443
+ }
444
+ //-------------------------------------------------
445
+ public static function createDatabase ($ request )
446
+ {
447
+
448
+ if (!$ request ->has ('inputs ' ))
449
+ {
450
+ $ response ['status ' ] = 'failed ' ;
451
+ $ response ['errors ' ][] = 'Select IDs ' ;
452
+ return $ response ;
453
+ }
454
+
455
+ $ item = static ::where ('id ' , $ request ->inputs )->withTrashed ()->first ();
456
+ $ server = Server::find ($ item ->vh_saas_server_id );
457
+
458
+ $ db_manager = new DatabaseManager ($ server , $ item );
459
+ $ response = $ db_manager ->createDatabase ();
460
+
461
+ if ($ response ['status ' ] == 'success ' )
462
+ {
463
+ $ item ->is_active = 1 ;
464
+ $ item ->activated_at = \Carbon::now ();
465
+ $ item ->is_database_created_at = \Carbon::now ();
466
+ $ item ->is_deactivated_at = null ;
467
+ $ item ->save ();
468
+ $ response ['data ' ] = [];
469
+ }
470
+
471
+ return $ response ;
472
+ }
473
+ //-------------------------------------------------
474
+ public static function deleteDatabase ($ request )
475
+ {
476
+
477
+ if (!$ request ->has ('inputs ' ))
478
+ {
479
+ $ response ['status ' ] = 'failed ' ;
480
+ $ response ['errors ' ][] = 'Select IDs ' ;
481
+ return $ response ;
482
+ }
483
+
484
+ $ item = static ::where ('id ' , $ request ->inputs )->withTrashed ()->first ();
485
+ $ server = Server::find ($ item ->vh_saas_server_id );
486
+
487
+ $ db_manager = new DatabaseManager ($ server , $ item );
488
+ $ response = $ db_manager ->deleteDatabase ();
489
+
490
+ if ($ response ['status ' ] == 'success ' )
491
+ {
492
+
493
+ $ item ->is_active = null ;
494
+ $ item ->activated_at = null ;
495
+ $ item ->is_database_created_at = null ;
496
+ $ item ->is_deactivated_at = \Carbon::now ();
497
+ $ item ->save ();
498
+
499
+ $ response ['data ' ] = [];
500
+
501
+ }
502
+
503
+ return $ response ;
504
+
505
+
506
+ }
507
+ //-------------------------------------------------
508
+ public static function artisanCommandValidation ($ value , $ key ='uuid ' )
509
+ {
510
+ $ tenant = static ::withTrashed ()->where ($ key , $ value )->first ();
511
+
512
+ if (!$ tenant )
513
+ {
514
+ $ response ['status ' ] = 'failed ' ;
515
+ $ response ['errors ' ][] = 'Tenant does not exist ' ;
516
+ return $ response ;
517
+ }
518
+
519
+ //check database is created for the tenant
520
+ if (!$ tenant ->is_database_created_at )
521
+ {
522
+ $ response ['status ' ] = 'failed ' ;
523
+ $ response ['errors ' ][] = 'Tenant database is not created ' ;
524
+ return $ response ;
525
+ }
526
+
527
+ $ server = Server::find ($ tenant ->vh_saas_server_id );
528
+
529
+ if (!$ server )
530
+ {
531
+ $ response ['status ' ] = 'failed ' ;
532
+ $ response ['errors ' ][] = 'Tenant database server does not exist ' ;
533
+ return $ response ;
534
+ }
535
+
536
+ $ db_manager = new DatabaseManager ($ server , $ tenant );
537
+
538
+ //check server connection
539
+ $ is_connected = $ db_manager ->testConnection ();
540
+ if ($ is_connected ['status ' ] == 'failed ' )
541
+ {
542
+ return $ is_connected ;
543
+ }
544
+
545
+ //check if database does not exist on the server
546
+ $ db_exist = $ db_manager ->databaseExists ();
547
+ if ($ db_exist ['status ' ] == 'failed ' )
548
+ {
549
+ return $ db_exist ;
550
+ }
427
551
}
428
552
//-------------------------------------------------
553
+ public static function migrate ($ inputs , $ value , $ key ='uuid ' )
554
+ {
555
+ $ rules = array (
556
+ 'command ' => 'required ' ,
557
+ );
558
+
559
+ if (isset ($ inputs ['command ' ]))
560
+ {
561
+ $ rules ['path ' ] = 'required ' ;
562
+ }
563
+
564
+ $ validator = Validator::make ( $ inputs , $ rules );
565
+ if ( $ validator ->fails () ) {
566
+
567
+ $ errors = errorsToArray ($ validator ->errors ());
568
+ $ response ['status ' ] = 'failed ' ;
569
+ $ response ['errors ' ] = $ errors ;
570
+ return $ response ;
571
+ }
572
+
573
+ $ is_valid = static ::artisanCommandValidation ($ value , $ key ='uuid ' );
574
+
575
+ if ($ is_valid ['status ' ] == 'failed ' )
576
+ {
577
+ return $ is_valid ;
578
+ }
579
+
580
+ $ tenant = static ::withTrashed ()->where ($ key , $ value )->first ();
581
+ $ server = Server::find ($ tenant ->vh_saas_server_id );
582
+ $ db_manager = new DatabaseManager ($ server , $ tenant );
583
+
584
+ //connect to database
585
+ $ connection = $ db_manager ->connectToDatabase ();
586
+
587
+ if (isset ($ connection ['status ' ]) && $ connection ['status ' ] == 'failed ' )
588
+ {
589
+ return $ connection ;
590
+ }
591
+
592
+ $ db_connection_name = $ connection ['data ' ]['connection_name ' ];
593
+
594
+ $ response = \VaahArtisan::migrate ($ inputs ['command ' ], $ db_connection_name , $ inputs ['path ' ]);
595
+
596
+ return $ response ;
597
+
598
+ }
599
+ //-------------------------------------------------
600
+ public static function seed ($ inputs , $ value , $ key ='uuid ' )
601
+ {
602
+ $ rules = array (
603
+ 'command ' => 'required ' ,
604
+ );
605
+
606
+ if (isset ($ inputs ['command ' ]))
607
+ {
608
+ $ rules ['class ' ] = 'required ' ;
609
+ }
610
+
611
+ $ validator = Validator::make ( $ inputs , $ rules );
612
+ if ( $ validator ->fails () ) {
613
+
614
+ $ errors = errorsToArray ($ validator ->errors ());
615
+ $ response ['status ' ] = 'failed ' ;
616
+ $ response ['errors ' ] = $ errors ;
617
+ return $ response ;
618
+ }
619
+
620
+ $ is_valid = static ::artisanCommandValidation ($ value , $ key ='uuid ' );
621
+
622
+ if ($ is_valid ['status ' ] == 'failed ' )
623
+ {
624
+ return $ is_valid ;
625
+ }
626
+
627
+ $ tenant = static ::withTrashed ()->where ($ key , $ value )->first ();
628
+ $ server = Server::find ($ tenant ->vh_saas_server_id );
629
+ $ db_manager = new DatabaseManager ($ server , $ tenant );
630
+
631
+ //connect to database
632
+ $ connection = $ db_manager ->connectToDatabase ();
633
+
634
+ if (isset ($ connection ['status ' ]) && $ connection ['status ' ] == 'failed ' )
635
+ {
636
+ return $ connection ;
637
+ }
638
+
639
+ $ db_connection_name = $ connection ['data ' ]['connection_name ' ];
640
+
641
+ $ response = \VaahArtisan::seed ($ inputs ['command ' ], $ db_connection_name , $ inputs ['class ' ]);
642
+
643
+ return $ response ;
644
+
645
+ }
646
+
647
+ //-------------------------------------------------
648
+ //-------------------------------------------------
649
+ //-------------------------------------------------
429
650
//-------------------------------------------------
430
651
//-------------------------------------------------
431
652
0 commit comments