ProjectInit.php
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2023/4/12
* Time: 15:33
*/
namespace App\Console\Commands;
use App\Models\Project;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Class ProjectInitDatabase
* @package App\Console\Commands
*/
class ProjectInit extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'project:init';
/**
* The console command description.
*
* @var string
*/
protected $description = '项目数据库初始化';
protected $connect = null;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* @return bool
*/
public function handle()
{
#TODO 通过项目ID获取项目部署数据库配置, 创建数据库, 同步数据表
$project_id = 102;
$project = Project::getProjectById($project_id);
if (empty($project) || empty($project->mysqlConfig()))
return true;
$this->initDatabase($project);
return true;
}
/**
* @param Project $project
* @return bool
*/
public function initDatabase($project)
{
$create_flag = $this->createDatabase($project);
if (!$create_flag) {
// 创建数据库失败 添加通知以及再次处理
}
// 设置 database.connections.custom_mysql 数据
config(['database.connections.custom_mysql.host' => $project->mysqlConfig()->host]);
config(['database.connections.custom_mysql.port' => $project->mysqlConfig()->port]);
config(['database.connections.custom_mysql.database' => $project->databaseName()]);
config(['database.connections.custom_mysql.username' => $project->mysqlConfig()->user]);
config(['database.connections.custom_mysql.password' => $project->mysqlConfig()->password]);
// TODO 创建对应库 初始化数据表
$this->initTable();
return true;
}
/**
* @return bool
*/
public function initTable()
{
// $table = DB::select('show tables');
// $table = array_column($table, 'Tables_in_globalso_dev');
// $table = DB::connection('custom_tmp_mysql')->select('show tables');
// $table_in = DB::connection('custom_tmp_mysql')->getDatabaseName();
// dd($table, $table_in);
$database_name = DB::connection('custom_tmp_mysql')->getDatabaseName();
$table = Schema::connection('custom_tmp_mysql')->getAllTables();
$table = array_column($table, 'Tables_in_' . $database_name);
foreach ($table as $v) {
$has_table = Schema::connection('custom_mysql')->hasTable($v);
if ($has_table)
continue;
$connection = DB::connection('custom_tmp_mysql');
$sql = $connection->getDoctrineSchemaManager()
->getDatabasePlatform()
->getCreateTableSQL($connection->getDoctrineSchemaManager()->listTableDetails($v));
DB::connection('custom_mysql')->select($sql[0]);
}
return true;
}
/**
* 创建数据库
* 链接mysql 查询数据库是否存在 创建数据库
* @param Project $project
* @return bool|\mysqli_result|null
*/
public function createDatabase($project)
{
# 该方法需要:composer require parity-bit/laravel-db-commands
// $result = Artisan::call('db:create', [
// '--database' => $database_name,
// ]);
//
// return $result;
if ($this->connect)
return $this->connect;
//连接到 MySQL 服务器
$servername = $project->mysqlConfig()->host;
$username = $project->mysqlConfig()->user;
$password = $project->mysqlConfig()->password;
$conn = new \mysqli($servername, $username, $password);
//检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$this->connect = $conn;
// $result = $conn->query('SHOW DATABASES LIKE \'' . $database_name . '\';');
// if ($result)
// return true;
$result = $conn->query('CREATE DATABASE ' . $project->databaseName() . ';');
return $result;
}
}