@@ -38,8 +38,6 @@ pub async fn connect() -> Result<Pool<Sqlite>> {
3838 counterparty_pubkey TEXT,
3939 is_mine BOOLEAN,
4040 buyer_invoice TEXT,
41- buyer_token INTEGER,
42- seller_token INTEGER,
4341 request_id INTEGER,
4442 created_at INTEGER,
4543 expires_at INTEGER
@@ -66,11 +64,68 @@ pub async fn connect() -> Result<Pool<Sqlite>> {
6664 println ! ( "User created with pubkey: {}" , user. i0_pubkey) ;
6765 } else {
6866 pool = SqlitePool :: connect ( & db_url) . await ?;
67+
68+ // Migration: Drop buyer_token and seller_token columns if they exist
69+ migrate_remove_token_columns ( & pool) . await ?;
6970 }
7071
7172 Ok ( pool)
7273}
7374
75+ async fn migrate_remove_token_columns ( pool : & SqlitePool ) -> Result < ( ) > {
76+ println ! ( "Checking for legacy token columns..." ) ;
77+
78+ // Check if buyer_token column exists
79+ let buyer_token_exists = sqlx:: query_scalar :: < _ , i64 > (
80+ "SELECT COUNT(*) FROM pragma_table_info('orders') WHERE name = 'buyer_token'" ,
81+ )
82+ . fetch_one ( pool)
83+ . await ?;
84+
85+ // Check if seller_token column exists
86+ let seller_token_exists = sqlx:: query_scalar :: < _ , i64 > (
87+ "SELECT COUNT(*) FROM pragma_table_info('orders') WHERE name = 'seller_token'" ,
88+ )
89+ . fetch_one ( pool)
90+ . await ?;
91+
92+ // Drop buyer_token column if it exists
93+ if buyer_token_exists > 0 {
94+ println ! ( "Removing legacy buyer_token column..." ) ;
95+ match sqlx:: query ( "ALTER TABLE orders DROP COLUMN buyer_token" )
96+ . execute ( pool)
97+ . await
98+ {
99+ Ok ( _) => println ! ( "Successfully removed buyer_token column" ) ,
100+ Err ( e) => {
101+ println ! ( "Warning: Could not remove buyer_token column: {}" , e) ;
102+ // Continue execution - this is not critical
103+ }
104+ }
105+ }
106+
107+ // Drop seller_token column if it exists
108+ if seller_token_exists > 0 {
109+ println ! ( "Removing legacy seller_token column..." ) ;
110+ match sqlx:: query ( "ALTER TABLE orders DROP COLUMN seller_token" )
111+ . execute ( pool)
112+ . await
113+ {
114+ Ok ( _) => println ! ( "Successfully removed seller_token column" ) ,
115+ Err ( e) => {
116+ println ! ( "Warning: Could not remove seller_token column: {}" , e) ;
117+ // Continue execution - this is not critical
118+ }
119+ }
120+ }
121+
122+ if buyer_token_exists == 0 && seller_token_exists == 0 {
123+ println ! ( "No legacy token columns found - database is up to date" ) ;
124+ }
125+
126+ Ok ( ( ) )
127+ }
128+
74129#[ derive( Debug , Default , Clone , sqlx:: FromRow ) ]
75130pub struct User {
76131 /// The user's ID is the identity pubkey
@@ -214,8 +269,6 @@ pub struct Order {
214269 pub counterparty_pubkey : Option < String > ,
215270 pub is_mine : Option < bool > ,
216271 pub buyer_invoice : Option < String > ,
217- pub buyer_token : Option < u16 > ,
218- pub seller_token : Option < u16 > ,
219272 pub request_id : Option < i64 > ,
220273 pub created_at : Option < i64 > ,
221274 pub expires_at : Option < i64 > ,
@@ -248,8 +301,6 @@ impl Order {
248301 counterparty_pubkey : None ,
249302 is_mine : Some ( true ) ,
250303 buyer_invoice : None ,
251- buyer_token : None ,
252- seller_token : None ,
253304 request_id,
254305 created_at : Some ( chrono:: Utc :: now ( ) . timestamp ( ) ) ,
255306 expires_at : None ,
@@ -259,9 +310,8 @@ impl Order {
259310 r#"
260311 INSERT INTO orders (id, kind, status, amount, min_amount, max_amount,
261312 fiat_code, fiat_amount, payment_method, premium, trade_keys,
262- counterparty_pubkey, is_mine, buyer_invoice, buyer_token, seller_token,
263- request_id, created_at, expires_at)
264- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
313+ counterparty_pubkey, is_mine, buyer_invoice, request_id, created_at, expires_at)
314+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
265315 "# ,
266316 )
267317 . bind ( & order. id )
@@ -278,8 +328,6 @@ impl Order {
278328 . bind ( & order. counterparty_pubkey )
279329 . bind ( order. is_mine )
280330 . bind ( & order. buyer_invoice )
281- . bind ( order. buyer_token )
282- . bind ( order. seller_token )
283331 . bind ( order. request_id )
284332 . bind ( order. created_at )
285333 . bind ( order. expires_at )
@@ -359,8 +407,7 @@ impl Order {
359407 UPDATE orders
360408 SET kind = ?, status = ?, amount = ?, fiat_code = ?, min_amount = ?, max_amount = ?,
361409 fiat_amount = ?, payment_method = ?, premium = ?, trade_keys = ?, counterparty_pubkey = ?,
362- is_mine = ?, buyer_invoice = ?, created_at = ?, expires_at = ?, buyer_token = ?,
363- seller_token = ?
410+ is_mine = ?, buyer_invoice = ?, expires_at = ?
364411 WHERE id = ?
365412 "# ,
366413 )
@@ -377,10 +424,7 @@ impl Order {
377424 . bind ( & self . counterparty_pubkey )
378425 . bind ( self . is_mine )
379426 . bind ( & self . buyer_invoice )
380- . bind ( self . created_at )
381427 . bind ( self . expires_at )
382- . bind ( self . buyer_token )
383- . bind ( self . seller_token )
384428 . bind ( id)
385429 . execute ( pool)
386430 . await ?;
@@ -440,22 +484,15 @@ impl Order {
440484 }
441485
442486 pub async fn get_all_trade_keys ( pool : & SqlitePool ) -> Result < Vec < String > > {
443- #[ derive( sqlx:: FromRow ) ]
444- struct TradeKeyRow {
445- trade_keys : Option < String > ,
446- }
447-
448- let rows = sqlx:: query_as :: < _ , TradeKeyRow > (
449- "SELECT DISTINCT trade_keys FROM orders WHERE trade_keys IS NOT NULL"
487+ let trade_keys: Vec < String > = sqlx:: query_scalar :: < _ , Option < String > > (
488+ "SELECT DISTINCT trade_keys FROM orders WHERE trade_keys IS NOT NULL" ,
450489 )
451490 . fetch_all ( pool)
452- . await ?;
453-
454- let trade_keys: Vec < String > = rows
455- . into_iter ( )
456- . filter_map ( |row| row. trade_keys )
457- . collect ( ) ;
458-
491+ . await ?
492+ . into_iter ( )
493+ . flatten ( )
494+ . collect ( ) ;
495+
459496 Ok ( trade_keys)
460497 }
461498
0 commit comments