define foreign keys

This commit is contained in:
yggverse 2024-11-16 15:39:42 +02:00
parent fdc6e739b4
commit 7370815f9d
16 changed files with 59 additions and 26 deletions

View File

@ -10,7 +10,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
"CREATE TABLE IF NOT EXISTS `app_browser` "CREATE TABLE IF NOT EXISTS `app_browser`
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_id` INTEGER NOT NULL `app_id` INTEGER NOT NULL,
FOREIGN KEY (`app_id`) REFERENCES `app`(`id`)
)", )",
[], [],
) )

View File

@ -16,7 +16,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
`app_browser_id` INTEGER NOT NULL, `app_browser_id` INTEGER NOT NULL,
`default_width` INTEGER NOT NULL, `default_width` INTEGER NOT NULL,
`default_height` INTEGER NOT NULL, `default_height` INTEGER NOT NULL,
`is_maximized` INTEGER NOT NULL `is_maximized` INTEGER NOT NULL,
FOREIGN KEY (`app_browser_id`) REFERENCES `app_browser`(`id`)
)", )",
[], [],
) )

View File

@ -10,7 +10,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
"CREATE TABLE IF NOT EXISTS `app_browser_window` "CREATE TABLE IF NOT EXISTS `app_browser_window`
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_id` INTEGER NOT NULL `app_browser_id` INTEGER NOT NULL,
FOREIGN KEY (`app_browser_id`) REFERENCES `app_browser`(`id`)
)", )",
[], [],
) )

View File

@ -10,7 +10,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab` "CREATE TABLE IF NOT EXISTS `app_browser_window_tab`
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_id` INTEGER NOT NULL `app_browser_window_id` INTEGER NOT NULL,
FOREIGN KEY (`app_browser_window_id`) REFERENCES `app_browser_window`(`id`)
)", )",
[], [],
) )

View File

@ -17,7 +17,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
`page_position` INTEGER NOT NULL, `page_position` INTEGER NOT NULL,
`is_pinned` INTEGER NOT NULL, `is_pinned` INTEGER NOT NULL,
`is_selected` INTEGER NOT NULL, `is_selected` INTEGER NOT NULL,
`is_attention` INTEGER NOT NULL `is_attention` INTEGER NOT NULL,
FOREIGN KEY (`app_browser_window_tab_id`) REFERENCES `app_browser_window_tab`(`id`)
)", )",
[], [],
) )

View File

@ -10,7 +10,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab_item_page` "CREATE TABLE IF NOT EXISTS `app_browser_window_tab_item_page`
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_tab_item_id` INTEGER NOT NULL `app_browser_window_tab_item_id` INTEGER NOT NULL,
FOREIGN KEY (`app_browser_window_tab_item_id`) REFERENCES `app_browser_window_tab_item`(`id`)
)", )",
[], [],
) )

View File

@ -12,7 +12,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_tab_item_page_id` INTEGER NOT NULL, `app_browser_window_tab_item_page_id` INTEGER NOT NULL,
`title` VARCHAR(1024) `title` VARCHAR(1024),
FOREIGN KEY (`app_browser_window_tab_item_page_id`) REFERENCES `app_browser_window_tab_item_page`(`id`)
)", )",
[], [],
) )

View File

@ -10,7 +10,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab_item_page_navigation` "CREATE TABLE IF NOT EXISTS `app_browser_window_tab_item_page_navigation`
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_tab_item_page_id` INTEGER NOT NULL `app_browser_window_tab_item_page_id` INTEGER NOT NULL,
FOREIGN KEY (`app_browser_window_tab_item_page_id`) REFERENCES `app_browser_window_tab_item_page`(`id`)
)", )",
[], [],
) )

View File

@ -10,7 +10,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab_item_page_navigation_request` "CREATE TABLE IF NOT EXISTS `app_browser_window_tab_item_page_navigation_request`
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_tab_item_page_navigation_id` INTEGER NOT NULL `app_browser_window_tab_item_page_navigation_id` INTEGER NOT NULL,
FOREIGN KEY (`app_browser_window_tab_item_page_navigation_id`) REFERENCES `app_browser_window_tab_item_page_navigation`(`id`)
)", )",
[], [],
) )

View File

@ -12,7 +12,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_tab_item_page_navigation_request_id` INTEGER NOT NULL, `app_browser_window_tab_item_page_navigation_request_id` INTEGER NOT NULL,
`text` VARCHAR(1024) `text` VARCHAR(1024),
FOREIGN KEY (`app_browser_window_tab_item_page_navigation_request_id`) REFERENCES `app_browser_window_tab_item_page_navigation_request`(`id`)
)", )",
[], [],
) )

View File

