Skip to content

Commit a00b84e

Browse files
committed
[skip ci] improve db clean
1 parent 391cd7f commit a00b84e

File tree

2 files changed

+95
-24
lines changed

2 files changed

+95
-24
lines changed

bin/admin-clear-db.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const axios = require('axios')
22

3-
// const adminUser = process.env.ADMIN_USER
4-
// const adminPass = process.env.ADMIN_PASS
5-
// const url = process.env.ADMIN_INIT_DOMAIN
3+
const adminUser = process.env.ADMIN_USER
4+
const adminPass = process.env.ADMIN_PASS
5+
const url = process.env.ADMIN_INIT_DOMAIN
66

7-
const adminUser = process.env.ADMIN_USER_PROD
8-
const adminPass = process.env.ADMIN_PASS_PROD
9-
const url = process.env.ADMIN_INIT_DOMAIN_PROD
7+
// const adminUser = process.env.ADMIN_USER_PROD
8+
// const adminPass = process.env.ADMIN_PASS_PROD
9+
// const url = process.env.ADMIN_INIT_DOMAIN_PROD
1010
// const adminGithubLogin = process.env.ADMIN_GITHUB_LOGIN
1111

1212
const apiUrl = `${url}/api/admin` // Replace with your actual domain

bin/admin-db-clean.js

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,26 @@ async function cleanDb () {
2121
}).then(res => {
2222
return res.data
2323
})
24-
const tokenIds = users.reduce((acc, user) => {
25-
return acc.concat(user.tokenIds.split(','))
24+
// Identify users without tokens and mark them for deletion
25+
const usersToDelete = []
26+
const validUsers = []
27+
28+
for (const user of users) {
29+
const userTokenIds = user.tokenIds.split(',').filter(t => t !== '')
30+
if (userTokenIds.length === 0) {
31+
usersToDelete.push(user.id)
32+
console.log('User without tokens to be deleted:', user.id)
33+
} else {
34+
validUsers.push(user)
35+
}
36+
}
37+
38+
const tokenIds = validUsers.reduce((acc, user) => {
39+
return acc.concat(user.tokenIds.split(',').filter(t => t !== ''))
2640
}, [])
2741
console.log('tokenIds', tokenIds)
28-
const dataIds = users.reduce((acc, user) => {
29-
return acc.concat(user.dataIds.split(','))
42+
const dataIds = validUsers.reduce((acc, user) => {
43+
return acc.concat(user.dataIds.split(',').filter(d => d !== ''))
3044
}, [])
3145
console.log('dataIds', dataIds)
3246
const tokens = await axios.post(apiUrl, {
@@ -42,25 +56,80 @@ async function cleanDb () {
4256
}).then(res => {
4357
return res.data
4458
})
45-
console.log('tokens count', tokens.length)
59+
let tokenCount = tokens.length
60+
const userCount = validUsers.length // Use validUsers count instead of all users
61+
console.log('valid user count', userCount)
62+
console.log('users to delete count', usersToDelete.length)
63+
64+
// Delete users without tokens first
65+
for (const userId of usersToDelete) {
66+
console.log('deleting user id', userId)
67+
await axios.post(apiUrl, {
68+
func: 'delete',
69+
tableName: 'User',
70+
params: [userId]
71+
}, {
72+
proxy: false,
73+
auth: {
74+
username: adminUser,
75+
password: adminPass
76+
}
77+
}).catch(err => {
78+
console.log(err.message)
79+
})
80+
}
81+
82+
await axios.post(apiUrl, {
83+
func: 'update',
84+
tableName: 'Statics',
85+
params: [{ id: 'userCount', value: userCount }]
86+
}, {
87+
proxy: false,
88+
auth: {
89+
username: adminUser,
90+
password: adminPass
91+
}
92+
}).catch(err => {
93+
console.log(err.message)
94+
})
95+
const tokensTOdel = []
4696
for (const token of tokens) {
4797
console.log('token id', token.id)
4898
if (!tokenIds.includes(token.id)) {
49-
await axios.post(apiUrl, {
50-
func: 'delete',
51-
tableName: 'Token',
52-
params: [token.id]
53-
}, {
54-
proxy: false,
55-
auth: {
56-
username: adminUser,
57-
password: adminPass
58-
}
59-
}).catch(err => {
60-
console.log(err.message)
61-
})
99+
tokensTOdel.push(token.id)
100+
tokenCount--
62101
}
63102
}
103+
console.log('token count', tokenCount)
104+
await axios.post(apiUrl, {
105+
func: 'update',
106+
tableName: 'Statics',
107+
params: [{ id: 'tokenCount', value: tokenCount }]
108+
}, {
109+
proxy: false,
110+
auth: {
111+
username: adminUser,
112+
password: adminPass
113+
}
114+
}).catch(err => {
115+
console.log(err.message)
116+
})
117+
for (const token of tokensTOdel) {
118+
console.log('token id', token)
119+
await axios.post(apiUrl, {
120+
func: 'delete',
121+
tableName: 'Token',
122+
params: [token]
123+
}, {
124+
proxy: false,
125+
auth: {
126+
username: adminUser,
127+
password: adminPass
128+
}
129+
}).catch(err => {
130+
console.log(err.message)
131+
})
132+
}
64133
const datas = await axios.post(apiUrl, {
65134
func: 'list',
66135
tableName: 'Data',
@@ -75,6 +144,8 @@ async function cleanDb () {
75144
return res.data
76145
})
77146
console.log('datas count', datas.length)
147+
148+
// Clean up orphaned data (data not referenced by valid users)
78149
for (const data of datas) {
79150
if (!dataIds.includes(data.id)) {
80151
await axios.post(apiUrl, {

0 commit comments

Comments
 (0)