Migrate architecture to PostgreSQL for Swarm HA

This commit is contained in:
ypratama committed 2026-08-12 14:32:00 +07:00
1 parent ee28968a70
commit 2317f9f133
39 files changed
+3849 -10

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
const { DatabaseSync } = require('node:sqlite');
const db = new DatabaseSync('dev.db');
try {
// 1. Delete all ApprovalLogs
db.exec('DELETE FROM ApprovalLog');
console.log('Cleared all ApprovalLogs');
// 2. Delete all Requests
db.exec('DELETE FROM Request');
console.log('Cleared all Requests');
// 3. Delete ActivityLogs
db.exec('DELETE FROM ActivityLog');
console.log('Cleared all ActivityLogs');
// 4. Reset all Item quantities to 0
db.exec('UPDATE Item SET totalQuantity = 0, borrowedQty = 0, trialQty = 0, billedQty = 0, availableQty = 0, damagedQty = 0, soldQty = 0');
console.log('Reset all item quantities to 0');
console.log('Done!');
} catch (err) {
console.error('Error:', err);
} finally {
db.close();
}
+45
View File
@@ -0,0 +1,45 @@
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient({
datasources: {
db: {
url: 'file:dev.db'
}
}
})
async function main() {
console.log('Clearing data...')
// 1. Delete all ApprovalLogs
await prisma.approvalLog.deleteMany({})
console.log('Cleared all ApprovalLogs')
// 2. Delete all Requests
await prisma.request.deleteMany({})
console.log('Cleared all Requests')
// 3. Delete ActivityLogs to clear history page completely
await prisma.activityLog.deleteMany({})
console.log('Cleared all ActivityLogs')
// 4. Reset all Item quantities to 0
await prisma.item.updateMany({
data: {
totalQuantity: 0,
borrowedQty: 0,
trialQty: 0,
billedQty: 0,
availableQty: 0,
damagedQty: 0,
soldQty: 0,
}
})
console.log('Reset all item quantities to 0')
console.log('Done!')
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect())
+80
View File
@@ -0,0 +1,80 @@
import { PrismaClient } from '@prisma/client'
import { PrismaLibSql } from '@prisma/adapter-libsql'
const adapter = new PrismaLibSql({
url: 'file:dev.db',
})
const prisma = new PrismaClient({ adapter })
async function main() {
const items = await prisma.item.findMany({
include: { requests: { where: { status: 'approved_2' } } }
})
let updatedCount = 0;
for (const item of items) {
let borrowedQty = 0;
let trialQty = 0;
let billedQty = 0;
let soldQty = 0;
let damagedQty = 0;
for (const req of item.requests) {
const qty = req.quantity;
let type = req.type.toLowerCase();
if (type === 'manage service') type = 'billed';
if (type === 'outright purchase') type = 'outright_purchase';
if (type !== req.type) {
await prisma.request.update({ where: { id: req.id }, data: { type } });
}
// Borrowed
if (type === 'borrow') borrowedQty += qty;
if (type === 'return_borrow' || type === 'return_damaged_borrow') borrowedQty -= qty;
// Trial
if (type === 'trial') trialQty += qty;
if (type === 'return_trial' || type === 'return_damaged_trial') trialQty -= qty;
// Billed
if (type === 'billed') billedQty += qty;
if (type === 'return_billed' || type === 'return_damaged_billed') billedQty -= qty;
// Sold
if (type === 'outright_purchase') soldQty += qty;
if (type === 'return_damaged_sold') soldQty -= qty;
// Damaged
if (type === 'return_damaged_borrow' || type === 'return_damaged_trial' || type === 'return_damaged_billed' || type === 'return_damaged_sold') {
damagedQty += qty;
}
}
borrowedQty = Math.max(0, borrowedQty);
trialQty = Math.max(0, trialQty);
billedQty = Math.max(0, billedQty);
soldQty = Math.max(0, soldQty);
damagedQty = Math.max(0, damagedQty);
const availableQty = Math.max(0, item.totalQuantity - borrowedQty - trialQty - billedQty - damagedQty - soldQty);
await prisma.item.update({
where: { id: item.id },
data: {
borrowedQty,
trialQty,
billedQty,
soldQty,
damagedQty,
availableQty
}
})
updatedCount++;
}
console.log(`Resynced ${updatedCount} items.`);
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect())