diff --git a/src/database/contacts.rs b/src/database/contacts.rs index 584069d06..11baa310a 100644 --- a/src/database/contacts.rs +++ b/src/database/contacts.rs @@ -1,5 +1,5 @@ use dash_sdk::platform::Identifier; -use rusqlite::params; +use rusqlite::{Connection, params}; #[derive(Debug, Clone)] pub struct ContactPrivateInfo { @@ -11,7 +11,7 @@ pub struct ContactPrivateInfo { } impl crate::database::Database { - pub fn init_contacts_tables(&self) -> rusqlite::Result<()> { + pub fn init_contacts_tables(&self, conn: &Connection) -> rusqlite::Result<()> { let sql = " CREATE TABLE IF NOT EXISTS contact_private_info ( owner_identity_id BLOB NOT NULL, @@ -24,7 +24,7 @@ impl crate::database::Database { PRIMARY KEY (owner_identity_id, contact_identity_id) ); "; - self.execute(sql, [])?; + conn.execute(sql, [])?; Ok(()) } diff --git a/src/database/dashpay.rs b/src/database/dashpay.rs index 8f47924d4..6c9a0edde 100644 --- a/src/database/dashpay.rs +++ b/src/database/dashpay.rs @@ -161,105 +161,6 @@ impl crate::database::Database { Ok(()) } - /// Initialize all DashPay-related database tables - pub fn init_dashpay_tables(&self) -> rusqlite::Result<()> { - // Profiles table - self.execute( - "CREATE TABLE IF NOT EXISTS dashpay_profiles ( - identity_id BLOB PRIMARY KEY, - display_name TEXT, - bio TEXT, - avatar_url TEXT, - avatar_hash BLOB, - avatar_fingerprint BLOB, - public_message TEXT, - created_at INTEGER DEFAULT (unixepoch()), - updated_at INTEGER DEFAULT (unixepoch()) - )", - [], - )?; - - // Contacts table (extends the existing contact_private_info) - self.execute( - "CREATE TABLE IF NOT EXISTS dashpay_contacts ( - owner_identity_id BLOB NOT NULL, - contact_identity_id BLOB NOT NULL, - username TEXT, - display_name TEXT, - avatar_url TEXT, - public_message TEXT, - contact_status TEXT DEFAULT 'pending', - created_at INTEGER DEFAULT (unixepoch()), - updated_at INTEGER DEFAULT (unixepoch()), - last_seen INTEGER, - PRIMARY KEY (owner_identity_id, contact_identity_id) - )", - [], - )?; - - // Contact requests table - self.execute( - "CREATE TABLE IF NOT EXISTS dashpay_contact_requests ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - from_identity_id BLOB NOT NULL, - to_identity_id BLOB NOT NULL, - to_username TEXT, - account_label TEXT, - request_type TEXT NOT NULL CHECK (request_type IN ('sent', 'received')), - status TEXT DEFAULT 'pending' CHECK (status IN ('pending', 'accepted', 'rejected', 'expired')), - created_at INTEGER DEFAULT (unixepoch()), - responded_at INTEGER, - expires_at INTEGER - )", - [], - )?; - - // Create index for faster queries - self.execute( - "CREATE INDEX IF NOT EXISTS idx_contact_requests_from - ON dashpay_contact_requests(from_identity_id)", - [], - )?; - - self.execute( - "CREATE INDEX IF NOT EXISTS idx_contact_requests_to - ON dashpay_contact_requests(to_identity_id)", - [], - )?; - - // Payments/transactions table - self.execute( - "CREATE TABLE IF NOT EXISTS dashpay_payments ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - tx_id TEXT UNIQUE NOT NULL, - from_identity_id BLOB NOT NULL, - to_identity_id BLOB NOT NULL, - amount INTEGER NOT NULL, - memo TEXT, - payment_type TEXT NOT NULL CHECK (payment_type IN ('sent', 'received')), - status TEXT DEFAULT 'pending' CHECK (status IN ('pending', 'confirmed', 'failed')), - created_at INTEGER DEFAULT (unixepoch()), - confirmed_at INTEGER - )", - [], - )?; - - // Create index for faster queries - self.execute( - "CREATE INDEX IF NOT EXISTS idx_payments_from - ON dashpay_payments(from_identity_id)", - [], - )?; - - self.execute( - "CREATE INDEX IF NOT EXISTS idx_payments_to - ON dashpay_payments(to_identity_id)", - [], - )?; - - Ok(()) - } - // Profile operations pub fn save_dashpay_profile( diff --git a/src/database/initialization.rs b/src/database/initialization.rs index 63fa025d1..c686a69b5 100644 --- a/src/database/initialization.rs +++ b/src/database/initialization.rs @@ -392,9 +392,9 @@ impl Database { self.initialize_token_order_table(&conn)?; self.initialize_identity_token_balances_table(&conn)?; - // Initialize contacts table - self.init_contacts_tables()?; - self.init_dashpay_tables()?; + // Initialize contacts and DashPay tables while holding the same connection lock + self.init_contacts_tables(&conn)?; + self.init_dashpay_tables_in_tx(&conn)?; Ok(()) }