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/util/licenses/check-disk-name-uniqueness.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// NOTE: you might wonder, why to check for unique names? wouldn't something random be fine?
7
// well, there is a case where more than one disk is mounted in a project.
8
// By tying the name to the license, it is always clear which disk is which.
9
10
const Q_EXISTS_DISK = `
11
SELECT EXISTS(
12
SELECT 1
13
FROM site_licenses
14
WHERE quota -> 'dedicated_disk' IS NOT NULL
15
AND quota -> 'dedicated_disk' ->> 'name' = $1
16
)`;
17
18
export async function checkDedicateDiskNameUniqueness(
19
pool,
20
name?: string
21
): Promise<{ available: boolean }> {
22
if (typeof name !== "string") {
23
throw new Error(`name must be a string`);
24
}
25
const { rows } = await pool.query(Q_EXISTS_DISK, [name]);
26
if (rows[0].exists) {
27
throw new Error(`Disk name ${name} is already taken`);
28
}
29
30
return { available: true };
31
}
32
33