-
-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d60a58c
commit f140b76
Showing
3 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { config } from 'dotenv'; | ||
import { program } from 'commander'; | ||
import { join } from 'path'; | ||
import ora from 'ora'; | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
config(); | ||
|
||
program | ||
.requiredOption('-y, --yes', 'ARE YOU SURE?') | ||
.option('-d, --days <number>', 'number of days', 365); | ||
|
||
program.parse(); | ||
|
||
const options = program.opts(); | ||
|
||
let spinner = ora('Connecting').start(); | ||
|
||
const prisma_options = {}; | ||
|
||
if (process.env.DB_PROVIDER === 'sqlite' && !process.env.DB_CONNECTION_URL) { | ||
prisma_options.datasources = { db: { url: 'file:' + join(process.cwd(), './user/database.db') } }; | ||
} | ||
|
||
const prisma = new PrismaClient(prisma_options); | ||
|
||
if (process.env.DB_PROVIDER === 'sqlite') { | ||
const { default: sqliteMiddleware } = await import('../src/lib/middleware/prisma-sqlite.js'); | ||
prisma.$use(sqliteMiddleware); | ||
await prisma.$queryRaw`PRAGMA journal_mode=WAL;`; | ||
await prisma.$queryRaw`PRAGMA synchronous=normal;`; | ||
} | ||
|
||
spinner.succeed('Connected'); | ||
|
||
const now = Date.now(); | ||
const elapsed = 1000 * 60 * 60 * 24 * options.days; | ||
const cutoff = now - elapsed; | ||
|
||
spinner = ora('Counting total guilds').start(); | ||
const total = await prisma.guild.count(); | ||
spinner.succeed(`Found ${total} total guilds`); | ||
|
||
// ! the bot might still be in these guilds | ||
spinner = ora(`Deleting guilds inactive for more than ${options.days} days`).start(); | ||
const result = await prisma.guild.deleteMany({ where: { tickets: { none: { createdAt: { gt: new Date(cutoff) } } } } }); | ||
spinner.succeed(`Deleted ${result.count} guilds; ${total - result.count} remaining`); | ||
|
||
process.exit(0); |