Path: blob/master/src/packages/util/licenses/check-disk-name-uniqueness.ts
1447 views
/*1* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// NOTE: you might wonder, why to check for unique names? wouldn't something random be fine?6// well, there is a case where more than one disk is mounted in a project.7// By tying the name to the license, it is always clear which disk is which.89const Q_EXISTS_DISK = `10SELECT EXISTS(11SELECT 112FROM site_licenses13WHERE quota -> 'dedicated_disk' IS NOT NULL14AND quota -> 'dedicated_disk' ->> 'name' = $115)`;1617export async function checkDedicateDiskNameUniqueness(18pool,19name?: string20): Promise<{ available: boolean }> {21if (typeof name !== "string") {22throw new Error(`name must be a string`);23}24const { rows } = await pool.query(Q_EXISTS_DISK, [name]);25if (rows[0].exists) {26throw new Error(`Disk name ${name} is already taken`);27}2829return { available: true };30}313233