Add SQLite support as alternative database backend

Support config-driven choice between MySQL and SQLite via DB_DRIVER
constant, defaulting to MySQL for backward compatibility. All SQL
adaptation lives in Database.php (UDFs + query rewriting), so model
files need no changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

SQLite: remove FK constraints, revert 0→null sentinel changes

The SQLite schema had FOREIGN KEY constraints that don't exist in the
MySQL schema. These forced a cascade of 0→null changes to satisfy FK
enforcement. Removing them keeps the two backends behaviorally consistent
and minimises the diff. Real SQLite compat fixes (UDFs, query rewriting,
rowCount→count, Router fixes, EditAlbum guard) are preserved.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 12:04:42 +01:00
parent b0ee3081a6
commit a71b8c9717
9 changed files with 286 additions and 55 deletions

103
schema.sqlite.sql Normal file
View File

@@ -0,0 +1,103 @@
-- SQLite schema for Kabuki CMS / pics
--
-- Usage:
-- sqlite3 data/pics.sqlite < schema.sqlite.sql
--
-- Config (add to config.php):
-- define('DB_DRIVER', 'sqlite');
-- define('DB_FILE', __DIR__ . '/data/pics.sqlite');
CREATE TABLE IF NOT EXISTS users (
id_user INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
surname TEXT NOT NULL,
slug TEXT NOT NULL UNIQUE,
emailaddress TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
creation_time INTEGER NOT NULL,
last_action_time INTEGER,
ip_address TEXT,
is_admin INTEGER NOT NULL DEFAULT 0,
reset_key TEXT,
reset_blocked_until INTEGER
);
CREATE TABLE IF NOT EXISTS assets (
id_asset INTEGER PRIMARY KEY,
id_user_uploaded INTEGER NOT NULL,
subdir TEXT NOT NULL,
filename TEXT NOT NULL,
title TEXT,
slug TEXT UNIQUE,
mimetype TEXT,
image_width INTEGER,
image_height INTEGER,
date_captured TEXT,
priority INTEGER DEFAULT 0
);
CREATE TABLE IF NOT EXISTS assets_meta (
id_asset INTEGER NOT NULL,
variable TEXT NOT NULL,
value TEXT,
PRIMARY KEY (id_asset, variable)
);
CREATE TABLE IF NOT EXISTS assets_thumbs (
id_asset INTEGER NOT NULL,
width INTEGER NOT NULL,
height INTEGER NOT NULL,
mode TEXT,
filename TEXT,
PRIMARY KEY (id_asset, width, height, mode)
);
CREATE TABLE IF NOT EXISTS tags (
id_tag INTEGER PRIMARY KEY,
id_parent INTEGER,
id_asset_thumb INTEGER,
id_user_owner INTEGER,
tag TEXT NOT NULL,
slug TEXT NOT NULL UNIQUE,
description TEXT,
kind TEXT NOT NULL DEFAULT 'Tag',
count INTEGER DEFAULT 0
);
CREATE TABLE IF NOT EXISTS assets_tags (
id_asset INTEGER NOT NULL,
id_tag INTEGER NOT NULL,
PRIMARY KEY (id_asset, id_tag)
);
CREATE TABLE IF NOT EXISTS posts_assets (
id_post INTEGER NOT NULL,
id_asset INTEGER NOT NULL,
PRIMARY KEY (id_post, id_asset)
);
CREATE TABLE IF NOT EXISTS posts_tags (
id_post INTEGER NOT NULL,
id_tag INTEGER NOT NULL,
PRIMARY KEY (id_post, id_tag)
);
CREATE TABLE IF NOT EXISTS settings (
id_user INTEGER NOT NULL,
variable TEXT NOT NULL,
value TEXT,
time_set TEXT DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id_user, variable)
);
CREATE TABLE IF NOT EXISTS log_errors (
id_entry INTEGER PRIMARY KEY,
id_user INTEGER,
message TEXT,
debug_info TEXT,
file TEXT,
line INTEGER,
request_uri TEXT,
time TEXT DEFAULT CURRENT_TIMESTAMP,
ip_address TEXT
);