feat(db): add properties table and junction table for custom properties
This commit is contained in:
parent
8860ace51f
commit
ecc991cc24
1 changed files with 28 additions and 4 deletions
32
src/db.rs
32
src/db.rs
|
@ -25,26 +25,50 @@ mod db_impl {
|
||||||
// Create the database schema
|
// Create the database schema
|
||||||
pub async fn create_schema(&self) -> Result<(), Error> {
|
pub async fn create_schema(&self) -> Result<(), Error> {
|
||||||
let conn = self.conn.lock().await;
|
let conn = self.conn.lock().await;
|
||||||
|
|
||||||
|
// 1. Properties table
|
||||||
|
conn.execute_batch(
|
||||||
|
"CREATE TABLE IF NOT EXISTS properties (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
name TEXT NOT NULL UNIQUE, // Property name
|
||||||
|
global_usage_count INTEGER DEFAULT 0 // Track usage across ALL URLs
|
||||||
|
);"
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// 2. URLs table
|
||||||
conn.execute_batch(
|
conn.execute_batch(
|
||||||
"CREATE TABLE IF NOT EXISTS urls (
|
"CREATE TABLE IF NOT EXISTS urls (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
url TEXT NOT NULL,
|
url TEXT NOT NULL UNIQUE, // Enforce unique URLs
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
);",
|
);",
|
||||||
)?;
|
)?;
|
||||||
logging::log!("URLs table created or verified");
|
logging::log!("URLs table created or verified");
|
||||||
|
|
||||||
|
// 3. Items table
|
||||||
conn.execute_batch(
|
conn.execute_batch(
|
||||||
"CREATE TABLE IF NOT EXISTS items (
|
"CREATE TABLE IF NOT EXISTS items (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
|
url_id INTEGER NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
description TEXT,
|
description TEXT,
|
||||||
wikidata_id TEXT,
|
wikidata_id TEXT,
|
||||||
custom_properties TEXT,
|
FOREIGN KEY (url_id) REFERENCES urls(id) ON DELETE CASCADE
|
||||||
url_id INTEGER,
|
|
||||||
FOREIGN KEY (url_id) REFERENCES urls (id)
|
|
||||||
);",
|
);",
|
||||||
)?;
|
)?;
|
||||||
logging::log!("Items table updated with foreign key to URLs table");
|
logging::log!("Items table updated with foreign key to URLs table");
|
||||||
|
|
||||||
|
// 4. Junction table for custom properties
|
||||||
|
conn.execute_batch(
|
||||||
|
"CREATE TABLE IF NOT EXISTS item_properties (
|
||||||
|
item_id TEXT NOT NULL,
|
||||||
|
property_id INTEGER NOT NULL,
|
||||||
|
value TEXT NOT NULL,
|
||||||
|
PRIMARY KEY (item_id, property_id),
|
||||||
|
FOREIGN KEY (item_id) REFERENCES items(id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (property_id) REFERENCES properties(id) ON DELETE CASCADE
|
||||||
|
);"
|
||||||
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue