feat: 迁移自定义功能至 1.5.9.9 版本
包含音乐源限制、CI 防 OOM 优化、数据库连接池优化、管理端权限及 UI 细节定制等功能
This commit is contained in:
@@ -1,43 +1,31 @@
|
||||
import { db } from '~/drizzle/db'
|
||||
import { sql } from 'drizzle-orm'
|
||||
|
||||
function getFirstRow<T>(result: any): T | undefined {
|
||||
return result?.rows?.[0] ?? result?.[0]
|
||||
}
|
||||
|
||||
function quoteIdentifierPart(value: string) {
|
||||
const normalized =
|
||||
value.startsWith('"') && value.endsWith('"') ? value.slice(1, -1).replace(/""/g, '"') : value
|
||||
|
||||
return `"${normalized.replace(/"/g, '""')}"`
|
||||
}
|
||||
|
||||
function toQualifiedIdentifier(value: string) {
|
||||
return sql.raw(value.split('.').map(quoteIdentifierPart).join('.'))
|
||||
}
|
||||
|
||||
function toPgRelationName(value: string) {
|
||||
return value.split('.').map(quoteIdentifierPart).join('.')
|
||||
}
|
||||
|
||||
function toRegclass(value: string) {
|
||||
return sql.raw(`'${value.replace(/'/g, "''")}'::regclass`)
|
||||
}
|
||||
|
||||
// 修复单个表的序列
|
||||
async function fixTableSequence(table: string, dbTableName: string) {
|
||||
try {
|
||||
// 获取表的最大ID
|
||||
const maxIdResult = await db.execute(
|
||||
sql`SELECT MAX(id) as max_id FROM ${toQualifiedIdentifier(dbTableName)}`
|
||||
)
|
||||
const maxId = Number(getFirstRow<{ max_id: number | string | null }>(maxIdResult)?.max_id || 0)
|
||||
const maxIdResult = await db.execute(sql.raw(`SELECT MAX(id) as max_id FROM "${dbTableName}"`))
|
||||
const maxId = Number((maxIdResult as any)[0]?.max_id || 0)
|
||||
|
||||
if (maxId === 0) {
|
||||
return {
|
||||
success: true,
|
||||
table: table,
|
||||
message: `表 ${table} 为空,无需修复序列`,
|
||||
data: {
|
||||
table: table,
|
||||
maxId: 0,
|
||||
skipped: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取序列名称
|
||||
const sequenceNameResult = await db.execute(
|
||||
sql`SELECT pg_get_serial_sequence(${toPgRelationName(dbTableName)}, 'id') as sequence_name`
|
||||
sql.raw(`SELECT pg_get_serial_sequence('"${dbTableName}"', 'id') as sequence_name`)
|
||||
)
|
||||
const sequenceName = getFirstRow<{ sequence_name: string | null }>(sequenceNameResult)?.sequence_name
|
||||
const sequenceName = (sequenceNameResult as any)[0]?.sequence_name
|
||||
|
||||
if (!sequenceName) {
|
||||
return {
|
||||
@@ -47,29 +35,29 @@ async function fixTableSequence(table: string, dbTableName: string) {
|
||||
}
|
||||
}
|
||||
|
||||
if (maxId === 0) {
|
||||
await db.execute(sql`ALTER SEQUENCE ${toQualifiedIdentifier(sequenceName)} RESTART WITH 1`)
|
||||
// 获取当前序列值
|
||||
const currentSeqResult = await db.execute(sql.raw(`SELECT last_value FROM ${sequenceName}`))
|
||||
const currentSeqValue = Number((currentSeqResult as any)[0]?.last_value || 0)
|
||||
|
||||
// 重置序列值到最大ID + 1
|
||||
const newSequenceValue = maxId + 1
|
||||
|
||||
// 检查序列是否已经正确
|
||||
if (currentSeqValue >= newSequenceValue) {
|
||||
return {
|
||||
success: true,
|
||||
table: table,
|
||||
message: `表 ${table} 为空,序列已重置为 1`,
|
||||
message: `表 ${table} 序列值已正确,无需修复`,
|
||||
data: {
|
||||
table: table,
|
||||
maxId: 0,
|
||||
newSequenceValue: 1,
|
||||
sequenceName: sequenceName
|
||||
maxId: maxId,
|
||||
currentSequenceValue: currentSeqValue,
|
||||
alreadyCorrect: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const currentSeqResult = await db.execute(
|
||||
sql`SELECT last_value FROM ${toQualifiedIdentifier(sequenceName)}`
|
||||
)
|
||||
const currentSeqValue = Number(
|
||||
getFirstRow<{ last_value: number | string }>(currentSeqResult)?.last_value || 0
|
||||
)
|
||||
const newSequenceValue = maxId
|
||||
await db.execute(sql`SELECT setval(${toRegclass(sequenceName)}, ${newSequenceValue})`)
|
||||
await db.execute(sql.raw(`SELECT setval('${sequenceName}', ${newSequenceValue})`))
|
||||
|
||||
return {
|
||||
success: true,
|
||||
@@ -99,7 +87,7 @@ export default defineEventHandler(async (event) => {
|
||||
if (!user || !['ADMIN', 'SUPER_ADMIN'].includes(user.role)) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
message: '需要管理员权限'
|
||||
statusMessage: '需要管理员权限'
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user