Skip to content

Commit 0a4d115

Browse files
committed
feat: add fixPostgresCollation command to CLI
- Introduced a new command to fix collation issues for the current PostgreSQL database.
1 parent d0cac82 commit 0a4d115

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/bin/cli/cli.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import { PrismaClient } from '@prisma/client';
3+
import { Prisma, PrismaClient } from '@prisma/client';
44
import consola from 'consola';
55

66
import { encodeCertPayload } from '@common/utils/certs/encode-node-payload';
@@ -16,6 +16,7 @@ const prisma = new PrismaClient({
1616

1717
const enum CLI_ACTIONS {
1818
EXIT = 'exit',
19+
FIX_POSTGRES_COLLATION = 'fix-postgres-collation',
1920
GET_SSL_CERT_FOR_NODE = 'get-ssl-cert-for-node',
2021
RESET_CERTS = 'reset-certs',
2122
RESET_SUPERADMIN = 'reset-superadmin',
@@ -139,6 +140,36 @@ async function getSslCertForNode() {
139140
}
140141
}
141142

143+
async function fixPostgresCollation() {
144+
consola.start('🔄 Fixing Collation...');
145+
146+
const answer = await consola.prompt('Are you sure you want to fix Collation?', {
147+
type: 'confirm',
148+
required: true,
149+
});
150+
151+
if (!answer) {
152+
consola.error('❌ Aborted.');
153+
process.exit(1);
154+
}
155+
156+
try {
157+
const result = await prisma.$queryRaw<
158+
{ dbname: string }[]
159+
>`SELECT current_database() as dbname;`;
160+
const dbName = result[0].dbname;
161+
162+
consola.info(`🔄 Refreshing Collation for database: ${dbName}`);
163+
164+
await prisma.$executeRaw`ALTER DATABASE ${Prisma.raw(dbName)} REFRESH COLLATION VERSION;`;
165+
consola.success('✅ Collation fixed successfully.');
166+
process.exit(0);
167+
} catch (error) {
168+
consola.error('❌ Failed to fix Collation:', error);
169+
process.exit(1);
170+
}
171+
}
172+
142173
async function main() {
143174
consola.box('Remnawave Rescue CLI v0.2');
144175

@@ -169,12 +200,17 @@ async function main() {
169200
label: 'Get SSL_CERT for a Remnawave Node',
170201
hint: 'Get SSL_CERT in cases, where you can not get from Panel',
171202
},
203+
{
204+
value: CLI_ACTIONS.FIX_POSTGRES_COLLATION,
205+
label: 'Fix Collation',
206+
hint: 'Fix Collation issues for current database',
207+
},
172208
{
173209
value: CLI_ACTIONS.EXIT,
174210
label: 'Exit',
175211
},
176212
],
177-
initial: CLI_ACTIONS.RESET_SUPERADMIN,
213+
initial: CLI_ACTIONS.EXIT,
178214
});
179215

180216
switch (action) {
@@ -187,6 +223,9 @@ async function main() {
187223
case CLI_ACTIONS.GET_SSL_CERT_FOR_NODE:
188224
await getSslCertForNode();
189225
break;
226+
case CLI_ACTIONS.FIX_POSTGRES_COLLATION:
227+
await fixPostgresCollation();
228+
break;
190229
case CLI_ACTIONS.EXIT:
191230
consola.info('👋 Exiting...');
192231
process.exit(0);

0 commit comments

Comments
 (0)