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

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