作者 赵彬吉

update

@@ -5,6 +5,7 @@ namespace App\Http\Logic\Aside; @@ -5,6 +5,7 @@ namespace App\Http\Logic\Aside;
5 5
6 use App\Http\Logic\Aside\User\ProjectLogic; 6 use App\Http\Logic\Aside\User\ProjectLogic;
7 use App\Models\ServerConfig; 7 use App\Models\ServerConfig;
  8 +use App\Services\ProjectServer;
8 use Illuminate\Support\Facades\DB; 9 use Illuminate\Support\Facades\DB;
9 10
10 /** 11 /**
@@ -36,10 +37,15 @@ class ServerConfigLogic extends BaseLogic @@ -36,10 +37,15 @@ class ServerConfigLogic extends BaseLogic
36 $data['mysql_id'] = $res['id']; 37 $data['mysql_id'] = $res['id'];
37 } 38 }
38 39
39 - //todo 创建数据库 初始化表  
40 -  
41 (new ProjectLogic)->save($data); 40 (new ProjectLogic)->save($data);
42 41
  42 + //数据库配置
  43 + $project = ProjectServer::useProject($param['project_id']);
  44 + //创建数据库
  45 + ProjectServer::createDatabase($project);
  46 + //创建表
  47 + ProjectServer::initTable($project);
  48 +
43 DB::commit(); 49 DB::commit();
44 } catch (\Exception $e) { 50 } catch (\Exception $e) {
45 DB::rollBack(); 51 DB::rollBack();
@@ -78,25 +78,25 @@ class ServerConfig extends Base @@ -78,25 +78,25 @@ class ServerConfig extends Base
78 /** 78 /**
79 * @return mixed 79 * @return mixed
80 */ 80 */
81 - public function getUserAttribute() 81 + public function getUserAttribute($value)
82 { 82 {
83 - return decrypt($this->user); 83 + return decrypt($value);
84 } 84 }
85 85
86 /** 86 /**
87 * @return mixed 87 * @return mixed
88 */ 88 */
89 - public function getPasswordAttribute() 89 + public function getPasswordAttribute($value)
90 { 90 {
91 - return decrypt($this->password); 91 + return decrypt($value);
92 } 92 }
93 93
94 /** 94 /**
95 * @return mixed 95 * @return mixed
96 */ 96 */
97 - public function getPortAttribute() 97 + public function getPortAttribute($value)
98 { 98 {
99 - return decrypt($this->port); 99 + return decrypt($value);
100 } 100 }
101 101
102 } 102 }
@@ -28,11 +28,11 @@ class ProjectServer extends BaseService @@ -28,11 +28,11 @@ class ProjectServer extends BaseService
28 if (empty($project)) 28 if (empty($project))
29 return false; 29 return false;
30 // 设置 database.connections.custom_mysql 配置 30 // 设置 database.connections.custom_mysql 配置
31 - config(['database.connections.custom_mysql.host' => $project->mysqlConfig()->host]);  
32 - config(['database.connections.custom_mysql.port' => $project->mysqlConfig()->port]); 31 + config(['database.connections.custom_mysql.host' => $project->mysqlConfig->host]);
  32 + config(['database.connections.custom_mysql.port' => $project->mysqlConfig->port]);
33 config(['database.connections.custom_mysql.database' => $project->databaseName()]); 33 config(['database.connections.custom_mysql.database' => $project->databaseName()]);
34 - config(['database.connections.custom_mysql.username' => $project->mysqlConfig()->user]);  
35 - config(['database.connections.custom_mysql.password' => $project->mysqlConfig()->password]); 34 + config(['database.connections.custom_mysql.username' => $project->mysqlConfig->user]);
  35 + config(['database.connections.custom_mysql.password' => $project->mysqlConfig->password]);
36 // 设置 redis 配置 36 // 设置 redis 配置
37 return $project; 37 return $project;
38 } 38 }
@@ -40,40 +40,39 @@ class ProjectServer extends BaseService @@ -40,40 +40,39 @@ class ProjectServer extends BaseService
40 40
41 /** 41 /**
42 * 创建数据库 42 * 创建数据库
43 - * @param $project_id 43 + * @param $project
  44 + * @return bool
44 * @author zbj 45 * @author zbj
45 * @date 2023/4/23 46 * @date 2023/4/23
46 */ 47 */
47 - public static function createDatabase($project_id){  
48 - $project = self::useProject($project_id);  
49 - DB::connection('custom_mysql')->statement("CREATE DATABASE IF NOT EXISTS {$project->databaseName()}"); 48 + public static function createDatabase($project)
  49 + {
  50 + $conn = new \mysqli($project->mysqlConfig->host, $project->mysqlConfig->user, $project->mysqlConfig->password);
  51 + $conn->query("CREATE DATABASE IF NOT EXISTS {$project->databaseName()}");
  52 + return true;
50 } 53 }
51 54
52 55
53 /** 56 /**
54 - * @param $project_id 57 + * @param $project
55 * @return bool 58 * @return bool
56 - * @throws \Doctrine\DBAL\Exception  
57 * @author zbj 59 * @author zbj
58 * @date 2023/4/23 60 * @date 2023/4/23
59 */ 61 */
60 - public static function initTable($project_id){  
61 - $project = self::useProject($project_id); 62 + public static function initTable($project)
  63 + {
62 $database_name = DB::connection('custom_tmp_mysql')->getDatabaseName(); 64 $database_name = DB::connection('custom_tmp_mysql')->getDatabaseName();
63 65
64 $table = Schema::connection('custom_tmp_mysql')->getAllTables(); 66 $table = Schema::connection('custom_tmp_mysql')->getAllTables();
65 $table = array_column($table, 'Tables_in_' . $database_name); 67 $table = array_column($table, 'Tables_in_' . $database_name);
66 foreach ($table as $v) { 68 foreach ($table as $v) {
67 $has_table = Schema::connection('custom_mysql')->hasTable($v); 69 $has_table = Schema::connection('custom_mysql')->hasTable($v);
68 - if ($has_table) 70 + if ($has_table) {
69 continue; 71 continue;
  72 + }
70 73
71 - $connection = DB::connection('custom_tmp_mysql');  
72 - $sql = $connection->getDoctrineSchemaManager()  
73 - ->getDatabasePlatform()  
74 - ->getCreateTableSQL($connection->getDoctrineSchemaManager()->listTableDetails($v));  
75 -  
76 - DB::connection('custom_mysql')->select($sql[0]); 74 + $sql = DB::connection('custom_tmp_mysql')->select("SHOW CREATE TABLE {$v}");
  75 + DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
77 } 76 }
78 return true; 77 return true;
79 } 78 }