remove all drops from sql files, add triggers and functions for updated_at, added files and folder tables, update some types

This commit is contained in:
creations 2025-03-06 13:57:43 -05:00
parent 6fdc82dd49
commit 94ba46cc2d
Signed by: creations
GPG key ID: 8F553AA4320FC711
11 changed files with 295 additions and 101 deletions

View file

@ -1,6 +1,8 @@
import { logger } from "@helpers/logger";
import { type ReservedSQL, sql } from "bun";
export const order: number = 1;
export async function createTable(reservation?: ReservedSQL): Promise<void> {
let selfReservation: boolean = false;
@ -11,20 +13,20 @@ export async function createTable(reservation?: ReservedSQL): Promise<void> {
try {
await reservation`
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
authorization_token UUID NOT NULL UNIQUE DEFAULT gen_random_uuid(),
username VARCHAR(20) NOT NULL UNIQUE,
email VARCHAR(254) NOT NULL UNIQUE,
email_verified boolean NOT NULL DEFAULT false,
password TEXT NOT NULL,
avatar boolean NOT NULL DEFAULT false,
roles TEXT[] NOT NULL DEFAULT ARRAY['user'],
timezone VARCHAR(64) DEFAULT NULL,
invited_by UUID REFERENCES users(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
last_seen TIMESTAMPTZ DEFAULT NOW() NOT NULL
);`;
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
authorization_token UUID NOT NULL UNIQUE DEFAULT gen_random_uuid(),
username VARCHAR(20) NOT NULL UNIQUE,
email VARCHAR(254) NOT NULL UNIQUE,
email_verified boolean NOT NULL DEFAULT false,
password TEXT NOT NULL,
avatar boolean NOT NULL DEFAULT false,
roles TEXT[] NOT NULL DEFAULT ARRAY['user'],
timezone VARCHAR(64) DEFAULT NULL,
invited_by UUID REFERENCES users(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
last_seen TIMESTAMPTZ DEFAULT NOW() NOT NULL
);`;
} catch (error) {
logger.error(["Could not create the users table:", error as Error]);
throw error;
@ -35,29 +37,6 @@ export async function createTable(reservation?: ReservedSQL): Promise<void> {
}
}
export async function drop(
cascade: boolean,
reservation?: ReservedSQL,
): Promise<void> {
let selfReservation: boolean = false;
if (!reservation) {
reservation = await sql.reserve();
selfReservation = true;
}
try {
await reservation`DROP TABLE IF EXISTS users ${cascade ? "CASCADE" : ""};`;
} catch (error) {
logger.error(["Could not drop the users table:", error as Error]);
throw error;
} finally {
if (selfReservation) {
reservation.release();
}
}
}
// * Validation functions
// ? should support non english characters but wont mess up the url