35 lines · 1.5 KB
Raw Download
1
-- Database schema for the personal code repository site.
2
-- Import once (e.g. via phpMyAdmin) into the database named in config.php.
3
4
CREATE TABLE IF NOT EXISTS repositories (
5
  id          INT AUTO_INCREMENT PRIMARY KEY,
6
  slug        VARCHAR(100) NOT NULL UNIQUE,
7
  name        VARCHAR(150) NOT NULL,
8
  description TEXT NULL,
9
  language    VARCHAR(50) NULL,
10
  created_at  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
11
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
12
13
CREATE TABLE IF NOT EXISTS files (
14
  id         INT AUTO_INCREMENT PRIMARY KEY,
15
  repo_id    INT NOT NULL,
16
  filename   VARCHAR(255) NOT NULL,           -- display name / relative path
17
  filepath   VARCHAR(500) NOT NULL,           -- path relative to uploads/
18
  filesize   INT NOT NULL DEFAULT 0,
19
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
20
  FOREIGN KEY (repo_id) REFERENCES repositories(id) ON DELETE CASCADE,
21
  UNIQUE KEY uniq_repo_file (repo_id, filename)
22
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
23
24
-- Folders are first-class so that empty (and manually created) folders persist.
25
-- Each nesting level is stored as its own row, e.g. "a/b/c" also stores "a"
26
-- and "a/b".
27
CREATE TABLE IF NOT EXISTS folders (
28
  id         INT AUTO_INCREMENT PRIMARY KEY,
29
  repo_id    INT NOT NULL,
30
  path       VARCHAR(500) NOT NULL,           -- relative path, e.g. "src/utils"
31
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
32
  FOREIGN KEY (repo_id) REFERENCES repositories(id) ON DELETE CASCADE,
33
  UNIQUE KEY uniq_repo_folder (repo_id, path)
34
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
35