Expand database schema to support spell management
- Added `spells_enabled` column to `book_tools` table. - Introduced `book_spells` and `book_spell_tags` tables with related indexes for spell and tag storage. - Updated database initialization and migration scripts to include new tables and fields.
This commit is contained in:
@@ -19,7 +19,7 @@ const schemaVersion = 1;
|
||||
* Mets ta query, test, efface après
|
||||
*/
|
||||
const devQueries: string[] = [
|
||||
// "ALTER TABLE book_tools ADD COLUMN last_update INTEGER DEFAULT 0",
|
||||
|
||||
];
|
||||
|
||||
const isDev = process.env.NODE_ENV === 'development';
|
||||
@@ -84,6 +84,47 @@ function migrateFromOldSystem(db: Database): void {
|
||||
// Add last_update column to book_tools if missing (was added after v3)
|
||||
addColumn(db, 'book_tools', 'last_update', 'INTEGER DEFAULT 0');
|
||||
|
||||
// Add spells_enabled column to book_tools if missing
|
||||
addColumn(db, 'book_tools', 'spells_enabled', 'INTEGER NOT NULL DEFAULT 0');
|
||||
|
||||
// Create book_spell_tags table if missing
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS book_spell_tags (
|
||||
tag_id TEXT PRIMARY KEY,
|
||||
book_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
name_hash TEXT NOT NULL,
|
||||
color TEXT,
|
||||
last_update INTEGER DEFAULT 0,
|
||||
FOREIGN KEY (book_id) REFERENCES erit_books(book_id) ON DELETE CASCADE
|
||||
);
|
||||
`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spell_tags_book ON book_spell_tags(book_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spell_tags_user ON book_spell_tags(user_id)`);
|
||||
|
||||
// Create book_spells table if missing
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS book_spells (
|
||||
spell_id TEXT PRIMARY KEY,
|
||||
book_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
name_hash TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
appearance TEXT NOT NULL,
|
||||
tags TEXT NOT NULL,
|
||||
power_level TEXT,
|
||||
components TEXT,
|
||||
limitations TEXT,
|
||||
notes TEXT,
|
||||
last_update INTEGER DEFAULT 0,
|
||||
FOREIGN KEY (book_id) REFERENCES erit_books(book_id) ON DELETE CASCADE
|
||||
);
|
||||
`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spells_book ON book_spells(book_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spells_user ON book_spells(user_id)`);
|
||||
|
||||
// Drop old schema version table
|
||||
db.exec('DROP TABLE IF EXISTS _schema_version');
|
||||
}
|
||||
@@ -110,8 +151,9 @@ export function runMigrations(db: Database): void {
|
||||
const currentVersion = getDbVersion(db);
|
||||
if (currentVersion >= schemaVersion) return;
|
||||
|
||||
// v1 - book_tools table with last_update (for fresh DBs or DBs without old system)
|
||||
// v1 - book_tools table + spell book tables (for fresh DBs or DBs without old system)
|
||||
if (currentVersion < 1) {
|
||||
// Book Tools
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS book_tools (
|
||||
book_id TEXT NOT NULL,
|
||||
@@ -119,6 +161,7 @@ export function runMigrations(db: Database): void {
|
||||
characters_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
worlds_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
locations_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
spells_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
last_update INTEGER DEFAULT 0,
|
||||
UNIQUE (book_id, user_id),
|
||||
FOREIGN KEY (book_id) REFERENCES erit_books(book_id) ON DELETE CASCADE
|
||||
@@ -126,6 +169,44 @@ export function runMigrations(db: Database): void {
|
||||
`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_book_tools_book ON book_tools(book_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_book_tools_user ON book_tools(user_id)`);
|
||||
|
||||
// Book Spell Tags
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS book_spell_tags (
|
||||
tag_id TEXT PRIMARY KEY,
|
||||
book_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
name_hash TEXT NOT NULL,
|
||||
color TEXT,
|
||||
last_update INTEGER DEFAULT 0,
|
||||
FOREIGN KEY (book_id) REFERENCES erit_books(book_id) ON DELETE CASCADE
|
||||
);
|
||||
`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spell_tags_book ON book_spell_tags(book_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spell_tags_user ON book_spell_tags(user_id)`);
|
||||
|
||||
// Book Spells
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS book_spells (
|
||||
spell_id TEXT PRIMARY KEY,
|
||||
book_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
name_hash TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
appearance TEXT NOT NULL,
|
||||
tags TEXT NOT NULL,
|
||||
power_level TEXT,
|
||||
components TEXT,
|
||||
limitations TEXT,
|
||||
notes TEXT,
|
||||
last_update INTEGER DEFAULT 0,
|
||||
FOREIGN KEY (book_id) REFERENCES erit_books(book_id) ON DELETE CASCADE
|
||||
);
|
||||
`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spells_book ON book_spells(book_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spells_user ON book_spells(user_id)`);
|
||||
}
|
||||
|
||||
setDbVersion(db, schemaVersion);
|
||||
@@ -541,12 +622,47 @@ export function initializeSchema(db: Database): void {
|
||||
characters_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
worlds_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
locations_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
spells_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
last_update INTEGER DEFAULT 0,
|
||||
UNIQUE (book_id, user_id),
|
||||
FOREIGN KEY (book_id) REFERENCES erit_books(book_id) ON DELETE CASCADE
|
||||
);
|
||||
`);
|
||||
|
||||
// Book Spell Tags
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS book_spell_tags (
|
||||
tag_id TEXT PRIMARY KEY,
|
||||
book_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
name_hash TEXT NOT NULL,
|
||||
color TEXT,
|
||||
last_update INTEGER DEFAULT 0,
|
||||
FOREIGN KEY (book_id) REFERENCES erit_books(book_id) ON DELETE CASCADE
|
||||
);
|
||||
`);
|
||||
|
||||
// Book Spells
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS book_spells (
|
||||
spell_id TEXT PRIMARY KEY,
|
||||
book_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
name_hash TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
appearance TEXT NOT NULL,
|
||||
tags TEXT NOT NULL,
|
||||
power_level TEXT,
|
||||
components TEXT,
|
||||
limitations TEXT,
|
||||
notes TEXT,
|
||||
last_update INTEGER DEFAULT 0,
|
||||
FOREIGN KEY (book_id) REFERENCES erit_books(book_id) ON DELETE CASCADE
|
||||
);
|
||||
`);
|
||||
|
||||
// Create indexes for better performance
|
||||
createIndexes(db);
|
||||
}
|
||||
@@ -566,6 +682,10 @@ function createIndexes(db: Database): void {
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_world_elements_world ON book_world_elements(world_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_book_tools_book ON book_tools(book_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_book_tools_user ON book_tools(user_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spell_tags_book ON book_spell_tags(book_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spell_tags_user ON book_spell_tags(user_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spells_book ON book_spells(book_id)`);
|
||||
db.exec(`CREATE INDEX IF NOT EXISTS idx_spells_user ON book_spells(user_id)`);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user