CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/database/postgres/schema/drop-deprecated-tables.ts
Views: 687
1
/*
2
Drop ancient deprecated tables in some cases.
3
4
This doesn't work via any generic schema, but is some very specific code.
5
*/
6
7
import type { Client } from "@cocalc/database/pool";
8
9
export async function dropDeprecatedTables(db: Client) {
10
// There was a table compute_servers used from 2013-2016 with a primary key host.
11
// We drop that table from the database if it exists *with that primary key*.
12
// During sync we will then create the new compute_servers table, which has
13
// primary key "id", is modern, and does something completely different.
14
15
const result = await db.query(`SELECT EXISTS (
16
SELECT FROM pg_class c
17
JOIN pg_namespace n on n.oid = c.relnamespace
18
WHERE c.relkind = 'r'
19
AND n.nspname = 'public'
20
AND c.relname = 'compute_servers'
21
AND exists (
22
SELECT 1
23
FROM pg_attribute attr
24
JOIN pg_index idx on idx.indrelid = attr.attrelid
25
and idx.indkey[0] = attr.attnum
26
WHERE idx.indrelid = c.oid
27
AND idx.indisprimary
28
AND attr.attname = 'host'
29
));`);
30
31
if (result.rows[0].exists) {
32
await db.query(`DROP TABLE compute_servers;`);
33
}
34
}
35
36