1 parent
d92dfdacff
commit
f68c367042
34 files changed
+2121
-74
No files matched your search
@@ -41,7 +41,7 @@ const baseGroups = [
|
||||
field('domains.accessHosts', '额外允许的访问主机', 'list', { hint: '按需填写域名或 IP 地址,不填写则保持默认访问范围。', placeholder: 'mail.example.com' }),
|
||||
field('logLevel', '日志级别', 'select', { options: options([['', '默认级别'], ['debug', '调试'], ['info', '信息'], ['warn', '警告'], ['error', '错误']]) })
|
||||
]),
|
||||
group('database', '数据库连接', '数据库类型保持不变;修改数据库名称或文件请使用右侧“数据库迁移”,避免切换到空数据库。', [
|
||||
group('database', '数据库连接', '修改数据库类型、名称或文件请使用右侧“数据库迁移”,支持 SQLite、MySQL、PostgreSQL,避免切换到空数据库。', [
|
||||
field('database.type', '数据库类型', 'select', { locked: true, options: options([['sqlite', 'SQLite(本地文件)'], ['mysql', 'MySQL'], ['postgresql', 'PostgreSQL']]) }),
|
||||
field('database.localRef', '数据库文件', 'text', { locked: true, show: config => !networkDatabase(config) }),
|
||||
field('database.name', '数据库名称', 'text', { locked: true, show: networkDatabase }),
|
||||
|
||||
+35
-6
@@ -12,7 +12,9 @@
|
||||
<section v-if="pageKey === 'migration' && canMigrate" class="maintenance-panel migration-panel">
|
||||
<div class="maintenance-body">
|
||||
<el-alert v-if="sectionError" :title="sectionError" type="error" :closable="false" show-icon />
|
||||
<p class="panel-desc">系统先生成一次性预览票据,再复制并校验数据。主机、端口、用户名和密码保持不变,旧数据库不会自动删除。</p>
|
||||
<p class="panel-desc">支持 SQLite、MySQL、PostgreSQL 之间迁移。先验证目标连接,再复制并逐表校验数据;旧数据库不会删除。迁移期间暂停写入,成功后必须立即重启后端。</p>
|
||||
<el-alert v-if="status.pendingRestart?.length" title="当前配置等待重启,请先重启后端再发起新的迁移。" type="warning" :closable="false" show-icon />
|
||||
<el-alert v-if="migrationError" :title="migrationError" type="error" :closable="false" show-icon />
|
||||
<div class="migration-source">
|
||||
<div>
|
||||
<span>当前数据库类型</span>
|
||||
@@ -24,19 +26,43 @@
|
||||
</div>
|
||||
</div>
|
||||
<el-form label-position="top" class="migration-form">
|
||||
<el-form-item :label="databaseConfig.type === 'sqlite' ? '目标数据库文件引用' : '目标数据库名称'">
|
||||
<el-form-item label="目标数据库类型">
|
||||
<el-radio-group :model-value="migrationTarget.type" :disabled="!!working || !!sectionError" @update:model-value="changeMigrationType">
|
||||
<el-radio-button label="sqlite">SQLite</el-radio-button>
|
||||
<el-radio-button label="mysql">MySQL</el-radio-button>
|
||||
<el-radio-button label="postgresql">PostgreSQL</el-radio-button>
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
<div v-if="migrationTarget.type !== 'sqlite'" class="migration-connection-grid">
|
||||
<el-form-item label="数据库主机" required>
|
||||
<el-input v-model="migrationTarget.host" placeholder="例如 127.0.0.1 或数据库域名" :disabled="!!working" maxlength="253" @update:model-value="invalidateMigration" />
|
||||
</el-form-item>
|
||||
<el-form-item label="数据库端口" required>
|
||||
<el-input-number v-model="migrationTarget.port" :min="1" :max="65535" :precision="0" :disabled="!!working" @update:model-value="invalidateMigration" />
|
||||
</el-form-item>
|
||||
<el-form-item label="数据库用户名" required>
|
||||
<el-input v-model="migrationTarget.username" autocomplete="off" placeholder="具有创建数据库权限的账号" :disabled="!!working" maxlength="128" @update:model-value="invalidateMigration" />
|
||||
</el-form-item>
|
||||
<el-form-item label="数据库密码" required>
|
||||
<el-input v-model="migrationTarget.password" type="password" autocomplete="new-password" placeholder="填写目标连接密码,不会回显旧密码" :disabled="!!working" maxlength="4096" @update:model-value="invalidateMigration" />
|
||||
</el-form-item>
|
||||
</div>
|
||||
<el-form-item :label="migrationTarget.type === 'sqlite' ? '目标数据库文件名' : '目标数据库名称'" required>
|
||||
<el-input
|
||||
v-model="migrationTargetName"
|
||||
:placeholder="databaseConfig.type === 'sqlite' ? '例如 smail-next.db' : '例如 smail_next'"
|
||||
:placeholder="migrationTarget.type === 'sqlite' ? '例如 smail-next.db' : '例如 smail_next'"
|
||||
:disabled="!!working || !!sectionError"
|
||||
maxlength="128"
|
||||
clearable
|
||||
@update:model-value="invalidateMigration"
|
||||
/>
|
||||
</el-form-item>
|
||||
<p class="panel-desc">{{ migrationTarget.type === 'sqlite' ? '文件固定存放在运行目录 config 下,只填写新文件名,不填写路径。' : '请使用尚不存在的库名,系统执行时自动建库。新连接必须填写密码,不会复用源数据库密码。' }}</p>
|
||||
<p v-if="migrationTarget.type === 'postgresql'" class="panel-desc">PostgreSQL 连接要求数据库服务启用 TLS 加密,与迁移后的运行连接保持一致。</p>
|
||||
</el-form>
|
||||
<div class="action-row">
|
||||
<el-button :loading="working === 'migration-preview'" :disabled="!!working || !!sectionError || !migrationTargetName.trim()" @click="previewDatabaseMigration">
|
||||
生成迁移预览
|
||||
<el-button :loading="working === 'migration-preview'" :disabled="!!working || !!sectionError || !migrationReady" @click="previewDatabaseMigration">
|
||||
验证连接并预览
|
||||
</el-button>
|
||||
<el-button type="primary" :loading="working === 'migration-execute'" :disabled="!!working || !migrationPreview || !migrationToken" @click="executeDatabaseMigration">
|
||||
执行迁移
|
||||
@@ -52,7 +78,9 @@
|
||||
</div>
|
||||
<span>迁移编号:{{ migrationRecord.id }}</span>
|
||||
<span>源数据库:{{ migrationRecord.source?.name || migrationRecord.source?.localRef || '-' }}</span>
|
||||
<span>目标数据库:{{ migrationRecord.target?.name || migrationRecord.target?.localRef || '-' }}</span>
|
||||
<span>目标数据库:{{ databaseTypeLabel(migrationRecord.target?.type) }} · {{ migrationRecord.target?.name || migrationRecord.target?.localRef || '-' }}</span>
|
||||
<span v-if="migrationRecord.target?.host">目标连接:{{ migrationRecord.target.host }}:{{ migrationRecord.target.port }} · {{ migrationRecord.target.username }}</span>
|
||||
<el-alert v-if="migrationRecord.status === 'succeeded'" title="迁移完成,旧库已暂停写入。请立即重启后端启用新数据库;若网页提示近期认证已过期,请在部署端重启,不要重复迁移。" type="warning" :closable="false" show-icon />
|
||||
<span v-if="migrationRecord.errorCode" class="migration-error">错误代码:{{ migrationRecord.errorCode }}</span>
|
||||
</div>
|
||||
</div>
|
||||
@@ -160,6 +188,7 @@ const {
|
||||
loading, availabilityError, working, status, refreshPage, beforeLeave, sectionError,
|
||||
canMigrate, databaseConfig, databaseTypeLabel, migrationTargetName, migrationPreview,
|
||||
migrationToken, migrationRecord, migrationStatusType, migrationStatusLabel,
|
||||
migrationTarget, migrationReady, migrationError, invalidateMigration, changeMigrationType,
|
||||
previewDatabaseMigration, executeDatabaseMigration, loadMigrationStatus,
|
||||
canRestart, serviceLabel, restartPreview, previewRestart, requestRestart,
|
||||
canRollback, backupsLoading, backupsError, backups, rollbackGeneration, rollbackConfirmed,
|
||||
|
||||
@@ -471,8 +471,22 @@
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.migration-form {
|
||||
max-width: 880px;
|
||||
margin-top: 16px;
|
||||
}
|
||||
.migration-connection-grid {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
|
||||
gap: 0 20px;
|
||||
}
|
||||
.migration-connection-grid :deep(.el-input-number) {
|
||||
width: 100%;
|
||||
}
|
||||
@container config-main (max-width: 620px) {
|
||||
.migration-connection-grid {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
}
|
||||
.migration-form :deep(.el-form-item) {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ const sections = [
|
||||
]
|
||||
|
||||
const serviceLabels = {
|
||||
database: '数据库服务',
|
||||
smtp: 'SMTP 服务',
|
||||
email: '邮箱服务',
|
||||
ssl: 'SSL 服务',
|
||||
@@ -68,9 +69,43 @@ export function usePage(pageKey = 'base') {
|
||||
const auditItems = ref([])
|
||||
const databaseConfig = reactive({ type: '', name: '', localRef: '' })
|
||||
const migrationTargetName = ref('')
|
||||
const migrationTarget = reactive({ type: 'sqlite', host: '127.0.0.1', port: 3306, username: '', password: '' })
|
||||
const migrationError = ref('')
|
||||
let migrationInput = ''
|
||||
const migrationPreview = ref(null)
|
||||
const migrationToken = ref('')
|
||||
const migrationRecord = ref(null)
|
||||
const invalidateMigration = () => {
|
||||
migrationPreview.value = null
|
||||
migrationToken.value = ''
|
||||
migrationInput = ''
|
||||
migrationError.value = ''
|
||||
}
|
||||
const changeMigrationType = type => {
|
||||
migrationTarget.type = type
|
||||
migrationTarget.port = type === 'postgresql' ? 5432 : 3306
|
||||
migrationTarget.password = ''
|
||||
migrationTargetName.value = ''
|
||||
invalidateMigration()
|
||||
}
|
||||
const migrationPayload = () => migrationTarget.type === 'sqlite'
|
||||
? { type: 'sqlite', localRef: migrationTargetName.value.trim() }
|
||||
: {
|
||||
type: migrationTarget.type,
|
||||
host: migrationTarget.host.trim(),
|
||||
port: Number(migrationTarget.port),
|
||||
username: migrationTarget.username.trim(),
|
||||
name: migrationTargetName.value.trim(),
|
||||
password: { plaintext: migrationTarget.password }
|
||||
}
|
||||
const migrationReady = computed(() => {
|
||||
if (!migrationTargetName.value.trim() || status.pendingRestart?.length) return false
|
||||
if (migrationTarget.type === 'sqlite') return /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(migrationTargetName.value.trim())
|
||||
return ['mysql', 'postgresql'].includes(migrationTarget.type) &&
|
||||
/^[A-Za-z][A-Za-z0-9_-]{0,62}$/.test(migrationTargetName.value.trim()) &&
|
||||
!!migrationTarget.host.trim() && !!migrationTarget.username.trim() && !!migrationTarget.password &&
|
||||
Number.isInteger(Number(migrationTarget.port)) && Number(migrationTarget.port) > 0 && Number(migrationTarget.port) <= 65535
|
||||
})
|
||||
const restartPreview = ref(null)
|
||||
const hasChanges = computed(() => editorText.value !== loadedText.value || !!secretValue.value || !!savedGeneration.value)
|
||||
|
||||
@@ -219,22 +254,27 @@ export function usePage(pageKey = 'base') {
|
||||
}
|
||||
|
||||
const previewDatabaseMigration = async () => {
|
||||
if (!canMigrate.value || working.value || sectionError.value || !migrationTargetName.value.trim()) return
|
||||
if (!canMigrate.value || working.value || sectionError.value || !migrationReady.value) return
|
||||
working.value = 'migration-preview'
|
||||
migrationPreview.value = null
|
||||
migrationRecord.value = null
|
||||
migrationToken.value = ''
|
||||
migrationError.value = ''
|
||||
try {
|
||||
const target = migrationPayload()
|
||||
const response = await http.post('/api/v1/database/migrations/preview', {
|
||||
targetName: migrationTargetName.value.trim(),
|
||||
target,
|
||||
expectedGeneration: Number(status.activeGeneration)
|
||||
})
|
||||
if (disposed) return
|
||||
notifyResponseError(response, '数据库迁移预览失败')
|
||||
migrationPreview.value = response.data?.migration || null
|
||||
migrationRecord.value = migrationPreview.value
|
||||
migrationToken.value = response.data?.token || ''
|
||||
ElMessage.success('数据库迁移预览已生成,请确认目标名称后执行')
|
||||
migrationInput = JSON.stringify(target)
|
||||
ElMessage.success('目标连接验证通过,请核对数据库类型和连接后执行')
|
||||
} catch (error) {
|
||||
migrationError.value = error.message || '数据库迁移预览失败'
|
||||
ElMessage.error(error.message || '数据库迁移预览失败')
|
||||
} finally {
|
||||
working.value = ''
|
||||
@@ -257,10 +297,15 @@ export function usePage(pageKey = 'base') {
|
||||
|
||||
const executeDatabaseMigration = async () => {
|
||||
if (!canMigrate.value || working.value || !migrationPreview.value?.id || !migrationToken.value) return
|
||||
if (migrationInput !== JSON.stringify(migrationPayload())) {
|
||||
invalidateMigration()
|
||||
migrationError.value = '目标连接已修改,请重新生成迁移预览'
|
||||
return
|
||||
}
|
||||
working.value = 'migration-confirm'
|
||||
try {
|
||||
await ElMessageBox.confirm(
|
||||
'迁移将复制数据库内容并切换配置,旧数据库会保留。确认继续吗?',
|
||||
`将迁移到 ${databaseTypeLabel(migrationPreview.value.target?.type)}:${migrationPreview.value.target?.name || migrationPreview.value.target?.localRef || migrationTargetName.value.trim()}。迁移期间暂停写入,成功后必须立即重启后端。旧数据库及失败目标均保留。确认继续吗?`,
|
||||
'确认执行数据库迁移',
|
||||
{ type: 'warning', confirmButtonText: '继续迁移', cancelButtonText: '取消' }
|
||||
)
|
||||
@@ -268,19 +313,29 @@ export function usePage(pageKey = 'base') {
|
||||
const response = await http.post('/api/v1/database/migrations', {
|
||||
id: migrationPreview.value.id,
|
||||
token: migrationToken.value
|
||||
})
|
||||
}, { timeout: 11 * 60 * 1000 })
|
||||
notifyResponseError(response, '数据库迁移执行失败')
|
||||
migrationRecord.value = response.data || migrationPreview.value
|
||||
migrationToken.value = ''
|
||||
migrationPreview.value = null
|
||||
migrationTarget.password = ''
|
||||
migrationInput = ''
|
||||
await Promise.all([loadStatus(), loadSection()])
|
||||
if (migrationRecord.value.status === 'succeeded') {
|
||||
ElMessage.success('数据库迁移已完成,旧数据库仍然保留')
|
||||
ElMessage.success('数据已复制校验并切换配置,请立即重启后端以恢复写入;旧数据库保留')
|
||||
} else {
|
||||
ElMessage.warning(`数据库迁移${migrationStatusLabel(migrationRecord.value.status)}`)
|
||||
}
|
||||
} catch (error) {
|
||||
if (error !== 'cancel' && error !== 'close') ElMessage.error(error.message || '数据库迁移执行失败')
|
||||
if (error !== 'cancel' && error !== 'close') {
|
||||
migrationToken.value = ''
|
||||
migrationPreview.value = null
|
||||
migrationTarget.password = ''
|
||||
migrationInput = ''
|
||||
migrationError.value = error.message || '数据库迁移执行失败'
|
||||
await loadMigrationStatus()
|
||||
ElMessage.error(migrationError.value)
|
||||
}
|
||||
} finally {
|
||||
working.value = ''
|
||||
}
|
||||
@@ -321,6 +376,8 @@ export function usePage(pageKey = 'base') {
|
||||
}
|
||||
}
|
||||
const loadAll = async () => {
|
||||
invalidateMigration()
|
||||
migrationTarget.password = ''
|
||||
loading.value = true
|
||||
availabilityError.value = ''
|
||||
try {
|
||||
@@ -539,6 +596,8 @@ export function usePage(pageKey = 'base') {
|
||||
savedInput = null
|
||||
confirmed.value = false
|
||||
migrationToken.value = ''
|
||||
migrationTarget.password = ''
|
||||
migrationInput = ''
|
||||
})
|
||||
|
||||
return {
|
||||
@@ -551,6 +610,7 @@ export function usePage(pageKey = 'base') {
|
||||
loadBackups, previewRollback, executeRollback, loadAudit, exportAudit, formatTime,
|
||||
serviceLabel, sectionLabel, operationLabel, databaseConfig, databaseTypeLabel, canMigrate,
|
||||
migrationTargetName, migrationPreview, migrationToken, migrationRecord, migrationStatusLabel, migrationStatusType,
|
||||
migrationTarget, migrationReady, migrationError, invalidateMigration, changeMigrationType,
|
||||
previewDatabaseMigration, loadMigrationStatus, executeDatabaseMigration, canRestart, restartPreview,
|
||||
previewRestart, requestRestart, beforeLeave, refreshPage, hasChanges
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user