Fixed Database creation, The error was that there were 2 simultaenous Database connections, causing an error when both attempted to connect at once.

This commit is contained in:
Lynixenn
2025-03-30 22:59:19 +02:00
parent fb916e9e9f
commit 8f27246234
8 changed files with 52 additions and 73 deletions

View File

@@ -7,18 +7,10 @@
// +++ IMPORTS ------------------------------------------------------ //
import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
import {
dirname,
fromFileUrl,
join,
} from "https://deno.land/std@0.224.0/path/mod.ts";
// +++ VARIABLES ---------------------------------------------------- //
const _dirname: string = dirname(fromFileUrl(import.meta.url));
const dbPath: string = join(_dirname, "../database/esp-projekt.sqlite");
const db = new DB(dbPath);
export function createDatabase(): void {
export function createDatabase(db: DB): void {
db.execute(`
CREATE TABLE IF NOT EXISTS accounts (
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -72,12 +64,17 @@ export function createDatabase(): void {
chat_name TEXT,
participants TEXT,
created_at TEXT
)
);
CREATE TABLE IF NOT EXISTS marker (
id INTEGER PRIMARY KEY AUTOINCREMENT
);
`);
}
// Sample data generated using AI, does not work yet and will be adjusted
export function insertSampleData(): void {
export function insertSampleData(db: DB): void {
db.query(
`INSERT INTO accounts (user_group, bio, displayname, username, user_email, password, password_salt, firstname, surname, account_created, blocked_users, followers, following, contacts) VALUES
('admin', 'Admin bio', 'Admin User', 'admin', 'admin@example.com', 'pw1', 'salt1', 'Admin', 'User', '2024-01-01', '[]', '[]', '[]', '[]'),

View File

@@ -77,48 +77,53 @@ export function queryDatabase<T>(
// +++ DATABASE INITIALIZATION -------------------------------------- //
export async function ensureDatabaseExists(): Promise<void> {
const dbDir = dirname(dbPath);
let dbInstance: DB | null = null; // Avoids hard to decode errors because it Throws one if it cant continue.
try {
// Check if the database directory exists, create it if not
const dbDir = dirname(dbPath);
try {
await Deno.stat(dbDir);
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// Create the database directory
await Deno.mkdir(dbDir, { recursive: true });
if (error instanceof Deno.errors.NotFound) { // Deno.errors.NotFound is a type of error that is thrown when a file or directory is not found.
// In short, It's a type, and it makes sure that the "error" variable is of type Deno.errors.NotFound.
console.log(`Created database directory: ${dbDir}`);
} else {
throw error;
}
}
console.log(`Opening database connection: ${dbPath}`);
dbInstance = new DB(dbPath);
// Check if the database file exists
try {
await Deno.stat(dbPath);
console.log("Database file already exists");
dbInstance.query("SELECT 1 FROM marker LIMIT 1;");
console.log("Database already initialized (marker table found).");
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
createDatabaseIfNotExist();
insertSamples();
// Nothing, file will be created below
if (error instanceof Error) {
console.log(
"Marker table not found or query failed. Initializing database tables.",
);
db_create.createDatabase(dbInstance);
db_create.insertSampleData(dbInstance);
console.log("Database initialization complete.");
} else {
throw error;
}
}
} catch (error) {
console.error("Error ensuring database exists:", error);
throw error;
console.error(
"Error during database existence check or initialization:",
error,
);
} finally {
if (dbInstance) {
dbInstance.close();
console.log("Database connection closed.");
} else {
console.log("Database connection was not opened.");
}
}
}
export function createDatabaseIfNotExist(): void {
db_create.createDatabase();
}
export function insertSamples(): void {
db_create.insertSampleData();
}
// +++ ACCOUNT FUNCTIONS -------------------------------------------- //
export const getAllUsersFromDB = () => getAllUsersFromDBInternal(db);
export const getUserByUsername = (username: string) =>