gongxiangp2/scripts/execute-sql-in-container.js
chenpei 0ce59be34e 备份:组织架构设计方案讨论前的代码快照
- 包含所有当前修改和新功能
- 准备开始组织架构优化实施方案
2025-12-30 13:20:26 +08:00

53 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
async function main() {
try {
// 检查字段是否已存在
const result = await prisma.$queryRaw`
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'material_skus'
AND column_name = 'deleted_at'
`
if (result.length === 0) {
console.log('添加 deleted_at 字段...')
await prisma.$executeRaw`
ALTER TABLE material_skus ADD COLUMN deleted_at TIMESTAMP
`
console.log('✅ deleted_at 字段已添加')
} else {
console.log('ℹ️ deleted_at 字段已存在')
}
// 检查索引是否已存在
const indexResult = await prisma.$queryRaw`
SELECT indexname
FROM pg_indexes
WHERE tablename = 'material_skus'
AND indexname = 'material_skus_deleted_at_idx'
`
if (indexResult.length === 0) {
console.log('添加 deleted_at 索引...')
await prisma.$executeRaw`
CREATE INDEX material_skus_deleted_at_idx ON material_skus(deleted_at)
`
console.log('✅ deleted_at 索引已添加')
} else {
console.log('ℹ️ deleted_at 索引已存在')
}
console.log('✅ 数据库迁移完成')
} catch (error) {
console.error('❌ 迁移失败:', error)
process.exit(1)
} finally {
await prisma.$disconnect()
}
}
main()