From b3477fc2a3ddf421fbebff1384b6b75961ed188c Mon Sep 17 00:00:00 2001 From: CaptOrb Date: Sun, 24 Aug 2025 16:38:18 +0100 Subject: [PATCH] add initial DB schema for server --- server/migrations/1_create_molci_schema.sql | 56 +++++++++++++++++++++ server/src/server.ts | 4 +- 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 server/migrations/1_create_molci_schema.sql diff --git a/server/migrations/1_create_molci_schema.sql b/server/migrations/1_create_molci_schema.sql new file mode 100644 index 0000000..176e3d4 --- /dev/null +++ b/server/migrations/1_create_molci_schema.sql @@ -0,0 +1,56 @@ +CREATE TABLE forges ( + forge_id INTEGER PRIMARY KEY NOT NULL, + display_name VARCHAR(255) NOT NULL, -- e.g gitea + base_url TEXT NOT NULL, -- API base URL + client_id VARCHAR(64) NOT NULL, -- OAuth client ID + client_secret VARCHAR(128) NOT NULL -- OAuth secret +); + +-- MOLCI users +CREATE TABLE users ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + username VARCHAR(255) NOT NULL UNIQUE +); + +-- Forge accounts linked to MOLCI users +CREATE TABLE user_forge_accounts ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, -- MOLCI internal ID + user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE, -- Foreign key to the users table, pointing to the MOLCI user that owns this forge account + forge_id INT NOT NULL REFERENCES forges(forge_id) ON DELETE CASCADE, + forge_user_id VARCHAR(64) NOT NULL, + access_token TEXT NOT NULL, -- OAuth access token + token_expires_at TIMESTAMP, + UNIQUE(user_id, forge_id) -- Only one account per forge per user +); + +-- Repos from forges +CREATE TABLE repositories ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + forge_id INT NOT NULL REFERENCES forges(forge_id) ON DELETE CASCADE, + forge_repo_id VARCHAR(255) NOT NULL, + repo_name VARCHAR(500) NOT NULL, + description TEXT, + clone_url TEXT NOT NULL, + ssh_url TEXT, + html_url TEXT, -- might be useful for linking to the repo in our frontend + is_private BOOLEAN DEFAULT FALSE, + default_branch VARCHAR(100) DEFAULT 'main', -- need to know the main branch to trigger builds + created_at TIMESTAMP DEFAULT NOW(), + updated_at TIMESTAMP DEFAULT NOW(), + UNIQUE(forge_id, forge_repo_id) +); + +INSERT INTO forges (forge_id, display_name, base_url, client_id, client_secret) +VALUES (0, 'gitea', 'http://localhost:3000', 'dev_client_id', 'dev_client_secret'); + +INSERT INTO repositories (forge_id, forge_repo_id, repo_name, description, clone_url, ssh_url, html_url, is_private) +VALUES + (0, '1', 'my-awesome-project', 'A sample project for testing MOLCI', + 'http://localhost:3000/testuser/my-awesome-project.git', + 'git@localhost:testuser/my-awesome-project.git', + 'http://localhost:3000/testuser/my-awesome-project', false), + + (0, '2', 'molci-frontend', 'Frontend for MOLCI CI system', + 'http://localhost:3000/testuser/molci-frontend.git', + 'git@localhost:testuser/molci-frontend.git', + 'http://localhost:3000/testuser/molci-frontend', false); diff --git a/server/src/server.ts b/server/src/server.ts index b857308..81439e7 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -23,7 +23,7 @@ api.register({ const app = express(); app.use(express.json()); - app.get("/ping", async (_req, res) => { +app.get("/ping", async (_req, res) => { try { const result = await pool.query("SELECT NOW()"); res.json({ success: true, time: result.rows[0].now }); @@ -33,7 +33,7 @@ app.use(express.json()); .status(500) .json({ success: false, error: "Database connection failed" }); } -}); +}); api.init(); app.use((req, res) => api.handleRequest(req as Request, req, res));