File size: 1,282 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { rawDataSource } from 'src/database/typeorm/raw/raw.datasource';

import { performQuery } from './setup-db-utils';

async function dropSchemasSequentially() {
  try {
    await rawDataSource.initialize();

    // Fetch all schemas excluding the ones we want to keep
    const schemas =
      (await performQuery<{ schema_name: string }[]>(
        `
      SELECT n.nspname AS "schema_name"
      FROM pg_catalog.pg_namespace n
      WHERE n.nspname !~ '^pg_'
        AND n.nspname <> 'information_schema'
        AND n.nspname NOT IN ('metric_helpers', 'user_management', 'public')
    `,
        'Fetching schemas...',
      )) ?? [];

    const batchSize = 10;

    for (let i = 0; i < schemas.length; i += batchSize) {
      const batch = schemas.slice(i, i + batchSize);

      await Promise.all(
        batch.map((schema) =>
          performQuery(
            `DROP SCHEMA IF EXISTS "${schema.schema_name}" CASCADE;`,
            `Dropping schema ${schema.schema_name}...`,
          ),
        ),
      );
    }
    // oxlint-disable-next-line no-console
    console.log('All schemas dropped successfully.');
  } catch (err) {
    // oxlint-disable-next-line no-console
    console.error('Error during schema dropping:', err);
  }
}

void dropSchemasSequentially();