From 7b3bca17141bf872091c0a9d60612a43c07ade57 Mon Sep 17 00:00:00 2001 From: scx1332 Date: Wed, 25 Oct 2023 12:33:27 +0200 Subject: [PATCH] Merged migrations into one file --- .../migrations/20221026000000_init.sql | 59 ++++++++++++------- .../migrations/20230819000000_paid_date.sql | 1 - .../migrations/20230820000000_replaced.sql | 1 - .../migrations/20231017000000_chain_tx.sql | 5 -- .../migrations/20231019000000_scan.sql | 10 ---- .../migrations/20231023000000_chain.sql | 3 - .../erc20_payment_lib/src/db/model/tx_dao.rs | 2 +- crates/erc20_payment_lib/src/db/ops/tx_ops.rs | 52 ++++++++++++++++ 8 files changed, 90 insertions(+), 43 deletions(-) delete mode 100644 crates/erc20_payment_lib/migrations/20230819000000_paid_date.sql delete mode 100644 crates/erc20_payment_lib/migrations/20230820000000_replaced.sql delete mode 100644 crates/erc20_payment_lib/migrations/20231017000000_chain_tx.sql delete mode 100644 crates/erc20_payment_lib/migrations/20231019000000_scan.sql delete mode 100644 crates/erc20_payment_lib/migrations/20231023000000_chain.sql diff --git a/crates/erc20_payment_lib/migrations/20221026000000_init.sql b/crates/erc20_payment_lib/migrations/20221026000000_init.sql index 7cd6223c..08b3abae 100644 --- a/crates/erc20_payment_lib/migrations/20221026000000_init.sql +++ b/crates/erc20_payment_lib/migrations/20221026000000_init.sql @@ -6,25 +6,26 @@ CREATE TABLE "tx" to_addr TEXT NOT NULL, chain_id INTEGER NOT NULL, gas_limit INTEGER NULL, - max_fee_per_gas TEXT NOT NULL, - priority_fee TEXT NOT NULL, + max_fee_per_gas TEXT NULL, + priority_fee TEXT NULL, val TEXT NOT NULL, nonce INTEGER NULL, processing INTEGER NOT NULL, call_data TEXT NULL, - created_date DATETIME NOT NULL, - first_processed DATETIME NULL, + created_date TEXT NOT NULL, + first_processed TEXT NULL, tx_hash TEXT NULL, signed_raw_data TEXT NULL, - signed_date DATETIME NULL, - broadcast_date DATETIME NULL, + signed_date TEXT NULL, + broadcast_date TEXT NULL, broadcast_count INTEGER NOT NULL, - confirm_date DATETIME NULL, + confirm_date TEXT NULL, block_number INTEGER NULL, chain_status INTEGER NULL, fee_paid TEXT NULL, - error TEXT NULL -); + error TEXT NULL, + orig_tx_id INTEGER NULL +) strict; CREATE INDEX "idx_tx_created_date" ON "tx" (created_date); CREATE INDEX "idx_tx_first_processed" ON "tx" (first_processed); @@ -43,14 +44,15 @@ CREATE TABLE "chain_tx" priority_fee TEXT NULL, val TEXT NOT NULL, nonce INTEGER NOT NULL, - checked_date DATETIME NOT NULL, - blockchain_date DATETIME NOT NULL, + checked_date TEXT NOT NULL, + blockchain_date TEXT NOT NULL, block_number INTEGER NOT NULL, chain_status INTEGER NOT NULL, fee_paid TEXT NULL, - error TEXT NULL -); - + error TEXT NULL, + balance_eth TEXT NULL, + balance_glm TEXT NULL +) strict; CREATE TABLE "token_transfer" ( @@ -61,12 +63,13 @@ CREATE TABLE "token_transfer" chain_id INTEGER NOT NULL, token_addr TEXT NULL, token_amount TEXT NOT NULL, - create_date DATETIME NOT NULL, + create_date TEXT NOT NULL, + paid_date TEXT NULL, tx_id INTEGER NULL, fee_paid TEXT NULL, error TEXT NULL, CONSTRAINT "fk_token_transfer_tx" FOREIGN KEY ("tx_id") REFERENCES "tx" ("id") -); +) strict; CREATE TABLE "transfer_in" ( @@ -78,9 +81,9 @@ CREATE TABLE "transfer_in" token_addr TEXT NULL, token_amount TEXT NOT NULL, tx_hash TEXT NULL, - requested_date DATETIME NOT NULL, - received_date DATETIME NULL -); + requested_date TEXT NOT NULL, + received_date TEXT NULL +) strict; CREATE TABLE "chain_transfer" ( @@ -91,8 +94,10 @@ CREATE TABLE "chain_transfer" token_addr TEXT NULL, token_amount TEXT NOT NULL, chain_tx_id INTEGER NOT NULL, + fee_paid TEXT NOT NULL, + blockchain_date TEXT NOT NULL, CONSTRAINT "fk_chain_transfer_tx" FOREIGN KEY ("chain_tx_id") REFERENCES "chain_tx" ("id") -); +) strict; CREATE TABLE "allowance" ( @@ -104,10 +109,20 @@ CREATE TABLE "allowance" chain_id INTEGER NOT NULL, tx_id INTEGER NULL, fee_paid TEXT NULL, - confirm_date DATETIME NULL, + confirm_date TEXT NULL, error TEXT NULL, CONSTRAINT "fk_allowance_tx" FOREIGN KEY ("tx_id") REFERENCES "tx" ("id") -); +) strict; + +CREATE TABLE "scan_info" +( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + chain_id INTEGER NOT NULL, + filter TEXT NOT NULL, + start_block INTEGER NOT NULL, + last_block INTEGER NOT NULL +) strict; +CREATE UNIQUE INDEX "idx_scan_info_chain_id" ON "scan_info" ("chain_id", "filter"); diff --git a/crates/erc20_payment_lib/migrations/20230819000000_paid_date.sql b/crates/erc20_payment_lib/migrations/20230819000000_paid_date.sql deleted file mode 100644 index d58f44b6..00000000 --- a/crates/erc20_payment_lib/migrations/20230819000000_paid_date.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE "token_transfer" ADD COLUMN "paid_date" DATETIME NULL; \ No newline at end of file diff --git a/crates/erc20_payment_lib/migrations/20230820000000_replaced.sql b/crates/erc20_payment_lib/migrations/20230820000000_replaced.sql deleted file mode 100644 index 81904995..00000000 --- a/crates/erc20_payment_lib/migrations/20230820000000_replaced.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE "tx" ADD COLUMN "orig_tx_id" INT NULL; diff --git a/crates/erc20_payment_lib/migrations/20231017000000_chain_tx.sql b/crates/erc20_payment_lib/migrations/20231017000000_chain_tx.sql deleted file mode 100644 index aeb02392..00000000 --- a/crates/erc20_payment_lib/migrations/20231017000000_chain_tx.sql +++ /dev/null @@ -1,5 +0,0 @@ -CREATE UNIQUE INDEX "chain_tx_tx_hash" ON "chain_tx" (tx_hash); - -ALTER TABLE "chain_transfer" ADD COLUMN "fee_paid" TEXT NULL; -ALTER TABLE "chain_transfer" ADD COLUMN "blockchain_date" DATETIME NULL; - diff --git a/crates/erc20_payment_lib/migrations/20231019000000_scan.sql b/crates/erc20_payment_lib/migrations/20231019000000_scan.sql deleted file mode 100644 index 24d978b1..00000000 --- a/crates/erc20_payment_lib/migrations/20231019000000_scan.sql +++ /dev/null @@ -1,10 +0,0 @@ -CREATE TABLE "scan_info" -( - id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - chain_id INTEGER NOT NULL, - filter TEXT NOT NULL, - start_block INTEGER NOT NULL, - last_block INTEGER NOT NULL -); - -CREATE UNIQUE INDEX "idx_scan_info_chain_id" ON "scan_info" ("chain_id", "filter"); \ No newline at end of file diff --git a/crates/erc20_payment_lib/migrations/20231023000000_chain.sql b/crates/erc20_payment_lib/migrations/20231023000000_chain.sql deleted file mode 100644 index 22e44f19..00000000 --- a/crates/erc20_payment_lib/migrations/20231023000000_chain.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE "chain_tx" ADD COLUMN "balance_eth" TEXT NULL; -ALTER TABLE "chain_tx" ADD COLUMN "balance_glm" TEXT NULL; - diff --git a/crates/erc20_payment_lib/src/db/model/tx_dao.rs b/crates/erc20_payment_lib/src/db/model/tx_dao.rs index e682ba72..f19be0bb 100644 --- a/crates/erc20_payment_lib/src/db/model/tx_dao.rs +++ b/crates/erc20_payment_lib/src/db/model/tx_dao.rs @@ -1,7 +1,7 @@ use chrono::{DateTime, Utc}; use serde::Serialize; -#[derive(Serialize, sqlx::FromRow, Debug, Clone)] +#[derive(Serialize, sqlx::FromRow, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct TxDao { pub id: i64, diff --git a/crates/erc20_payment_lib/src/db/ops/tx_ops.rs b/crates/erc20_payment_lib/src/db/ops/tx_ops.rs index 6d0e63a9..43fec0aa 100644 --- a/crates/erc20_payment_lib/src/db/ops/tx_ops.rs +++ b/crates/erc20_payment_lib/src/db/ops/tx_ops.rs @@ -239,3 +239,55 @@ WHERE id = $1 .await?; Ok(tx.clone()) } + +#[tokio::test] +async fn tx_test() -> sqlx::Result<()> { + println!("Start tx_test..."); + + use crate::db::create_sqlite_connection; + let conn = create_sqlite_connection(None, None, false, true) + .await + .unwrap(); + + println!("In memory DB created"); + + let mut tx_to_insert = TxDao { + id: -1, + tx_hash: Some("0x13d8a54dec1c0a30f1cd5129f690c3e27b9aadd59504957bad4d247966dadae7".to_string()), + signed_raw_data: None, + signed_date: Some(chrono::Utc::now()), + broadcast_date: Some(chrono::Utc::now()), + broadcast_count: 0, + method: "".to_string(), + from_addr: "0x001066290077e38f222cc6009c0c7a91d5192303".to_string(), + to_addr: "0xbcfe9736a4f5bf2e43620061ff3001ea0d003c0f".to_string(), + chain_id: 987789, + gas_limit: Some(100000), + max_fee_per_gas: "110000000000".to_string(), + priority_fee: "5110000000000".to_string(), + val: "0".to_string(), + nonce: Some(1), + processing: 0, + call_data: None, + created_date: chrono::Utc::now(), + block_number: Some(119677), + chain_status: Some(1), + fee_paid: Some("83779300533141".to_string()), + error: Some("Test error message".to_string()), + orig_tx_id: None, + engine_message: None, + engine_error: None, + first_processed: None, + confirm_date: None + }; + + let tx_from_insert = insert_tx(&conn, &tx_to_insert).await?; + tx_to_insert.id = tx_from_insert.id; + let tx_from_dao = get_transaction(&conn, tx_from_insert.id).await?; + + //all three should be equal + assert_eq!(tx_to_insert, tx_from_dao); + assert_eq!(tx_from_insert, tx_from_dao); + + Ok(()) +}