-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdrop.ts
More file actions
46 lines (42 loc) · 1.33 KB
/
drop.ts
File metadata and controls
46 lines (42 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { exit } from 'node:process';
import { db } from '@/database.providers';
import { articles, comments, favoriteArticles } from '@articles/articles.model';
import dbConfig from '@db/config';
import { articleTags, tags } from '@tags/tags.model';
import { userFollows, users } from '@users/users.model';
import { getTableName, sql } from 'drizzle-orm';
const tables = [
articleTags,
tags,
userFollows,
favoriteArticles,
comments,
articles,
users,
];
console.log('Dropping all tables from the database');
try {
// Use a transaction to ensure all deletions succeed or none do
await db.transaction(async (tx) => {
for (const table of tables) {
const name = getTableName(table);
console.log(`Dropping ${name}`);
await tx.execute(
sql`DROP TABLE IF EXISTS ${sql.identifier(name)} CASCADE;`,
);
console.log(`Dropped ${name}`);
}
if (dbConfig.migrations?.table) {
// Clean up migrations
console.log('Dropping migrations table: ', dbConfig.migrations.table);
await tx.execute(
sql`DROP TABLE IF EXISTS ${sql.identifier(dbConfig.migrations.schema ?? 'public')}.${sql.identifier(dbConfig.migrations.table)} CASCADE;`,
);
}
});
console.log('All tables dropped');
exit(0);
} catch (error) {
console.error('Failed to drop tables:', error);
exit(1);
}