/ opencodehub / scripts / reset-database.cjs
Swadhin Biswas Feature/ai code reviewer (#73) · 8/2/2026
105a287
65 lines · 1.7 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * Reset Database Script
 * 
 * Drops all tables and recreates the schema from scratch
 * 
 * Usage: node scripts/reset-database.cjs
 */

const { drizzle } = require('drizzle-orm/postgres-js');
const postgres = require('postgres');
const fs = require('fs');
const path = require('path');

// Load .env file manually
const envPath = path.join(__dirname, '..', '.env');
const envFile = fs.readFileSync(envPath, 'utf8');
envFile.split('\n').forEach(line => {
    const match = line.match(/^([^#=]+)=(.*)$/);
    if (match) {
        const key = match[1].trim();
        const value = match[2].trim().replace(/^["']|["']$/g, '');
        process.env[key] = value;
    }
});

async function resetDatabase() {
    const connectionString = process.env.DATABASE_URL;

    console.log('🗑️  Resetting database...\n');

    const sql = postgres(connectionString, { max: 1 });
    const db = drizzle(sql);
    void db;

    try {
        // Get all tables
        const tables = await sql`
      SELECT tablename 
      FROM pg_tables 
      WHERE schemaname = 'public'
    `;

        console.log(`Found ${tables.length} tables to drop:\n`);
        tables.forEach(t => console.log(`  - ${t.tablename}`));

        // Drop all tables
        for (const table of tables) {
            console.log(`\nDropping table: ${table.tablename}`);
            await sql.unsafe(`DROP TABLE IF EXISTS "${table.tablename}" CASCADE`);
        }

        console.log('\n✅ All tables dropped successfully!');
        console.log('\nNow run: npm run db:push');
        console.log('to recreate the schema\n');

    } catch (error) {
        console.error('❌ Error resetting database:', error);
        process.exit(1);
    } finally {
        await sql.end();
    }
}

resetDatabase();