@ -12,7 +12,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_tab_item_id` INTEGER NOT NULL, `app_browser_window_tab_item_id` INTEGER NOT NULL,
`title` VARCHAR(1024) `title` VARCHAR(1024),
FOREIGN KEY (`app_browser_window_tab_item_id`) REFERENCES `app_browser_window_tab_item`(`id`)
)", )",
[], [],
) )

View File

@ -82,7 +82,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_id` INTEGER NOT NULL, `profile_id` INTEGER NOT NULL,
`time` INTEGER NOT NULL, `time` INTEGER NOT NULL,
`request` TEXT NOT NULL `request` TEXT NOT NULL,
FOREIGN KEY (`profile_id`) REFERENCES `profile`(`id`)
)", )",
[], [],
) )

View File

@ -21,7 +21,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_id` INTEGER NOT NULL, `profile_id` INTEGER NOT NULL,
`time` INTEGER NOT NULL, `time` INTEGER NOT NULL,
`request` TEXT NOT NULL `request` TEXT NOT NULL,
FOREIGN KEY (`profile_id`) REFERENCES `profile`(`id`)
)", )",
[], [],
) )

View File

@ -27,7 +27,7 @@ impl Identity {
for auth_record in auth_records { for auth_record in auth_records {
if let Ok(gemini_records) = self.gemini.database.records() { if let Ok(gemini_records) = self.gemini.database.records() {
for gemini_record in gemini_records { for gemini_record in gemini_records {
if gemini_record.id == auth_record.gemini_id { if gemini_record.id == auth_record.profile_identity_gemini_id {
return Some(gemini_record.pem); return Some(gemini_record.pem);
} }
} }

View File

@ -5,10 +5,10 @@ use sqlite::{Connection, Error, Transaction};
pub struct Table { pub struct Table {
//pub id: i64, //pub id: i64,
//pub profile_id: i64, //pub profile_id: i64,
pub gemini_id: i64, pub profile_identity_gemini_id: i64,
} }
/// Storage for `gemini_id` + `url` auth pairs /// Storage for `profile_identity_gemini_id` + `url` auth pairs
pub struct Database { pub struct Database {
connection: Rc<RwLock<Connection>>, connection: Rc<RwLock<Connection>>,
profile_id: Rc<i64>, // multi-profile relationship profile_id: Rc<i64>, // multi-profile relationship
@ -41,15 +41,18 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
tx.execute( tx.execute(
"CREATE TABLE IF NOT EXISTS `profile_identity_gemini_auth` "CREATE TABLE IF NOT EXISTS `profile_identity_gemini_auth`
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_id` INTEGER NOT NULL, `profile_id` INTEGER NOT NULL,
`gemini_id` INTEGER NOT NULL, `profile_identity_gemini_id` INTEGER NOT NULL,
`is_active` INTEGER NOT NULL, `is_active` INTEGER NOT NULL,
`url` TEXT NOT NULL, `url` TEXT NOT NULL,
FOREIGN KEY (`profile_id`) REFERENCES `profile`(`id`),
FOREIGN KEY (`profile_identity_gemini_id`) REFERENCES `profile_identity_gemini`(`id`),
UNIQUE ( UNIQUE (
`profile_id`, `profile_id`,
`gemini_id`, `profile_identity_gemini_id`,
`is_active`, `is_active`,
`url` `url`
) )
@ -62,15 +65,17 @@ pub fn select(tx: &Transaction, profile_id: i64, url: Option<&str>) -> Result<Ve
let mut stmt = tx.prepare( let mut stmt = tx.prepare(
"SELECT `id`, "SELECT `id`,
`profile_id`, `profile_id`,
`gemini_id` FROM `profile_identity_gemini_auth` `profile_identity_gemini_id`
WHERE `profile_id` = ? AND `url` LIKE ?",
FROM `profile_identity_gemini_auth`
WHERE `profile_id` = ? AND `url` LIKE ?",
)?; )?;
let result = stmt.query_map((profile_id, url.unwrap_or("%")), |row| { let result = stmt.query_map((profile_id, url.unwrap_or("%")), |row| {
Ok(Table { Ok(Table {
//id: row.get(0)?, //id: row.get(0)?,
//profile_id: row.get(1)?, //profile_id: row.get(1)?,
gemini_id: row.get(2)?, profile_identity_gemini_id: row.get(2)?,
}) })
})?; })?;

View File

@ -40,7 +40,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
( (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_id` INTEGER NOT NULL, `profile_id` INTEGER NOT NULL,
`pem` TEXT NOT NULL `pem` TEXT NOT NULL,
FOREIGN KEY (`profile_id`) REFERENCES `profile`(`id`)
)", )",
[], [],
) )