feat: 重构安装向导页(管理员并入基础、存储测试、必填标记、主题语言切换)
管理员账号/密码/姓名并入基础卡片并移除独立管理员卡片; 移除 MySQL 建库管理员字段,库不存在自动建库、无权限给出清晰错误提示; 新增存储测试(本地目录可写 / OSS-S3 真实读写校验)后端 /api/install/test-storage; CDN 增加留空即不启用说明;必填项加红色 * 标记; 头部加入与后台一致的主题/语言切换按钮;清理页面冗余注释。
This commit is contained in:
@@ -198,16 +198,16 @@
|
||||
"database": "Database",
|
||||
"username": "Username",
|
||||
"password": "Password",
|
||||
"adminUser": "DB admin user (optional)",
|
||||
"adminPass": "DB admin password (optional)",
|
||||
"mysqlAdminHint": "Only needed if the business account cannot create databases; the database, tables and data are created automatically.",
|
||||
"admin": "Admin",
|
||||
"adminAccount": "Admin Account",
|
||||
"adminPwd": "Admin Password",
|
||||
"realName": "Name",
|
||||
"storage": "Storage",
|
||||
"driver": "Driver",
|
||||
"local": "Local",
|
||||
"localDir": "Local Directory",
|
||||
"cdnBaseUrl": "CDN URL",
|
||||
"cdnHint": "CDN acceleration URL; leave empty to disable (empty = serve from origin directly).",
|
||||
"failed": "Installation failed",
|
||||
"done": "Initialized. The service is restarting, please wait…",
|
||||
"loadFailed": "Failed to read installation status",
|
||||
|
||||
@@ -198,16 +198,16 @@
|
||||
"database": "数据库",
|
||||
"username": "账号",
|
||||
"password": "密码",
|
||||
"adminUser": "建库管理员账号(可选)",
|
||||
"adminPass": "建库管理员密码(可选)",
|
||||
"mysqlAdminHint": "仅当业务账号无建库权限时填写;系统会自动创建数据库并初始化表结构与数据。",
|
||||
"admin": "管理员",
|
||||
"adminAccount": "管理员账号",
|
||||
"adminPwd": "管理员密码",
|
||||
"realName": "姓名",
|
||||
"storage": "存储",
|
||||
"driver": "方式",
|
||||
"local": "本地",
|
||||
"localDir": "本地目录",
|
||||
"cdnBaseUrl": "CDN 地址",
|
||||
"cdnHint": "CDN 加速地址;如不启用可留空(为空则不开启,直接走源站)。",
|
||||
"failed": "安装失败",
|
||||
"done": "初始化完成,服务正在重启,请稍候…",
|
||||
"loadFailed": "读取安装状态失败",
|
||||
|
||||
@@ -6,9 +6,13 @@
|
||||
<h1>{{ form.appName || 'Artveris' }}</h1>
|
||||
<p>{{ tt('subtitle') }}</p>
|
||||
</div>
|
||||
<n-button v-if="!installed" type="primary" size="large" :loading="submitting" @click="submit">
|
||||
{{ submitting ? tt('installing') : tt('start') }}
|
||||
</n-button>
|
||||
<div class="head-actions">
|
||||
<lang-switch />
|
||||
<theme-switch />
|
||||
<n-button v-if="!installed" type="primary" size="large" :loading="submitting" @click="submit">
|
||||
{{ submitting ? tt('installing') : tt('start') }}
|
||||
</n-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<n-spin :show="loading">
|
||||
@@ -27,9 +31,14 @@
|
||||
<div class="grid">
|
||||
<section class="box">
|
||||
<h2>{{ tt('base') }}</h2>
|
||||
<label><span>{{ tt('appName') }}</span><n-input v-model:value="form.appName" /></label>
|
||||
<label><span>{{ tt('baseUrl') }}</span><n-input v-model:value="form.baseUrl" placeholder="https://example.com" /></label>
|
||||
<label><span>{{ tt('appName') }}<i class="req">*</i></span><n-input v-model:value="form.appName" /></label>
|
||||
<label><span>{{ tt('baseUrl') }}<i class="req">*</i></span><n-input v-model:value="form.baseUrl" placeholder="https://example.com" /></label>
|
||||
<p class="hint">{{ tt('baseUrlHint') }}</p>
|
||||
|
||||
<div class="sub">{{ tt('admin') }}</div>
|
||||
<label><span>{{ tt('adminAccount') }}<i class="req">*</i></span><n-input v-model:value="form.admin.username" /></label>
|
||||
<label><span>{{ tt('adminPwd') }}<i class="req">*</i></span><n-input v-model:value="form.admin.password" type="password" show-password-on="click" /></label>
|
||||
<label><span>{{ tt('realName') }}</span><n-input v-model:value="form.admin.realName" /></label>
|
||||
</section>
|
||||
|
||||
<section class="box">
|
||||
@@ -37,15 +46,12 @@
|
||||
<h2>MySQL</h2>
|
||||
<n-button size="tiny" :loading="testingMysql" @click="testMysql">{{ tt('testConnection') }}</n-button>
|
||||
</div>
|
||||
<label><span>{{ tt('host') }}</span><n-input v-model:value="form.mysql.host" /></label>
|
||||
<label><span>{{ tt('port') }}</span><n-input-number v-model:value="form.mysql.port" :min="1" :show-button="false" class="full" /></label>
|
||||
<label><span>{{ tt('database') }}</span><n-input v-model:value="form.mysql.database" /></label>
|
||||
<label><span>{{ tt('username') }}</span><n-input v-model:value="form.mysql.username" /></label>
|
||||
<label><span>{{ tt('host') }}<i class="req">*</i></span><n-input v-model:value="form.mysql.host" /></label>
|
||||
<label><span>{{ tt('port') }}<i class="req">*</i></span><n-input-number v-model:value="form.mysql.port" :min="1" :show-button="false" class="full" /></label>
|
||||
<label><span>{{ tt('database') }}<i class="req">*</i></span><n-input v-model:value="form.mysql.database" /></label>
|
||||
<label><span>{{ tt('username') }}<i class="req">*</i></span><n-input v-model:value="form.mysql.username" /></label>
|
||||
<label><span>{{ tt('password') }}</span><n-input v-model:value="form.mysql.password" type="password" show-password-on="click" /></label>
|
||||
<label><span>{{ tt('adminUser') }}</span><n-input v-model:value="form.mysql.adminUsername" /></label>
|
||||
<label><span>{{ tt('adminPass') }}</span><n-input v-model:value="form.mysql.adminPassword" type="password" show-password-on="click" /></label>
|
||||
<p v-if="mysqlTest" class="test-result" :class="mysqlTest.ok ? 'ok' : 'fail'">{{ mysqlTest.msg }}</p>
|
||||
<p class="hint">{{ tt('mysqlAdminHint') }}</p>
|
||||
</section>
|
||||
|
||||
<section class="box">
|
||||
@@ -53,34 +59,34 @@
|
||||
<h2>Redis</h2>
|
||||
<n-button size="tiny" :loading="testingRedis" @click="testRedis">{{ tt('testConnection') }}</n-button>
|
||||
</div>
|
||||
<label><span>{{ tt('host') }}</span><n-input v-model:value="form.redis.host" /></label>
|
||||
<label><span>{{ tt('port') }}</span><n-input-number v-model:value="form.redis.port" :min="1" :show-button="false" class="full" /></label>
|
||||
<label><span>{{ tt('host') }}<i class="req">*</i></span><n-input v-model:value="form.redis.host" /></label>
|
||||
<label><span>{{ tt('port') }}<i class="req">*</i></span><n-input-number v-model:value="form.redis.port" :min="1" :show-button="false" class="full" /></label>
|
||||
<label><span>{{ tt('password') }}</span><n-input v-model:value="form.redis.password" type="password" show-password-on="click" /></label>
|
||||
<p v-if="redisTest" class="test-result" :class="redisTest.ok ? 'ok' : 'fail'">{{ redisTest.msg }}</p>
|
||||
</section>
|
||||
|
||||
<section class="box">
|
||||
<h2>{{ tt('admin') }}</h2>
|
||||
<label><span>{{ tt('username') }}</span><n-input v-model:value="form.admin.username" /></label>
|
||||
<label><span>{{ tt('password') }}</span><n-input v-model:value="form.admin.password" type="password" show-password-on="click" /></label>
|
||||
<label><span>{{ tt('realName') }}</span><n-input v-model:value="form.admin.realName" /></label>
|
||||
</section>
|
||||
|
||||
<section class="box">
|
||||
<h2>{{ tt('storage') }}</h2>
|
||||
<div class="box-head">
|
||||
<h2>{{ tt('storage') }}</h2>
|
||||
<n-button size="tiny" :loading="testingStorage" @click="testStorage">{{ tt('testConnection') }}</n-button>
|
||||
</div>
|
||||
<label>
|
||||
<span>{{ tt('driver') }}</span>
|
||||
<span>{{ tt('driver') }}<i class="req">*</i></span>
|
||||
<n-select v-model:value="form.storage.driver" :options="driverOptions" class="full" />
|
||||
</label>
|
||||
<label><span>{{ tt('localDir') }}</span><n-input v-model:value="form.storage.localDir" /></label>
|
||||
<label><span>{{ tt('cdnBaseUrl') }}</span><n-input v-model:value="form.storage.cdnBaseUrl" /></label>
|
||||
<template v-if="form.storage.driver === 'oss'">
|
||||
<label><span>Endpoint</span><n-input v-model:value="form.storage.ossEndpoint" /></label>
|
||||
<label><span>Region</span><n-input v-model:value="form.storage.ossRegion" /></label>
|
||||
<label><span>Bucket</span><n-input v-model:value="form.storage.ossBucket" /></label>
|
||||
<label><span>AccessKey ID</span><n-input v-model:value="form.storage.ossAccessKeyId" /></label>
|
||||
<label><span>AccessKey Secret</span><n-input v-model:value="form.storage.ossAccessKeySecret" type="password" show-password-on="click" /></label>
|
||||
<template v-if="form.storage.driver === 'local'">
|
||||
<label><span>{{ tt('localDir') }}<i class="req">*</i></span><n-input v-model:value="form.storage.localDir" /></label>
|
||||
</template>
|
||||
<template v-else>
|
||||
<label><span>Endpoint<i class="req">*</i></span><n-input v-model:value="form.storage.ossEndpoint" /></label>
|
||||
<label><span>Region</span><n-input v-model:value="form.storage.ossRegion" /></label>
|
||||
<label><span>Bucket<i class="req">*</i></span><n-input v-model:value="form.storage.ossBucket" /></label>
|
||||
<label><span>AccessKey ID<i class="req">*</i></span><n-input v-model:value="form.storage.ossAccessKeyId" /></label>
|
||||
<label><span>AccessKey Secret<i class="req">*</i></span><n-input v-model:value="form.storage.ossAccessKeySecret" type="password" show-password-on="click" /></label>
|
||||
</template>
|
||||
<label><span>CDN</span><n-input v-model:value="form.storage.cdnBaseUrl" placeholder="https://cdn.example.com" /></label>
|
||||
<p class="hint">{{ tt('cdnHint') }}</p>
|
||||
<p v-if="storageTest" class="test-result" :class="storageTest.ok ? 'ok' : 'fail'">{{ storageTest.msg }}</p>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
@@ -95,19 +101,13 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import ThemeSwitch from '@/components/ThemeSwitch.vue';
|
||||
import LangSwitch from '@/components/LangSwitch.vue';
|
||||
|
||||
interface InstallForm {
|
||||
appName: string;
|
||||
baseUrl: string;
|
||||
mysql: {
|
||||
host: string;
|
||||
port: number;
|
||||
database: string;
|
||||
username: string;
|
||||
password: string;
|
||||
adminUsername: string;
|
||||
adminPassword: string;
|
||||
};
|
||||
mysql: { host: string; port: number; database: string; username: string; password: string };
|
||||
redis: { host: string; port: number; password: string };
|
||||
admin: { username: string; password: string; realName: string };
|
||||
storage: {
|
||||
@@ -123,7 +123,6 @@ interface InstallForm {
|
||||
}
|
||||
|
||||
const { t } = useI18n();
|
||||
// 安装向导文案统一走全局 locale(pages.install.*),保持与后台其它页面一致
|
||||
function tt(key: string): string {
|
||||
return t(`pages.install.${key}`);
|
||||
}
|
||||
@@ -136,15 +135,7 @@ const driverOptions = computed(() => [
|
||||
const form = reactive<InstallForm>({
|
||||
appName: tt('defaultAppName'),
|
||||
baseUrl: location.origin,
|
||||
mysql: {
|
||||
host: '127.0.0.1',
|
||||
port: 3306,
|
||||
database: 'artveris',
|
||||
username: 'artveris',
|
||||
password: '',
|
||||
adminUsername: '',
|
||||
adminPassword: '',
|
||||
},
|
||||
mysql: { host: '127.0.0.1', port: 3306, database: 'artveris', username: 'artveris', password: '' },
|
||||
redis: { host: '127.0.0.1', port: 6379, password: '' },
|
||||
admin: { username: 'admin', password: '', realName: tt('defaultAdminName') },
|
||||
storage: {
|
||||
@@ -166,16 +157,18 @@ const error = ref('');
|
||||
const success = ref('');
|
||||
const testingMysql = ref(false);
|
||||
const testingRedis = ref(false);
|
||||
const testingStorage = ref(false);
|
||||
const mysqlTest = ref<{ ok: boolean; msg: string } | null>(null);
|
||||
const redisTest = ref<{ ok: boolean; msg: string } | null>(null);
|
||||
const storageTest = ref<{ ok: boolean; msg: string } | null>(null);
|
||||
|
||||
function goAdmin(): void {
|
||||
location.href = '/admin';
|
||||
}
|
||||
|
||||
async function testConnection(payload: Record<string, unknown>): Promise<{ ok: boolean; msg: string }> {
|
||||
async function post(path: string, payload: Record<string, unknown>): Promise<{ ok: boolean; msg: string }> {
|
||||
try {
|
||||
const res = await fetch('/api/install/test-connection', {
|
||||
const res = await fetch(path, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
@@ -191,7 +184,7 @@ async function testConnection(payload: Record<string, unknown>): Promise<{ ok: b
|
||||
async function testMysql(): Promise<void> {
|
||||
testingMysql.value = true;
|
||||
mysqlTest.value = null;
|
||||
mysqlTest.value = await testConnection({
|
||||
mysqlTest.value = await post('/api/install/test-connection', {
|
||||
target: 'mysql',
|
||||
host: form.mysql.host,
|
||||
port: form.mysql.port,
|
||||
@@ -205,7 +198,7 @@ async function testMysql(): Promise<void> {
|
||||
async function testRedis(): Promise<void> {
|
||||
testingRedis.value = true;
|
||||
redisTest.value = null;
|
||||
redisTest.value = await testConnection({
|
||||
redisTest.value = await post('/api/install/test-connection', {
|
||||
target: 'redis',
|
||||
host: form.redis.host,
|
||||
port: form.redis.port,
|
||||
@@ -214,6 +207,13 @@ async function testRedis(): Promise<void> {
|
||||
testingRedis.value = false;
|
||||
}
|
||||
|
||||
async function testStorage(): Promise<void> {
|
||||
testingStorage.value = true;
|
||||
storageTest.value = null;
|
||||
storageTest.value = await post('/api/install/test-storage', { ...form.storage });
|
||||
testingStorage.value = false;
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const res = await fetch('/api/install/status');
|
||||
@@ -243,7 +243,7 @@ async function pollUntilReady(): Promise<void> {
|
||||
const body = await res.json();
|
||||
if (body.data?.installed) break;
|
||||
} catch {
|
||||
// 服务重启期间请求失败属正常,继续轮询
|
||||
// 服务重启期间请求失败属正常
|
||||
}
|
||||
}
|
||||
goAdmin();
|
||||
@@ -273,7 +273,6 @@ async function submit(): Promise<void> {
|
||||
|
||||
<style scoped>
|
||||
.install-page {
|
||||
/* 全局 #app 为 overflow-y:hidden,安装页是独立满屏路由,需自身承载纵向滚动 */
|
||||
height: 100vh;
|
||||
overflow-y: auto;
|
||||
padding: 24px;
|
||||
@@ -299,6 +298,11 @@ async function submit(): Promise<void> {
|
||||
padding-bottom: 14px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.head-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.brand h1 {
|
||||
margin: 0;
|
||||
font-size: 20px;
|
||||
@@ -337,17 +341,11 @@ async function submit(): Promise<void> {
|
||||
padding: 0;
|
||||
border: 0;
|
||||
}
|
||||
.test-result {
|
||||
margin: 2px 0 0;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
word-break: break-all;
|
||||
}
|
||||
.test-result.ok {
|
||||
color: #18a058;
|
||||
}
|
||||
.test-result.fail {
|
||||
color: #d03050;
|
||||
.sub {
|
||||
margin: 14px 0 10px;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
color: var(--n-text-color-2, #526070);
|
||||
}
|
||||
.box label {
|
||||
display: grid;
|
||||
@@ -360,6 +358,11 @@ async function submit(): Promise<void> {
|
||||
font-size: 13px;
|
||||
color: var(--n-text-color-2, #526070);
|
||||
}
|
||||
.req {
|
||||
color: #d03050;
|
||||
font-style: normal;
|
||||
margin-left: 2px;
|
||||
}
|
||||
.full {
|
||||
width: 100%;
|
||||
}
|
||||
@@ -369,6 +372,18 @@ async function submit(): Promise<void> {
|
||||
line-height: 1.5;
|
||||
color: var(--n-text-color-3, #6a7687);
|
||||
}
|
||||
.test-result {
|
||||
margin: 6px 0 0;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
word-break: break-all;
|
||||
}
|
||||
.test-result.ok {
|
||||
color: #18a058;
|
||||
}
|
||||
.test-result.fail {
|
||||
color: #d03050;
|
||||
}
|
||||
.mt {
|
||||
margin-top: 14px;
|
||||
}
|
||||
|
||||
@@ -27,17 +27,6 @@ export class InstallMysqlDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
password!: string;
|
||||
|
||||
// 仅当业务账号无建库权限时使用:用于自动 CREATE DATABASE。
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
adminUsername?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
adminPassword?: string;
|
||||
}
|
||||
|
||||
export class InstallRedisDto {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Body, Controller, Get, Post } from '@nestjs/common';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { Public } from '../common/decorators/public.decorator';
|
||||
import { InstallRequestDto, TestConnectionDto } from './dto/install.dto';
|
||||
import { InstallRequestDto, InstallStorageDto, TestConnectionDto } from './dto/install.dto';
|
||||
import { InstallService } from './install.service';
|
||||
|
||||
@ApiTags('Install')
|
||||
@@ -23,6 +23,13 @@ export class InstallController {
|
||||
return this.installService.testConnection(dto);
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Post('test-storage')
|
||||
@ApiOperation({ summary: 'Test storage (local/OSS) read-write' })
|
||||
testStorage(@Body() dto: InstallStorageDto) {
|
||||
return this.installService.testStorage(dto);
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Initialize installation' })
|
||||
|
||||
@@ -7,7 +7,10 @@ import * as bcrypt from 'bcryptjs';
|
||||
import { PrismaClient, RoleType, UserStatus } from '@prisma/client';
|
||||
import Redis from 'ioredis';
|
||||
import { envFilePath, isInstallMode } from '../config/runtime-config';
|
||||
import { InstallRequestDto, TestConnectionDto } from './dto/install.dto';
|
||||
import { InstallRequestDto, InstallStorageDto, TestConnectionDto } from './dto/install.dto';
|
||||
import { LocalDriver } from '../storage/local.driver';
|
||||
import { OssDriver } from '../storage/oss.driver';
|
||||
import type { StorageDriver } from '../storage/storage.types';
|
||||
|
||||
const ENCRYPTED_PREFIX = 'enc:v1:';
|
||||
|
||||
@@ -99,6 +102,42 @@ export class InstallService {
|
||||
}
|
||||
}
|
||||
|
||||
// 安装页「测试存储」:本地校验目录可写、OSS/S3 校验凭据可读写。只做健康检查、随后清理。
|
||||
async testStorage(dto: InstallStorageDto): Promise<{ success: true; message: string }> {
|
||||
if (!isInstallMode()) {
|
||||
throw new Error('系统已安装,存储测试不可用');
|
||||
}
|
||||
let driver: StorageDriver;
|
||||
if (dto.driver === 'oss') {
|
||||
if (!dto.ossBucket || !dto.ossAccessKeyId || !dto.ossAccessKeySecret) {
|
||||
throw new Error('OSS 配置不完整:Endpoint / Bucket / AccessKey ID / AccessKey Secret 均为必填');
|
||||
}
|
||||
driver = new OssDriver({
|
||||
endpoint: dto.ossEndpoint || '',
|
||||
region: dto.ossRegion || '',
|
||||
bucket: dto.ossBucket,
|
||||
accessKeyId: dto.ossAccessKeyId,
|
||||
accessKeySecret: dto.ossAccessKeySecret,
|
||||
cdnBaseUrl: dto.cdnBaseUrl || '',
|
||||
});
|
||||
} else {
|
||||
driver = new LocalDriver({
|
||||
localDir: dto.localDir || resolve(process.cwd(), 'storage'),
|
||||
publicPrefix: '/uploads',
|
||||
cdnBaseUrl: dto.cdnBaseUrl || '',
|
||||
});
|
||||
}
|
||||
const key = `.health/install-${Date.now()}.txt`;
|
||||
try {
|
||||
await driver.put(key, Buffer.from('ok'), { contentType: 'text/plain' });
|
||||
await driver.get(key);
|
||||
await driver.remove(key);
|
||||
} catch (e) {
|
||||
throw new Error(`存储测试失败:${(e as Error).message}`);
|
||||
}
|
||||
return { success: true, message: dto.driver === 'oss' ? 'OSS/S3 读写测试通过' : '本地目录读写测试通过' };
|
||||
}
|
||||
|
||||
private buildMysqlUrl(dto: InstallRequestDto): string {
|
||||
const user = encodeURIComponent(dto.mysql.username);
|
||||
const password = encodeURIComponent(dto.mysql.password);
|
||||
@@ -116,39 +155,29 @@ export class InstallService {
|
||||
}
|
||||
}
|
||||
|
||||
// 首次安装自动建库:优先用业务账号,失败再尝试建库管理员账号;库已存在则幂等跳过。
|
||||
// 库不存在时用业务账号自动尝试建库;无建库权限则给出清晰错误提示。
|
||||
private async ensureDatabase(dto: InstallRequestDto): Promise<void> {
|
||||
const db = dto.mysql.database;
|
||||
if (!/^[A-Za-z0-9_]+$/.test(db)) {
|
||||
throw new Error('数据库名仅允许字母、数字和下划线');
|
||||
}
|
||||
const credentials = [{ username: dto.mysql.username, password: dto.mysql.password }];
|
||||
if (dto.mysql.adminUsername) {
|
||||
credentials.push({ username: dto.mysql.adminUsername, password: dto.mysql.adminPassword ?? '' });
|
||||
const user = encodeURIComponent(dto.mysql.username);
|
||||
const password = encodeURIComponent(dto.mysql.password);
|
||||
const serverUrl = `mysql://${user}:${password}@${dto.mysql.host}:${dto.mysql.port}/information_schema`;
|
||||
const prisma = new PrismaClient({ datasources: { db: { url: serverUrl } } });
|
||||
try {
|
||||
await prisma.$connect();
|
||||
await prisma.$executeRawUnsafe(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`,
|
||||
);
|
||||
} catch {
|
||||
throw new Error(
|
||||
`所需库「${db}」不存在,当前账号无建库权限,请手动建库或提供足够权限的账号。` +
|
||||
`可手动执行:CREATE DATABASE \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;`,
|
||||
);
|
||||
} finally {
|
||||
await prisma.$disconnect().catch(() => undefined);
|
||||
}
|
||||
let lastError: unknown;
|
||||
for (const cred of credentials) {
|
||||
const user = encodeURIComponent(cred.username);
|
||||
const password = encodeURIComponent(cred.password);
|
||||
const serverUrl = `mysql://${user}:${password}@${dto.mysql.host}:${dto.mysql.port}/information_schema`;
|
||||
const prisma = new PrismaClient({ datasources: { db: { url: serverUrl } } });
|
||||
try {
|
||||
await prisma.$connect();
|
||||
await prisma.$executeRawUnsafe(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`,
|
||||
);
|
||||
return;
|
||||
} catch (e) {
|
||||
lastError = e;
|
||||
} finally {
|
||||
await prisma.$disconnect().catch(() => undefined);
|
||||
}
|
||||
}
|
||||
throw new Error(
|
||||
`自动创建数据库「${db}」失败:${(lastError as Error)?.message ?? '未知错误'}。` +
|
||||
`请确认账号具有建库权限,或在安装页填写“建库管理员账号”;` +
|
||||
`也可手动执行:CREATE DATABASE \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;`,
|
||||
);
|
||||
}
|
||||
|
||||
private async assertRedis(dto: InstallRequestDto): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user