resolve deadlock issue, draft migration features

This commit is contained in:
yggverse 2024-10-07 19:54:28 +03:00
parent 259ec321be
commit 366c5fad8e
12 changed files with 192 additions and 210 deletions

View file

@ -10,7 +10,11 @@ pub struct Database {
}
impl Database {
pub fn init(tx: &Transaction) -> Result<Database, Error> {
pub fn new() -> Self {
Self {}
}
pub fn init(tx: &Transaction) -> Result<usize, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser_window`
(
@ -18,9 +22,7 @@ impl Database {
`app_browser_id` INTEGER NOT NULL
)",
[],
)?;
Ok(Self {})
)
}
pub fn add(&self, tx: &Transaction, app_browser_id: &i64) -> Result<usize, Error> {

View file

@ -6,7 +6,7 @@ mod widget;
use database::Database;
use label::Label;
use page::Page;
use sqlite::{Connection, Transaction};
use sqlite::Transaction;
use widget::Widget;
use gtk::{
@ -16,11 +16,7 @@ use gtk::{
GestureClick, Notebook,
};
use std::{
cell::RefCell,
collections::HashMap,
sync::{Arc, RwLock},
};
use std::{cell::RefCell, collections::HashMap, sync::Arc};
// Common struct for HashMap index
pub struct TabItem {
@ -30,7 +26,6 @@ pub struct TabItem {
// Main
pub struct Tab {
profile_database_connection: Arc<RwLock<Connection>>,
database: Arc<Database>,
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
@ -47,7 +42,6 @@ pub struct Tab {
impl Tab {
// Construct
pub fn new(
profile_database_connection: Arc<RwLock<Connection>>,
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
@ -56,28 +50,7 @@ impl Tab {
action_update: Arc<SimpleAction>,
) -> Self {
// Init database
let database = {
// Init writable database connection
let mut connection = match profile_database_connection.write() {
Ok(connection) => connection,
Err(e) => todo!("{e}"),
};
// Init new transaction
let transaction = match connection.transaction() {
Ok(transaction) => transaction,
Err(e) => todo!("{e}"),
};
// Init database structure
match Database::init(&transaction) {
Ok(database) => match transaction.commit() {
Ok(_) => Arc::new(database),
Err(e) => todo!("{e}"),
},
Err(e) => todo!("{e}"),
}
};
let database = Arc::new(Database::new());
// Init empty HashMap index as no tabs appended yet
let index = RefCell::new(HashMap::new());
@ -87,7 +60,6 @@ impl Tab {
// Return non activated struct
Self {
profile_database_connection,
database,
// Define action links
action_tab_page_navigation_base,
@ -135,11 +107,7 @@ impl Tab {
let id = uuid_string_random();
// Init new tab components
let label = Arc::new(Label::new(
self.profile_database_connection.clone(),
id.clone(),
false,
));
let label = Arc::new(Label::new(id.clone(), false));
let page = Arc::new(Page::new(
id.clone(),
@ -335,4 +303,25 @@ impl Tab {
pub fn gobject(&self) -> &Notebook {
self.widget.gobject()
}
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Migrate self components
if let Err(e) = Database::init(&tx) {
return Err(e.to_string());
}
// Delegate migration to childs
if let Err(e) = Label::migrate(&tx) {
return Err(e.to_string());
}
/* @TODO
if let Err(e) = Page::migrate(&tx) {
return Err(e.to_string());
} */
// Success
Ok(())
}
}

View file

@ -11,7 +11,11 @@ pub struct Database {
}
impl Database {
pub fn init(tx: &Transaction) -> Result<Database, Error> {
pub fn new() -> Self {
Self {}
}
pub fn init(tx: &Transaction) -> Result<usize, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab`
(
@ -20,9 +24,7 @@ impl Database {
`is_current` INTEGER NOT NULL
)",
[],
)?;
Ok(Self {})
)
}
pub fn add(

View file

@ -5,12 +5,12 @@ mod widget;
use database::Database;
use pin::Pin;
use sqlite::{Connection, Transaction};
use sqlite::Transaction;
use title::Title;
use widget::Widget;
use gtk::{glib::GString, Box};
use std::sync::{Arc, RwLock};
use std::sync::Arc;
pub struct Label {
database: Arc<Database>,
@ -23,37 +23,9 @@ pub struct Label {
impl Label {
// Construct
pub fn new(
profile_database_connection: Arc<RwLock<Connection>>,
name: GString,
is_pinned: bool,
) -> Label {
pub fn new(name: GString, is_pinned: bool) -> Label {
// Init database
let database = {
/* @TODO init outside
// Init writable database connection
let mut connection = match profile_database_connection.write() {
Ok(connection) => connection,
Err(e) => todo!("{e}"),
};
// Init new transaction
let transaction = match connection.transaction() {
Ok(transaction) => transaction,
Err(e) => todo!("{e}"),
};
// Init database structure
match Database::init(&transaction) {
Ok(database) => match transaction.commit() {
Ok(_) => Arc::new(database),
Err(e) => todo!("{e}"),
},
Err(e) => todo!("{e}"),
} */
Arc::new(Database::new())
};
let database = Arc::new(Database::new());
// Components
let pin = Arc::new(Pin::new(is_pinned));
@ -135,4 +107,18 @@ impl Label {
pub fn gobject(&self) -> &Box {
&self.widget.gobject()
}
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Migrate self components
if let Err(e) = Database::init(&tx) {
return Err(e.to_string());
}
// Delegate migration to childs
// nothing yet..
// Success
Ok(())
}
}

View file

@ -15,7 +15,7 @@ impl Database {
Self {}
}
pub fn init(tx: &Transaction) -> Result<Database, Error> {
pub fn init(tx: &Transaction) -> Result<usize, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab_label`
(
@ -24,9 +24,7 @@ impl Database {
`is_pinned` INTEGER NOT NULL
)",
[],
)?;
Ok(Self {})
)
}
pub fn add(